Skip to content

Commit

Permalink
Merge pull request #27 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.5.0
  • Loading branch information
andyone authored May 6, 2023
2 parents fefdb71 + d5f2795 commit f0e557b
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 28 deletions.
39 changes: 39 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: "2"

checks:
argument-count:
enabled: true
config:
threshold: 6
complex-logic:
enabled: true
config:
threshold: 6
file-lines:
enabled: true
config:
threshold: 1000
method-complexity:
enabled: true
config:
threshold: 8
method-count:
enabled: true
config:
threshold: 20
method-lines:
enabled: true
config:
threshold: 100
nested-control-flow:
enabled: true
config:
threshold: 6
return-statements:
enabled: true
config:
threshold: 6
similar-code:
enabled: false
identical-code:
enabled: false
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
################################################################################

# This Makefile generated by GoMakeGen 2.1.0 using next command:
# This Makefile generated by GoMakeGen 2.2.0 using next command:
# gomakegen --mod .
#
# More info: https://kaos.sh/gomakegen
Expand Down Expand Up @@ -33,9 +33,9 @@ vendor: mod-vendor ## Make vendored copy of dependencies

test: ## Run tests
ifdef COVERAGE_FILE ## Save coverage data into file (String)
go test $(VERBOSE_FLAG) -covermode=count -coverprofile=$(COVERAGE_FILE) ./. ./bz2 ./gz ./tar ./tbz2 ./tgz ./txz ./tzst ./xz ./zip ./zst
go test $(VERBOSE_FLAG) -covermode=count -coverprofile=$(COVERAGE_FILE) ./. ./bz2 ./gz ./lz4 ./tar ./tbz2 ./tgz ./tlz4 ./txz ./tzst ./utils ./xz ./zip ./zst
else
go test $(VERBOSE_FLAG) -covermode=count ./. ./bz2 ./gz ./tar ./tbz2 ./tgz ./txz ./tzst ./xz ./zip ./zst
go test $(VERBOSE_FLAG) -covermode=count ./. ./bz2 ./gz ./lz4 ./tar ./tbz2 ./tgz ./tlz4 ./txz ./tzst ./utils ./xz ./zip ./zst
endif

mod-init:
Expand Down Expand Up @@ -87,6 +87,6 @@ help: ## Show this info
| sed 's/ifdef //' \
| awk 'BEGIN {FS = " .*?## "}; {printf " \033[32m%-14s\033[0m %s\n", $$1, $$2}'
@echo -e ''
@echo -e '\033[90mGenerated by GoMakeGen 2.1.0\033[0m\n'
@echo -e '\033[90mGenerated by GoMakeGen 2.2.0\033[0m\n'

################################################################################
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<p align="center"><a href="#readme"><img src="https://gh.kaos.st/npck.svg"/></a></p>

<p align="center">
<a href="https://kaos.sh/g/npck"><img src="https://gh.kaos.st/godoc.svg" alt="PkgGoDev" /></a>
<a href="https://kaos.sh/r/npck"><img src="https://gh.kaos.st/godoc.svg" alt="PkgGoDev" /></a>
<a href="https://kaos.sh/r/npck"><img src="https://kaos.sh/r/npck.svg" alt="GoReportCard" /></a>
<a href="https://kaos.sh/l/npck"><img src="https://kaos.sh/l/058a7dfd25582ff1093c.svg" alt="Code Climate Maintainability" /></a>
<a href="https://kaos.sh/b/npck"><img src="https://kaos.sh/b/fc322f23-4913-4edd-8f0f-33a3ce029add.svg" alt="Codebeat badge" /></a>
<br/>
<a href="https://kaos.sh/c/npck"><img src="https://kaos.sh/c/npck.svg" alt="Coverage Status" /></a>
<a href="https://kaos.sh/w/npck/ci"><img src="https://kaos.sh/w/npck/ci.svg" alt="GitHub Actions CI Status" /></a>
<a href="https://kaos.sh/w/npck/codeql"><img src="https://kaos.sh/w/npck/codeql.svg" alt="GitHub Actions CodeQL Status" /></a>
<a href="https://kaos.sh/c/npck"><img src="https://kaos.sh/c/npck.svg" alt="Coverage Status" /></a>
<a href="https://kaos.sh/b/npck"><img src="https://kaos.sh/b/fc322f23-4913-4edd-8f0f-33a3ce029add.svg" alt="Codebeat badge" /></a>
<a href="#license"><img src="https://gh.kaos.st/apache2.svg"></a>
</p>

Expand Down
4 changes: 2 additions & 2 deletions bz2/bz2.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"path/filepath"
"strings"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/essentialkaos/npck/utils"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -42,7 +42,7 @@ func Unpack(file, dir string) error {
output := strings.TrimSuffix(filepath.Base(file), ".bz2")
output = strings.TrimSuffix(output, ".BZ2")

path, err := securejoin.SecureJoin(dir, output)
path, err := utils.Join(dir, output)

if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module github.com/essentialkaos/npck
go 1.19

require (
github.com/cyphar/filepath-securejoin v0.2.3
github.com/essentialkaos/check v1.4.0
github.com/essentialkaos/ek/v12 v12.64.1
github.com/essentialkaos/ek/v12 v12.66.0
github.com/klauspost/compress v1.16.5
github.com/pierrec/lz4/v4 v4.1.17
github.com/ulikunitz/xz v0.5.11
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI=
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1nkuKk=
github.com/essentialkaos/check v1.4.0/go.mod h1:LMKPZ2H+9PXe7Y2gEoKyVAwUqXVgx7KtgibfsHJPus0=
github.com/essentialkaos/ek/v12 v12.64.1 h1:XjIF4GHCgVIY0S0KEz8g1OrNrRoHGU+vAUXSYIKNIbI=
github.com/essentialkaos/ek/v12 v12.64.1/go.mod h1:PFJckNu+x4mHqsjndUeYRDPgDgvZEgcjpy1RpCYtx4g=
github.com/essentialkaos/ek/v12 v12.66.0 h1:g/eg9IPKFDYHt0v5qB3b9pBeSSZ+N7gJdxdOLXIPFLA=
github.com/essentialkaos/ek/v12 v12.66.0/go.mod h1:PFJckNu+x4mHqsjndUeYRDPgDgvZEgcjpy1RpCYtx4g=
github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI=
github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
4 changes: 2 additions & 2 deletions gz/gz.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/klauspost/compress/gzip"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/essentialkaos/npck/utils"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -43,7 +43,7 @@ func Unpack(file, dir string) error {
output := strings.TrimSuffix(filepath.Base(file), ".gz")
output = strings.TrimSuffix(output, ".GZ")

path, err := securejoin.SecureJoin(dir, output)
path, err := utils.Join(dir, output)

if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions lz4/lz4.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/pierrec/lz4/v4"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/essentialkaos/npck/utils"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -43,7 +43,7 @@ func Unpack(file, dir string) error {
output := strings.TrimSuffix(filepath.Base(file), ".lz4")
output = strings.TrimSuffix(output, ".LZ4")

path, err := securejoin.SecureJoin(dir, output)
path, err := utils.Join(dir, output)

if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions tar/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"path/filepath"
"strings"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/essentialkaos/npck/utils"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -85,7 +85,7 @@ func Read(r io.Reader, dir string) error {
return fmt.Errorf("Path \"%s\" contains directory traversal element and cannot be used", header.Name)
}

path, err := securejoin.SecureJoin(dir, header.Name)
path, err := utils.Join(dir, header.Name)

if err != nil {
return err
Expand Down Expand Up @@ -203,7 +203,7 @@ func isExternalLink(path, dir string) bool {
return true
}

realPath, err := securejoin.SecureJoin(dir, path)
realPath, err := utils.Join(dir, path)

if err != nil {
return true
Expand Down
57 changes: 57 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Package utils provides auxiliary methods for working with archives
package utils

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"
"path/filepath"
"strings"
"syscall"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// Join joins all elements of path, makes lexical processing, and evaluating all symlinks.
// Method returns error if final destination is not a child path of root.
func Join(root string, elem ...string) (string, error) {
result, err := filepath.EvalSymlinks(root)

if err != nil {
result = root
} else {
root = result
}

for _, e := range elem {
result = filepath.Clean(result + "/" + e)

if isLink(result) {
result, err = filepath.EvalSymlinks(result)

if err != nil {
return "", fmt.Errorf("Can't eval symlinks: %w", err)
}
}
}

if !strings.HasPrefix(result, root) {
return "", fmt.Errorf("Final destination (%s) is outside root (%s)", result, root)
}

return result, nil
}

// ////////////////////////////////////////////////////////////////////////////////// //

func isLink(path string) bool {
var buf = make([]byte, 1)
_, err := syscall.Readlink(path, buf)

return err == nil
}
62 changes: 62 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package utils

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"os"
"testing"

. "github.com/essentialkaos/check"
)

// ////////////////////////////////////////////////////////////////////////////////// //

func Test(t *testing.T) { TestingT(t) }

// ////////////////////////////////////////////////////////////////////////////////// //

type UtilsSuite struct{}

var _ = Suite(&UtilsSuite{})

// ////////////////////////////////////////////////////////////////////////////////// //

func (s *UtilsSuite) TestJoin(c *C) {
p, err := Join("/test", "myapp")
c.Assert(err, IsNil)
c.Assert(p, Equals, "/test/myapp")

p, err = Join("/test", "myapp/config/../global.cfg")
c.Assert(err, IsNil)
c.Assert(p, Equals, "/test/myapp/global.cfg")

p, err = Join("/unknown", "myapp/config/../global.cfg")
c.Assert(err, IsNil)
c.Assert(p, Equals, "/unknown/myapp/global.cfg")

tmpDir := c.MkDir()
os.Mkdir(tmpDir+"/test", 0755)
os.Symlink(tmpDir+"/test", tmpDir+"/testlink")
testDir := tmpDir + "/testlink"

os.Symlink(testDir+"/test.log", testDir+"/test1.link")
os.WriteFile(testDir+"/test.log", []byte("\n"), 0644)
os.Symlink(testDir+"/test.log", testDir+"/test1.link")
os.Symlink("/etc", testDir+"/test2.link")
os.Symlink(testDir+"/test3.link", testDir+"/test3.link")

p, err = Join(testDir, "mytest/../test1.link")
c.Assert(err, IsNil)
c.Assert(p, Matches, "*/test/test.log")

p, err = Join(testDir, "mytest/../test2.link")
c.Assert(err, NotNil)

p, err = Join(testDir, "mytest/../test3.link")
c.Assert(err, NotNil)
}
4 changes: 2 additions & 2 deletions xz/xz.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/ulikunitz/xz"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/essentialkaos/npck/utils"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -43,7 +43,7 @@ func Unpack(file, dir string) error {
output := strings.TrimSuffix(filepath.Base(file), ".xz")
output = strings.TrimSuffix(output, ".XZ")

path, err := securejoin.SecureJoin(dir, output)
path, err := utils.Join(dir, output)

if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/klauspost/compress/zip"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/essentialkaos/npck/utils"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -66,7 +66,7 @@ func Read(r io.ReaderAt, dir string) error {
}

info := header.FileInfo()
path, err := securejoin.SecureJoin(dir, header.Name)
path, err := utils.Join(dir, header.Name)

if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions zst/zst.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/klauspost/compress/zstd"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/essentialkaos/npck/utils"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -43,7 +43,7 @@ func Unpack(file, dir string) error {
output := strings.TrimSuffix(filepath.Base(file), ".zst")
output = strings.TrimSuffix(output, ".ZST")

path, err := securejoin.SecureJoin(dir, output)
path, err := utils.Join(dir, output)

if err != nil {
return err
Expand Down

0 comments on commit f0e557b

Please sign in to comment.