Skip to content

Commit f5b97af

Browse files
Fix replace directives by using archiver/zip (#1218)
* Fix replace directives by using archiver/zip * Fix unZip function * Move unzip logic to utils * Fix lint * Rename files and handled error * Update go-version --------- Co-authored-by: Rajat Bajaj <duedares@gmail.com>
1 parent 9291efb commit f5b97af

5 files changed

Lines changed: 174 additions & 51 deletions

File tree

go.mod

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ module github.com/auth0/auth0-cli
22

33
go 1.24.0
44

5-
// TODO: This could be removed after https://github.com/mholt/archiver/pull/396 merged
6-
replace github.com/mholt/archiver/v3 => github.com/anchore/archiver/v3 v3.5.2
7-
85
require (
96
github.com/AlecAivazis/survey/v2 v2.3.7
107
github.com/PuerkitoBio/rehttp v1.4.0
118
github.com/atotto/clipboard v0.1.4
12-
github.com/auth0/go-auth0 v1.20.1-0.20250514112557-200204cf984b
9+
github.com/auth0/go-auth0 v1.21.0
1310
github.com/briandowns/spinner v1.23.2
1411
github.com/charmbracelet/glamour v0.10.0
1512
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
@@ -28,7 +25,6 @@ require (
2825
github.com/manifoldco/promptui v0.9.0
2926
github.com/mattn/go-isatty v0.0.20
3027
github.com/mattn/go-tty v0.0.7
31-
github.com/mholt/archiver/v3 v3.5.1
3228
github.com/olekukonko/tablewriter v0.0.5
3329
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2
3430
github.com/pkg/errors v0.9.1
@@ -52,7 +48,6 @@ require (
5248
al.essio.dev/pkg/shellescape v1.5.1 // indirect
5349
github.com/ProtonMail/go-crypto v1.1.6 // indirect
5450
github.com/alecthomas/chroma/v2 v2.14.0 // indirect
55-
github.com/andybalholm/brotli v1.1.0 // indirect
5651
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
5752
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
5853
github.com/aymerick/douceur v0.2.0 // indirect
@@ -67,18 +62,14 @@ require (
6762
github.com/davecgh/go-spew v1.1.1 // indirect
6863
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
6964
github.com/dlclark/regexp2 v1.11.0 // indirect
70-
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
7165
github.com/fatih/color v1.16.0 // indirect
7266
github.com/goccy/go-json v0.10.5 // indirect
7367
github.com/godbus/dbus/v5 v5.1.0 // indirect
74-
github.com/golang/snappy v0.0.4 // indirect
7568
github.com/gorilla/css v1.0.1 // indirect
7669
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
7770
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
7871
github.com/hashicorp/terraform-json v0.24.0 // indirect
7972
github.com/inconshreveable/mousetrap v1.1.0 // indirect
80-
github.com/klauspost/compress v1.17.7 // indirect
81-
github.com/klauspost/pgzip v1.2.5 // indirect
8273
github.com/kr/text v0.2.0 // indirect
8374
github.com/lestrrat-go/blackmagic v1.0.3 // indirect
8475
github.com/lestrrat-go/httpcc v1.0.1 // indirect
@@ -95,13 +86,9 @@ require (
9586
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
9687
github.com/muesli/reflow v0.3.0 // indirect
9788
github.com/muesli/termenv v0.16.0 // indirect
98-
github.com/nwaples/rardecode v1.1.0 // indirect
99-
github.com/pierrec/lz4/v4 v4.1.3 // indirect
10089
github.com/pmezard/go-difflib v1.0.0 // indirect
10190
github.com/rivo/uniseg v0.4.7 // indirect
10291
github.com/segmentio/asm v1.2.0 // indirect
103-
github.com/ulikunitz/xz v0.5.10 // indirect
104-
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
10592
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
10693
github.com/yuin/goldmark v1.7.8 // indirect
10794
github.com/yuin/goldmark-emoji v1.0.5 // indirect

go.sum

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@ github.com/alecthomas/chroma/v2 v2.14.0 h1:R3+wzpnUArGcQz7fCETQBzO5n9IMNi13iIs46
1818
github.com/alecthomas/chroma/v2 v2.14.0/go.mod h1:QolEbTfmUHIMVpBqxeDnNBj2uoeI4EbYP4i6n68SG4I=
1919
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
2020
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
21-
github.com/anchore/archiver/v3 v3.5.2 h1:Bjemm2NzuRhmHy3m0lRe5tNoClB9A4zYyDV58PaB6aA=
22-
github.com/anchore/archiver/v3 v3.5.2/go.mod h1:e3dqJ7H78uzsRSEACH1joayhuSyhnonssnDhppzS1L4=
23-
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
24-
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
25-
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
2621
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
2722
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
2823
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
2924
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
30-
github.com/auth0/go-auth0 v1.20.1-0.20250514112557-200204cf984b h1:G4wYJ00EHV6Cu89/rafMKz8FdHN/asnD/REaYINIHWk=
31-
github.com/auth0/go-auth0 v1.20.1-0.20250514112557-200204cf984b/go.mod h1:g9S/4ImupKFx1gSLqeQO0v1yV91Oo5J5bYobLCAL+J4=
25+
github.com/auth0/go-auth0 v1.21.0 h1:ua0RNXvV8aMWnraF5uYZ+c5P53ePLR0ieYsa/E0SvJw=
26+
github.com/auth0/go-auth0 v1.21.0/go.mod h1:g9S/4ImupKFx1gSLqeQO0v1yV91Oo5J5bYobLCAL+J4=
3227
github.com/aybabtme/iocontrol v0.0.0-20150809002002-ad15bcfc95a0 h1:0NmehRCgyk5rljDQLKUO+cRJCnduDyn11+zGZIc9Z48=
3328
github.com/aybabtme/iocontrol v0.0.0-20150809002002-ad15bcfc95a0/go.mod h1:6L7zgvqo0idzI7IO8de6ZC051AfXb5ipkIJ7bIA2tGA=
3429
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
@@ -82,9 +77,6 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 h1:NMZiJj8QnKe1LgsbDayM4UoHwbvw
8277
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0/go.mod h1:ZXNYxsqcloTdSy/rNShjYzMhyjf0LaoftYK0p+A3h40=
8378
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
8479
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
85-
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
86-
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
87-
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
8880
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
8981
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
9082
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
@@ -110,10 +102,6 @@ github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8J
110102
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
111103
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
112104
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
113-
github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
114-
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
115-
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
116-
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
117105
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
118106
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
119107
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
@@ -152,13 +140,6 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNU
152140
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
153141
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
154142
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
155-
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
156-
github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
157-
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
158-
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
159-
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
160-
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
161-
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
162143
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
163144
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
164145
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -209,13 +190,8 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
209190
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
210191
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
211192
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
212-
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ=
213-
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
214193
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
215194
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
216-
github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
217-
github.com/pierrec/lz4/v4 v4.1.3 h1:/dvQpkb0o1pVlSgKNQqfkavlnXaIK+hJ0LXsKRUN9D4=
218-
github.com/pierrec/lz4/v4 v4.1.3/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
219195
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
220196
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
221197
github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4=
@@ -255,14 +231,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
255231
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
256232
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
257233
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
258-
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
259-
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
260-
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
261-
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
262234
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
263235
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
264-
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
265-
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
266236
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
267237
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
268238
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
@@ -339,7 +309,6 @@ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJ
339309
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
340310
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
341311
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
342-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
343312
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
344313
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
345314
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=

internal/auth0/quickstart.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
"path"
1212

1313
"github.com/auth0/go-auth0/management"
14-
"github.com/mholt/archiver/v3"
14+
15+
"github.com/auth0/auth0-cli/internal/utils"
1516
)
1617

1718
const (
@@ -80,18 +81,22 @@ func (q Quickstart) Download(ctx context.Context, downloadPath string, client *m
8081
return err
8182
}
8283

83-
if err := tmpFile.Close(); err != nil {
84+
if err = tmpFile.Close(); err != nil {
8485
return err
8586
}
8687
defer func() {
8788
_ = os.Remove(tmpFile.Name())
8889
}()
8990

90-
if err := os.RemoveAll(downloadPath); err != nil {
91+
if err = os.RemoveAll(downloadPath); err != nil {
9192
return err
9293
}
9394

94-
return archiver.Unarchive(tmpFile.Name(), downloadPath)
95+
if err = utils.Unzip(tmpFile.Name(), downloadPath); err != nil {
96+
return fmt.Errorf("failed to unzip file: %w", err)
97+
}
98+
99+
return nil
95100
}
96101

97102
func GetQuickstarts(ctx context.Context) (Quickstarts, error) {

internal/utils/unzip.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"strings"
8+
9+
"archive/zip"
10+
"path/filepath"
11+
)
12+
13+
// Unzip extracts files from a zip archive to a specified destination directory.
14+
func Unzip(src, dest string) error {
15+
r, err := zip.OpenReader(src)
16+
if err != nil {
17+
return err
18+
}
19+
20+
defer func(r *zip.ReadCloser) {
21+
_ = r.Close()
22+
}(r)
23+
24+
for _, f := range r.File {
25+
filPath := filepath.Join(dest, f.Name)
26+
27+
relPath, err := filepath.Rel(dest, filPath)
28+
if err != nil || strings.Contains(relPath, ".."+string(os.PathSeparator)) {
29+
return fmt.Errorf("illegal file path: %s", filPath)
30+
}
31+
32+
if f.FileInfo().IsDir() {
33+
if err := os.MkdirAll(filPath, os.ModePerm); err != nil {
34+
return err
35+
}
36+
continue
37+
}
38+
39+
if err := os.MkdirAll(filepath.Dir(filPath), os.ModePerm); err != nil {
40+
return err
41+
}
42+
43+
inFile, err := f.Open()
44+
if err != nil {
45+
return err
46+
}
47+
48+
outFile, err := os.OpenFile(filPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
49+
if err != nil {
50+
_ = inFile.Close()
51+
return err
52+
}
53+
54+
_, err = io.Copy(outFile, inFile)
55+
_ = inFile.Close()
56+
_ = outFile.Close()
57+
58+
if err != nil {
59+
return err
60+
}
61+
}
62+
63+
return nil
64+
}

internal/utils/unzip_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package utils
2+
3+
import (
4+
"archive/zip"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func createZip(t *testing.T, zipPath string, files map[string]string) {
12+
out, err := os.Create(zipPath)
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
defer out.Close()
17+
18+
zipWriter := zip.NewWriter(out)
19+
defer zipWriter.Close()
20+
21+
for name, content := range files {
22+
w, err := zipWriter.Create(name)
23+
if err != nil {
24+
t.Fatal(err)
25+
}
26+
if _, err := w.Write([]byte(content)); err != nil {
27+
t.Fatal(err)
28+
}
29+
}
30+
}
31+
32+
func TestUnzip_Success(t *testing.T) {
33+
tmpDir := t.TempDir()
34+
zipPath := filepath.Join(tmpDir, "test.zip")
35+
destDir := filepath.Join(tmpDir, "out")
36+
37+
files := map[string]string{
38+
"file1.txt": "hello",
39+
"dir/file2.txt": "world",
40+
"dir/sub/file3.txt": "nested",
41+
}
42+
createZip(t, zipPath, files)
43+
44+
err := Unzip(zipPath, destDir)
45+
if err != nil {
46+
t.Fatalf("Unzip failed: %v", err)
47+
}
48+
49+
for name, content := range files {
50+
fullPath := filepath.Join(destDir, name)
51+
data, err := os.ReadFile(fullPath)
52+
if err != nil {
53+
t.Errorf("failed to read %s: %v", name, err)
54+
continue
55+
}
56+
if string(data) != content {
57+
t.Errorf("expected content %q in %s but got %q", content, name, string(data))
58+
}
59+
}
60+
}
61+
62+
func TestUnzip_IllegalPath(t *testing.T) {
63+
tmpDir := t.TempDir()
64+
zipPath := filepath.Join(tmpDir, "bad.zip")
65+
destDir := filepath.Join(tmpDir, "out")
66+
67+
out, err := os.Create(zipPath)
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
defer out.Close()
72+
73+
zipWriter := zip.NewWriter(out)
74+
_, err = zipWriter.Create("../evil.txt")
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
zipWriter.Close()
79+
80+
err = Unzip(zipPath, destDir)
81+
if err == nil || !strings.Contains(err.Error(), "illegal file path") {
82+
t.Fatalf("expected zip-slip error, got: %v", err)
83+
}
84+
}
85+
86+
func TestUnzip_InvalidFile(t *testing.T) {
87+
tmpDir := t.TempDir()
88+
invalidZip := filepath.Join(tmpDir, "not_a_zip.txt")
89+
90+
if err := os.WriteFile(invalidZip, []byte("invalid data"), 0644); err != nil {
91+
t.Fatal(err)
92+
}
93+
94+
err := Unzip(invalidZip, tmpDir)
95+
if err == nil {
96+
t.Fatal("expected error for invalid zip file")
97+
}
98+
}

0 commit comments

Comments
 (0)