From 63fc3a656a374776bfee350c74d8dd4af8a4870e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=8D=E1=B4=8F=E1=B4=8F=C9=B4D4=CA=80=E1=B4=8B?= Date: Sun, 14 Aug 2022 21:22:34 +0800 Subject: [PATCH] refactor: pass golang-ci lint check --- .golangci.yml | 6 ++- go.mod | 2 +- internal/browingdata/bookmark/bookmark.go | 8 ++-- internal/browingdata/browsingdata.go | 13 ++++-- internal/browingdata/cookie/cookie.go | 13 +++--- internal/browingdata/creditcard/creditcard.go | 9 ++-- internal/browingdata/download/download.go | 11 ++--- internal/browingdata/extension/extension.go | 4 +- internal/browingdata/history/history.go | 11 ++--- .../browingdata/localstorage/localstorage.go | 4 +- internal/browingdata/outputter.go | 7 ++-- internal/browingdata/outputter_test.go | 1 + internal/browingdata/password/password.go | 30 +++++++------- internal/browser/browser.go | 5 +-- internal/browser/browser_windows.go | 5 +++ internal/browser/chromium/chromium.go | 2 +- internal/browser/chromium/chromium_darwin.go | 3 +- internal/browser/chromium/chromium_windows.go | 18 ++++---- internal/browser/firefox/firefox.go | 3 +- internal/decrypter/decrypter.go | 1 - internal/utils/fileutil/filetutil.go | 41 +++++++++---------- internal/utils/typeutil/typeutil_test.go | 14 ++++--- 22 files changed, 112 insertions(+), 99 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index bd2a97b..cea0bad 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,7 +11,6 @@ linters: - 'deadcode' - 'depguard' - 'dogsled' - - 'errcheck' - 'errorlint' - 'exportloopref' - 'gofmt' @@ -42,6 +41,7 @@ linters: - 'structcheck' - 'stylecheck' - 'unused' + - 'errcheck' issues: exclude-use-default: false @@ -52,7 +52,9 @@ issues: - G101 # G103: Use of unsafe calls should be audited - G103 - # G404, G401, G502, G505 weak cryptographic list + # G304: Potential file inclusion via variable + - G304 + # G404, G401, G502, G505: weak cryptographic list - G401 - G404 - G502 diff --git a/go.mod b/go.mod index f2418f0..eb0f7de 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module hack-browser-data -go 1.18 +go 1.19 require ( github.com/gocarina/gocsv v0.0.0-20211203214250-4735fba0c1d9 diff --git a/internal/browingdata/bookmark/bookmark.go b/internal/browingdata/bookmark/bookmark.go index 2e45535..0dcd03f 100644 --- a/internal/browingdata/bookmark/bookmark.go +++ b/internal/browingdata/bookmark/bookmark.go @@ -6,14 +6,14 @@ import ( "sort" "time" - "github.com/tidwall/gjson" - "hack-browser-data/internal/item" "hack-browser-data/internal/log" "hack-browser-data/internal/utils/fileutil" "hack-browser-data/internal/utils/typeutil" + // import sqlite3 driver _ "github.com/mattn/go-sqlite3" + "github.com/tidwall/gjson" ) type ChromiumBookmark []bookmark @@ -51,7 +51,7 @@ func getBookmarkChildren(value gjson.Result, w *ChromiumBookmark) (children gjso const ( bookmarkID = "id" bookmarkAdded = "date_added" - bookmarkUrl = "url" + bookmarkURL = "url" bookmarkName = "name" bookmarkType = "type" bookmarkChildren = "children" @@ -60,7 +60,7 @@ func getBookmarkChildren(value gjson.Result, w *ChromiumBookmark) (children gjso bm := bookmark{ ID: value.Get(bookmarkID).Int(), Name: value.Get(bookmarkName).String(), - URL: value.Get(bookmarkUrl).String(), + URL: value.Get(bookmarkURL).String(), DateAdded: typeutil.TimeEpoch(value.Get(bookmarkAdded).Int()), } children = value.Get(bookmarkChildren) diff --git a/internal/browingdata/browsingdata.go b/internal/browingdata/browsingdata.go index b8aa751..7e1c275 100644 --- a/internal/browingdata/browsingdata.go +++ b/internal/browingdata/browsingdata.go @@ -53,17 +53,22 @@ func (d *Data) Output(dir, browserName, flag string) { // if the length of the export data is 0, then it is not necessary to output continue } - filename := fileutil.Filename(browserName, source.Name(), output.Ext()) + filename := fileutil.ItemName(browserName, source.Name(), output.Ext()) f, err := output.CreateFile(dir, filename) if err != nil { - log.Errorf("create file error %s", err) + log.Errorf("create file %s error %s", filename, err.Error()) + continue } if err := output.Write(source, f); err != nil { - log.Errorf("%s write to file %s error %s", source.Name(), filename, err.Error()) + log.Errorf("write to file %s error %s", filename, err.Error()) + continue + } + if err := f.Close(); err != nil { + log.Errorf("close file %s error %s", filename, err.Error()) + continue } log.Noticef("output to file %s success", path.Join(dir, filename)) - f.Close() } } diff --git a/internal/browingdata/cookie/cookie.go b/internal/browingdata/cookie/cookie.go index 14cae53..7f0e4f4 100644 --- a/internal/browingdata/cookie/cookie.go +++ b/internal/browingdata/cookie/cookie.go @@ -6,12 +6,13 @@ import ( "sort" "time" - _ "github.com/mattn/go-sqlite3" - "hack-browser-data/internal/decrypter" "hack-browser-data/internal/item" "hack-browser-data/internal/log" "hack-browser-data/internal/utils/typeutil" + + // import sqlite3 driver + _ "github.com/mattn/go-sqlite3" ) type ChromiumCookie []cookie @@ -72,7 +73,7 @@ func (c *ChromiumCookie) Parse(masterKey []byte) error { if len(encryptValue) > 0 { var err error if masterKey == nil { - value, err = decrypter.DPApi(encryptValue) + value, err = decrypter.DPAPI(encryptValue) } else { value, err = decrypter.Chromium(masterKey, encryptValue) } @@ -118,10 +119,10 @@ func (f *FirefoxCookie) Parse(masterKey []byte) error { for rows.Next() { var ( name, value, host, path string - isSecure, isHttpOnly int + isSecure, isHTTPOnly int creationTime, expiry int64 ) - if err = rows.Scan(&name, &value, &host, &path, &creationTime, &expiry, &isSecure, &isHttpOnly); err != nil { + if err = rows.Scan(&name, &value, &host, &path, &creationTime, &expiry, &isSecure, &isHTTPOnly); err != nil { log.Warn(err) } *f = append(*f, cookie{ @@ -129,7 +130,7 @@ func (f *FirefoxCookie) Parse(masterKey []byte) error { Host: host, Path: path, IsSecure: typeutil.IntToBool(isSecure), - IsHTTPOnly: typeutil.IntToBool(isHttpOnly), + IsHTTPOnly: typeutil.IntToBool(isHTTPOnly), CreateDate: typeutil.TimeStamp(creationTime / 1000000), ExpireDate: typeutil.TimeStamp(expiry), Value: value, diff --git a/internal/browingdata/creditcard/creditcard.go b/internal/browingdata/creditcard/creditcard.go index 781fe90..b77d527 100644 --- a/internal/browingdata/creditcard/creditcard.go +++ b/internal/browingdata/creditcard/creditcard.go @@ -4,11 +4,12 @@ import ( "database/sql" "os" - _ "github.com/mattn/go-sqlite3" - "hack-browser-data/internal/decrypter" "hack-browser-data/internal/item" "hack-browser-data/internal/log" + + // import sqlite3 driver + _ "github.com/mattn/go-sqlite3" ) type ChromiumCreditCard []card @@ -56,7 +57,7 @@ func (c *ChromiumCreditCard) Parse(masterKey []byte) error { NickName: nickname, } if masterKey == nil { - value, err = decrypter.DPApi(encryptValue) + value, err = decrypter.DPAPI(encryptValue) if err != nil { return err } @@ -112,7 +113,7 @@ func (c *YandexCreditCard) Parse(masterKey []byte) error { NickName: nickname, } if masterKey == nil { - value, err = decrypter.DPApi(encryptValue) + value, err = decrypter.DPAPI(encryptValue) if err != nil { return err } diff --git a/internal/browingdata/download/download.go b/internal/browingdata/download/download.go index 55bcc2d..a74d242 100644 --- a/internal/browingdata/download/download.go +++ b/internal/browingdata/download/download.go @@ -11,6 +11,7 @@ import ( "hack-browser-data/internal/log" "hack-browser-data/internal/utils/typeutil" + // import sqlite3 driver _ "github.com/mattn/go-sqlite3" "github.com/tidwall/gjson" ) @@ -19,7 +20,7 @@ type ChromiumDownload []download type download struct { TargetPath string - Url string + URL string TotalBytes int64 StartTime time.Time EndTime time.Time @@ -44,15 +45,15 @@ func (c *ChromiumDownload) Parse(masterKey []byte) error { defer rows.Close() for rows.Next() { var ( - targetPath, tabUrl, mimeType string + targetPath, tabURL, mimeType string totalBytes, startTime, endTime int64 ) - if err := rows.Scan(&targetPath, &tabUrl, &totalBytes, &startTime, &endTime, &mimeType); err != nil { + if err := rows.Scan(&targetPath, &tabURL, &totalBytes, &startTime, &endTime, &mimeType); err != nil { log.Warn(err) } data := download{ TargetPath: targetPath, - Url: tabUrl, + URL: tabURL, TotalBytes: totalBytes, StartTime: typeutil.TimeEpoch(startTime), EndTime: typeutil.TimeEpoch(endTime), @@ -119,7 +120,7 @@ func (f *FirefoxDownload) Parse(masterKey []byte) error { fileSize := gjson.Get(json, "fileSize") *f = append(*f, download{ TargetPath: path, - Url: url, + URL: url, TotalBytes: fileSize.Int(), StartTime: typeutil.TimeStamp(dateAdded / 1000000), EndTime: typeutil.TimeStamp(endTime.Int() / 1000), diff --git a/internal/browingdata/extension/extension.go b/internal/browingdata/extension/extension.go index 4758ce2..9a1bd01 100644 --- a/internal/browingdata/extension/extension.go +++ b/internal/browingdata/extension/extension.go @@ -3,11 +3,11 @@ package extension import ( "os" - "github.com/tidwall/gjson" - "hack-browser-data/internal/item" "hack-browser-data/internal/log" "hack-browser-data/internal/utils/fileutil" + + "github.com/tidwall/gjson" ) type ChromiumExtension []*extension diff --git a/internal/browingdata/history/history.go b/internal/browingdata/history/history.go index 2b67e58..25c0df2 100644 --- a/internal/browingdata/history/history.go +++ b/internal/browingdata/history/history.go @@ -6,18 +6,19 @@ import ( "sort" "time" - _ "github.com/mattn/go-sqlite3" - "hack-browser-data/internal/item" "hack-browser-data/internal/log" "hack-browser-data/internal/utils/typeutil" + + // import sqlite3 driver + _ "github.com/mattn/go-sqlite3" ) type ChromiumHistory []history type history struct { Title string - Url string + URL string VisitCount int LastVisitTime time.Time } @@ -48,7 +49,7 @@ func (c *ChromiumHistory) Parse(masterKey []byte) error { log.Warn(err) } data := history{ - Url: url, + URL: url, Title: title, VisitCount: visitCount, LastVisitTime: typeutil.TimeEpoch(lastVisitTime), @@ -109,7 +110,7 @@ func (f *FirefoxHistory) Parse(masterKey []byte) error { } *f = append(*f, history{ Title: title, - Url: url, + URL: url, VisitCount: visitCount, LastVisitTime: typeutil.TimeStamp(visitDate / 1000000), }) diff --git a/internal/browingdata/localstorage/localstorage.go b/internal/browingdata/localstorage/localstorage.go index ccd7340..8999c2c 100644 --- a/internal/browingdata/localstorage/localstorage.go +++ b/internal/browingdata/localstorage/localstorage.go @@ -7,11 +7,11 @@ import ( "os" "strings" - "github.com/syndtr/goleveldb/leveldb" - "hack-browser-data/internal/item" "hack-browser-data/internal/log" "hack-browser-data/internal/utils/typeutil" + + "github.com/syndtr/goleveldb/leveldb" ) type ChromiumLocalStorage []storage diff --git a/internal/browingdata/outputter.go b/internal/browingdata/outputter.go index 4b2c43c..1e9a3d0 100644 --- a/internal/browingdata/outputter.go +++ b/internal/browingdata/outputter.go @@ -52,7 +52,7 @@ func (o *OutPutter) CreateFile(dir, filename string) (*os.File, error) { if dir != "" { if _, err := os.Stat(dir); os.IsNotExist(err) { - err := os.MkdirAll(dir, 0o777) + err := os.MkdirAll(dir, 0o750) if err != nil { return nil, err } @@ -62,7 +62,7 @@ func (o *OutPutter) CreateFile(dir, filename string) (*os.File, error) { var file *os.File var err error p := filepath.Join(dir, filename) - file, err = os.OpenFile(p, os.O_TRUNC|os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o666) + file, err = os.OpenFile(filepath.Clean(p), os.O_TRUNC|os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600) if err != nil { return nil, err } @@ -72,7 +72,6 @@ func (o *OutPutter) CreateFile(dir, filename string) (*os.File, error) { func (o *OutPutter) Ext() string { if o.json { return "json" - } else { - return "csv" } + return "csv" } diff --git a/internal/browingdata/outputter_test.go b/internal/browingdata/outputter_test.go index 83f8361..ca3c6ab 100644 --- a/internal/browingdata/outputter_test.go +++ b/internal/browingdata/outputter_test.go @@ -6,6 +6,7 @@ import ( ) func TestNewOutPutter(t *testing.T) { + t.Parallel() out := NewOutPutter("json") if out == nil { t.Error("New() returned nil") diff --git a/internal/browingdata/password/password.go b/internal/browingdata/password/password.go index 77ea409..d6ddd06 100644 --- a/internal/browingdata/password/password.go +++ b/internal/browingdata/password/password.go @@ -4,18 +4,18 @@ import ( "bytes" "database/sql" "encoding/base64" - "io/ioutil" "os" "sort" "time" - _ "github.com/mattn/go-sqlite3" - "github.com/tidwall/gjson" - "hack-browser-data/internal/decrypter" "hack-browser-data/internal/item" "hack-browser-data/internal/log" "hack-browser-data/internal/utils/typeutil" + + // import sqlite3 driver + _ "github.com/mattn/go-sqlite3" + "github.com/tidwall/gjson" ) type ChromiumPassword []loginData @@ -25,7 +25,7 @@ type loginData struct { encryptPass []byte encryptUser []byte Password string - LoginUrl string + LoginURL string CreateDate time.Time } @@ -58,12 +58,12 @@ func (c *ChromiumPassword) Parse(masterKey []byte) error { login := loginData{ UserName: username, encryptPass: pwd, - LoginUrl: url, + LoginURL: url, } if len(pwd) > 0 { var err error if masterKey == nil { - password, err = decrypter.DPApi(pwd) + password, err = decrypter.DPAPI(pwd) } else { password, err = decrypter.Chromium(masterKey, pwd) } @@ -125,13 +125,13 @@ func (c *YandexPassword) Parse(masterKey []byte) error { login := loginData{ UserName: username, encryptPass: pwd, - LoginUrl: url, + LoginURL: url, } if len(pwd) > 0 { var err error if masterKey == nil { - password, err = decrypter.DPApi(pwd) + password, err = decrypter.DPAPI(pwd) } else { password, err = decrypter.Chromium(masterKey, pwd) } @@ -196,7 +196,7 @@ func (f *FirefoxPassword) Parse(masterKey []byte) error { if err != nil { return err } - allLogin, err := getFirefoxLoginData(item.TempFirefoxPassword) + allLogin, err := getFirefoxLoginData() if err != nil { return err } @@ -218,7 +218,7 @@ func (f *FirefoxPassword) Parse(masterKey []byte) error { return err } *f = append(*f, loginData{ - LoginUrl: v.LoginUrl, + LoginURL: v.LoginURL, UserName: string(user), Password: string(pwd), CreateDate: v.CreateDate, @@ -251,12 +251,12 @@ func getFirefoxDecryptKey(key4file string) (item1, item2, a11, a102 []byte, err return item1, item2, a11, a102, nil } -func getFirefoxLoginData(loginJson string) (l []loginData, err error) { - s, err := ioutil.ReadFile(loginJson) +func getFirefoxLoginData() (l []loginData, err error) { + s, err := os.ReadFile(item.TempFirefoxPassword) if err != nil { return nil, err } - defer os.Remove(loginJson) + defer os.Remove(item.TempFirefoxPassword) h := gjson.GetBytes(s, "logins") if h.Exists() { for _, v := range h.Array() { @@ -265,7 +265,7 @@ func getFirefoxLoginData(loginJson string) (l []loginData, err error) { user []byte pass []byte ) - m.LoginUrl = v.Get("formSubmitURL").String() + m.LoginURL = v.Get("formSubmitURL").String() user, err = base64.StdEncoding.DecodeString(v.Get("encryptedUsername").String()) if err != nil { return nil, err diff --git a/internal/browser/browser.go b/internal/browser/browser.go index bec0c37..47f8e79 100644 --- a/internal/browser/browser.go +++ b/internal/browser/browser.go @@ -119,14 +119,11 @@ const ( chromeBetaName = "Chrome Beta" chromiumName = "Chromium" edgeName = "Microsoft Edge" - speed360Name = "360speed" - qqBrowserName = "QQ" braveName = "Brave" operaName = "Opera" operaGXName = "OperaGX" vivaldiName = "Vivaldi" coccocName = "CocCoc" yandexName = "Yandex" - - firefoxName = "Firefox" + firefoxName = "Firefox" ) diff --git a/internal/browser/browser_windows.go b/internal/browser/browser_windows.go index 17fa899..5d0ebbc 100644 --- a/internal/browser/browser_windows.go +++ b/internal/browser/browser_windows.go @@ -6,6 +6,11 @@ import ( "hack-browser-data/internal/item" ) +const ( + speed360Name = "360speed" + qqBrowserName = "QQ" +) + var ( chromiumList = map[string]struct { name string diff --git a/internal/browser/chromium/chromium.go b/internal/browser/chromium/chromium.go index dd0d246..7f716e6 100644 --- a/internal/browser/chromium/chromium.go +++ b/internal/browser/chromium/chromium.go @@ -32,7 +32,7 @@ func New(name, storage, profilePath string, items []item.Item) ([]*chromium, err if err != nil { return nil, err } - var chromiumList []*chromium + chromiumList := make([]*chromium, 0, len(multiItemPaths)) for user, itemPaths := range multiItemPaths { chromiumList = append(chromiumList, &chromium{ name: fileutil.BrowserName(name, user), diff --git a/internal/browser/chromium/chromium_darwin.go b/internal/browser/chromium/chromium_darwin.go index ab5e6e2..e905d64 100644 --- a/internal/browser/chromium/chromium_darwin.go +++ b/internal/browser/chromium/chromium_darwin.go @@ -28,10 +28,9 @@ func (c *chromium) GetMasterKey() ([]byte, error) { ) // don't need chromium key file for macOS defer os.Remove(item.TempChromiumKey) - // defer os.Remove(item.TempChromiumKey) // Get the master key from the keychain // $ security find-generic-password -wa 'Chrome' - cmd = exec.Command("security", "find-generic-password", "-wa", c.storage) + cmd = exec.Command("security", "find-generic-password", "-wa", strings.TrimSpace(c.storage)) //nolint:gosec cmd.Stdout = &stdout cmd.Stderr = &stderr err := cmd.Run() diff --git a/internal/browser/chromium/chromium_windows.go b/internal/browser/chromium/chromium_windows.go index 0cd6846..a54b0e7 100644 --- a/internal/browser/chromium/chromium_windows.go +++ b/internal/browser/chromium/chromium_windows.go @@ -24,14 +24,14 @@ func (c *chromium) GetMasterKey() ([]byte, error) { } defer os.Remove(keyFile) encryptedKey := gjson.Get(keyFile, "os_crypt.encrypted_key") - if encryptedKey.Exists() { - pureKey, err := base64.StdEncoding.DecodeString(encryptedKey.String()) - if err != nil { - return nil, errDecodeMasterKeyFailed - } - c.masterKey, err = decrypter.DPApi(pureKey[5:]) - log.Infof("%s initialized master key success", c.name) - return c.masterKey, err + if !encryptedKey.Exists() { + return nil, nil } - return nil, nil + pureKey, err := base64.StdEncoding.DecodeString(encryptedKey.String()) + if err != nil { + return nil, errDecodeMasterKeyFailed + } + c.masterKey, err = decrypter.DPAPI(pureKey[5:]) + log.Infof("%s initialized master key success", c.name) + return c.masterKey, err } diff --git a/internal/browser/firefox/firefox.go b/internal/browser/firefox/firefox.go index 69441dc..fc0dcbd 100644 --- a/internal/browser/firefox/firefox.go +++ b/internal/browser/firefox/firefox.go @@ -35,7 +35,8 @@ func New(name, storage, profilePath string, items []item.Item) ([]*firefox, erro if err != nil { return nil, err } - var firefoxList []*firefox + + firefoxList := make([]*firefox, 0, len(multiItemPaths)) for name, itemPaths := range multiItemPaths { firefoxList = append(firefoxList, &firefox{ name: fmt.Sprintf("firefox-%s", name), diff --git a/internal/decrypter/decrypter.go b/internal/decrypter/decrypter.go index 4248afc..dbce6fe 100644 --- a/internal/decrypter/decrypter.go +++ b/internal/decrypter/decrypter.go @@ -16,7 +16,6 @@ import ( var ( errSecurityKeyIsEmpty = errors.New("input [security find-generic-password -wa 'Chrome'] in terminal") errPasswordIsEmpty = errors.New("password is empty") - errDecryptFailed = errors.New("decrypt encrypted value failed") errDecodeASN1Failed = errors.New("decode ASN1 data failed") errEncryptedLength = errors.New("length of encrypted password less than block size") ) diff --git a/internal/utils/fileutil/filetutil.go b/internal/utils/fileutil/filetutil.go index 801557e..1a94ce5 100644 --- a/internal/utils/fileutil/filetutil.go +++ b/internal/utils/fileutil/filetutil.go @@ -5,7 +5,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -55,7 +54,7 @@ func FilesInFolder(dir, filename string) ([]string, error) { // ReadFile reads the file from the provided path func ReadFile(filename string) (string, error) { - s, err := ioutil.ReadFile(filename) + s, err := os.ReadFile(filename) return string(s), err } @@ -71,20 +70,20 @@ func CopyDir(src, dst, skip string) error { // CopyDirHasSuffix copies the directory from the source to the destination // contain is the file if you want to copy, and rename copied filename with dir/index_filename func CopyDirHasSuffix(src, dst, suffix string) error { - var filelist []string + var files []string err := filepath.Walk(src, func(path string, f os.FileInfo, err error) error { if !f.IsDir() && strings.HasSuffix(strings.ToLower(f.Name()), suffix) { - filelist = append(filelist, path) + files = append(files, path) } return err }) if err != nil { return err } - if err := os.MkdirAll(dst, 0o755); err != nil { + if err := os.MkdirAll(dst, 0o700); err != nil { return err } - for index, file := range filelist { + for index, file := range files { // p = dir/index_file p := fmt.Sprintf("%s/%d_%s", dst, index, BaseDir(file)) err = CopyFile(file, p) @@ -97,20 +96,19 @@ func CopyDirHasSuffix(src, dst, suffix string) error { // CopyFile copies the file from the source to the destination func CopyFile(src, dst string) error { - // TODO: Handle read file error - d, err := ioutil.ReadFile(src) + s, err := os.ReadFile(src) if err != nil { return err } - err = ioutil.WriteFile(dst, d, 0o777) + err = os.WriteFile(dst, s, 0o600) if err != nil { return err } return nil } -// Filename returns the filename from the provided path -func Filename(browser, item, ext string) string { +// ItemName returns the filename from the provided path +func ItemName(browser, item, ext string) string { replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_") return strings.ToLower(fmt.Sprintf("%s_%s.%s", replace.Replace(browser), item, ext)) } @@ -137,26 +135,27 @@ func ParentBaseDir(p string) string { // CompressDir compresses the directory into a zip file func CompressDir(dir string) error { - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { return err } b := new(bytes.Buffer) zw := zip.NewWriter(b) for _, f := range files { - fw, _ := zw.Create(f.Name()) - fileName := path.Join(dir, f.Name()) - fileContent, err := ioutil.ReadFile(fileName) + fw, err := zw.Create(f.Name()) + if err != nil { + return err + } + name := path.Join(dir, f.Name()) + content, err := os.ReadFile(name) if err != nil { - zw.Close() return err } - _, err = fw.Write(fileContent) + _, err = fw.Write(content) if err != nil { - zw.Close() return err } - err = os.Remove(fileName) + err = os.Remove(name) if err != nil { return err } @@ -165,7 +164,7 @@ func CompressDir(dir string) error { return err } filename := filepath.Join(dir, fmt.Sprintf("%s.zip", dir)) - outFile, err := os.Create(filename) + outFile, err := os.Create(filepath.Clean(filename)) if err != nil { return err } @@ -173,5 +172,5 @@ func CompressDir(dir string) error { if err != nil { return err } - return nil + return outFile.Close() } diff --git a/internal/utils/typeutil/typeutil_test.go b/internal/utils/typeutil/typeutil_test.go index 7d5dc28..68e0aeb 100644 --- a/internal/utils/typeutil/typeutil_test.go +++ b/internal/utils/typeutil/typeutil_test.go @@ -4,13 +4,15 @@ import ( "testing" ) -var reverseTestCases = [][]any{ - {1, 2, 3, 4, 5}, - {"1", "2", "3", "4", "5"}, - {"1", 2, "3", "4", 5}, -} - func TestReverse(t *testing.T) { + t.Parallel() + + reverseTestCases := [][]any{ + {1, 2, 3, 4, 5}, + {"1", "2", "3", "4", "5"}, + {"1", 2, "3", "4", 5}, + } + for _, ts := range reverseTestCases { h := Reverse(ts) for i := 0; i < len(ts); i++ {