-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecrypt_test.go
More file actions
351 lines (300 loc) · 11.4 KB
/
Copy pathdecrypt_test.go
File metadata and controls
351 lines (300 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package main
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
)
// TestDecryptPackInner_NoContentsJSON verifies the upfront validation:
// pointing decrypt at a non-pack directory now returns errPackNoManifest,
// which humanize translates into "isn't a valid resource pack" instead
// of a bare "open: no such file" error.
func TestDecryptPackInner_NoContentsJSON(t *testing.T) {
empty := t.TempDir()
_, err := decryptPackInner(empty, testMasterKey, t.TempDir())
if err == nil {
t.Fatal("expected error from empty pack dir, got nil")
}
if !errors.Is(err, errPackNoManifest) {
t.Errorf("err = %v, want chain to include errPackNoManifest", err)
}
}
func TestDecryptContentsJSON(t *testing.T) {
contents := contentsFile{
Content: []contentsEntry{
{Path: "textures/block.png", Key: "KEYKEYKEYKEYKEYKEYKEYKEYKEYKEY32"},
{Path: "manifest.json", Key: ""},
},
}
payload, _ := json.Marshal(contents)
ciphertext := mustEncrypt(t, payload, []byte(testMasterKey))
header := make([]byte, contentsHeaderSize)
data := append(header, ciphertext...)
result, err := decryptContentsJSON(data, testMasterKey)
if err != nil {
t.Fatalf("decryptContentsJSON error: %v", err)
}
if len(result.Content) != 2 {
t.Fatalf("expected 2 entries, got %d", len(result.Content))
}
if result.Content[0].Path != "textures/block.png" {
t.Errorf("entry[0].Path = %q, want %q", result.Content[0].Path, "textures/block.png")
}
if result.Content[0].Key != "KEYKEYKEYKEYKEYKEYKEYKEYKEYKEY32" {
t.Errorf("entry[0].Key = %q", result.Content[0].Key)
}
}
func TestDecryptContentsJSON_TooSmall(t *testing.T) {
_, err := decryptContentsJSON(make([]byte, 100), testMasterKey)
if err == nil {
t.Error("expected error for data smaller than 256 bytes")
}
}
func TestDecryptContentsJSON_InvalidJSON(t *testing.T) {
garbage := mustEncrypt(t, []byte("not json {{{"), []byte(testMasterKey))
header := make([]byte, contentsHeaderSize)
data := append(header, garbage...)
_, err := decryptContentsJSON(data, testMasterKey)
if err == nil {
t.Error("expected error for invalid JSON")
}
}
// TestDecryptContentsJSON_WrongKey is the most common real-world failure:
// CFB8 is unauthenticated, so a wrong key yields garbage; the only guard is
// the JSON unmarshal, which must surface as errPackWrongKey.
func TestDecryptContentsJSON_WrongKey(t *testing.T) {
payload, _ := json.Marshal(contentsFile{Content: []contentsEntry{{Path: "a.txt"}}})
ciphertext := mustEncrypt(t, payload, []byte(testMasterKey))
data := append(make([]byte, contentsHeaderSize), ciphertext...)
_, err := decryptContentsJSON(data, "ZYXWVUTSRQPONMLKJIHGFEDCBA654321") // valid length, wrong key
if !errors.Is(err, errPackWrongKey) {
t.Fatalf("err = %v, want chain to include errPackWrongKey", err)
}
}
// TestDecryptContentsJSON_BadKeyLen: a wrong-length key (a truncated or typo'd
// paste) reports errPackBadKeyLen rather than errPackWrongKey, so the user is
// told the length is off instead of being pointed at the wrong pack's key.
func TestDecryptContentsJSON_BadKeyLen(t *testing.T) {
data := make([]byte, contentsHeaderSize+16)
_, err := decryptContentsJSON(data, "tooShort")
if !errors.Is(err, errPackBadKeyLen) {
t.Fatalf("err = %v, want chain to include errPackBadKeyLen", err)
}
}
// TestDecryptPackFiles_PathEscapeSkipped pins the zip-slip guard: a malicious
// contents.json entry pointing outside outDir must be skipped, not written.
func TestDecryptPackFiles_PathEscapeSkipped(t *testing.T) {
tmp := t.TempDir()
packDir := filepath.Join(tmp, "pack")
outDir := filepath.Join(tmp, "out")
os.MkdirAll(packDir, 0o755)
os.MkdirAll(outDir, 0o755)
os.WriteFile(filepath.Join(packDir, "safe.txt"), []byte("ok"), 0o644)
stats := decryptPackFiles(packDir, outDir, []contentsEntry{
{Path: "../escape.txt", Key: ""},
{Path: "safe.txt", Key: ""},
})
if _, err := os.Stat(filepath.Join(tmp, "escape.txt")); !os.IsNotExist(err) {
t.Fatalf("path-escaping entry was written outside outDir (err=%v)", err)
}
if _, err := os.Stat(filepath.Join(outDir, "safe.txt")); err != nil {
t.Fatalf("safe file should still be written: %v", err)
}
if stats.errors != 1 {
t.Errorf("escaping entry should count as 1 error, got %d", stats.errors)
}
}
// TestProcessFile_WrongKeyNoValidation documents the unauthenticated-cipher
// contract: a wrong per-file key writes garbage with no error. A future
// user-supplied-key feature would need to add validation - this is the tripwire.
func TestProcessFile_WrongKeyNoValidation(t *testing.T) {
tmp := t.TempDir()
src := filepath.Join(tmp, "src")
os.MkdirAll(src, 0o755)
plaintext := []byte("the original secret bytes here!!")
ciphertext := mustEncrypt(t, plaintext, []byte(testMasterKey))
os.WriteFile(filepath.Join(src, "data.bin"), ciphertext, 0o644)
entry := contentsEntry{Path: "data.bin", Key: "ZYXWVUTSRQPONMLKJIHGFEDCBA654321"}
dec, err := processFile(entry, filepath.Join(src, "data.bin"), filepath.Join(tmp, "out", "data.bin"))
if err != nil || !dec {
t.Fatalf("wrong key should still report decrypted with no error: dec=%v err=%v", dec, err)
}
got, _ := os.ReadFile(filepath.Join(tmp, "out", "data.bin"))
if len(got) == 0 || string(got) == string(plaintext) {
t.Errorf("expected non-empty garbage differing from plaintext, got %q", got)
}
}
func TestDecryptContentsJSON_TrailingNulls(t *testing.T) {
contents := contentsFile{Content: []contentsEntry{{Path: "a.txt", Key: ""}}}
payload, _ := json.Marshal(contents)
payload = append(payload, 0, 0, 0, '\n', ' ')
ciphertext := mustEncrypt(t, payload, []byte(testMasterKey))
header := make([]byte, contentsHeaderSize)
data := append(header, ciphertext...)
result, err := decryptContentsJSON(data, testMasterKey)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.Content) != 1 {
t.Errorf("expected 1 entry, got %d", len(result.Content))
}
}
func TestDecryptAll_DefaultOutBase(t *testing.T) {
cases := []struct {
cacheDir string
want string
}{
{".", "decrypted"},
{"./", "decrypted"},
{"/", "decrypted"},
{"./packs", "./packs_decrypted"},
{"./packs/", "./packs_decrypted"},
{"my_packs", "my_packs_decrypted"},
}
for _, tc := range cases {
t.Run(tc.cacheDir, func(t *testing.T) {
got := defaultDecryptOutBase(tc.cacheDir)
if got != tc.want {
t.Errorf("defaultDecryptOutBase(%q) = %q, want %q", tc.cacheDir, got, tc.want)
}
})
}
}
func TestProcessFile_CopyPlain(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
dstDir := filepath.Join(tmp, "dst")
os.MkdirAll(srcDir, 0755)
os.WriteFile(filepath.Join(srcDir, "readme.txt"), []byte("hello world"), 0644)
entry := contentsEntry{Path: "readme.txt", Key: ""}
decrypted, err := processFile(entry, filepath.Join(srcDir, "readme.txt"), filepath.Join(dstDir, "readme.txt"))
if err != nil {
t.Fatalf("processFile error: %v", err)
}
if decrypted {
t.Error("plain file should not be reported as decrypted")
}
got, _ := os.ReadFile(filepath.Join(dstDir, "readme.txt"))
if string(got) != "hello world" {
t.Errorf("got %q, want %q", got, "hello world")
}
}
func TestProcessFile_DecryptEncrypted(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
dstDir := filepath.Join(tmp, "dst")
os.MkdirAll(srcDir, 0755)
fileKey := "ZYXWVUTSRQPONMLKJIHGFEDCBA654321"
plaintext := []byte(`{"type":"texture","data":"abc"}`)
ciphertext := mustEncrypt(t, plaintext, []byte(fileKey))
os.WriteFile(filepath.Join(srcDir, "data.json"), ciphertext, 0644)
entry := contentsEntry{Path: "data.json", Key: fileKey}
decrypted, err := processFile(entry, filepath.Join(srcDir, "data.json"), filepath.Join(dstDir, "data.json"))
if err != nil {
t.Fatalf("processFile error: %v", err)
}
if !decrypted {
t.Error("encrypted file should be reported as decrypted")
}
got, _ := os.ReadFile(filepath.Join(dstDir, "data.json"))
if string(got) != string(plaintext) {
t.Errorf("got %q, want %q", got, plaintext)
}
}
func TestProcessFile_ManifestCopiedPlain(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
dstDir := filepath.Join(tmp, "dst")
os.MkdirAll(srcDir, 0755)
manifest := `{"format_version":2,"header":{"uuid":"abc"}}`
os.WriteFile(filepath.Join(srcDir, "manifest.json"), []byte(manifest), 0644)
entry := contentsEntry{Path: "manifest.json", Key: "some-key-that-should-be-ignored!"}
decrypted, err := processFile(entry, filepath.Join(srcDir, "manifest.json"), filepath.Join(dstDir, "manifest.json"))
if err != nil {
t.Fatalf("processFile error: %v", err)
}
if decrypted {
t.Error("manifest.json should be copied plain, not reported as decrypted")
}
got, _ := os.ReadFile(filepath.Join(dstDir, "manifest.json"))
if string(got) != manifest {
t.Errorf("manifest.json should be copied verbatim, got %q", got)
}
}
func TestProcessFile_DirectoryMarker(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
dstDir := filepath.Join(tmp, "dst")
os.MkdirAll(filepath.Join(srcDir, "items"), 0755)
os.WriteFile(filepath.Join(srcDir, "items", "child.json"), []byte("{}"), 0644)
entry := contentsEntry{Path: "items", Key: ""}
decrypted, err := processFile(entry, filepath.Join(srcDir, "items"), filepath.Join(dstDir, "items"))
if err != nil {
t.Fatalf("processFile should treat directory marker as no-op, got error: %v", err)
}
if decrypted {
t.Error("directory marker should not be reported as decrypted")
}
info, err := os.Stat(filepath.Join(dstDir, "items"))
if err != nil {
t.Fatalf("expected dst directory to be created: %v", err)
}
if !info.IsDir() {
t.Error("dst path should be a directory")
}
}
func TestCopyPackIcon(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
dstDir := filepath.Join(tmp, "dst")
os.MkdirAll(srcDir, 0755)
os.MkdirAll(dstDir, 0755)
os.WriteFile(filepath.Join(srcDir, "pack_icon.png"), []byte("PNG_DATA"), 0644)
err := copyIfMissing(srcDir, dstDir, packIconPNG)
if err != nil {
t.Fatalf("copyIfMissing error: %v", err)
}
got, _ := os.ReadFile(filepath.Join(dstDir, "pack_icon.png"))
if string(got) != "PNG_DATA" {
t.Errorf("got %q, want %q", got, "PNG_DATA")
}
}
func TestCopyPackIcon_AlreadyExists(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
dstDir := filepath.Join(tmp, "dst")
os.MkdirAll(srcDir, 0755)
os.MkdirAll(dstDir, 0755)
os.WriteFile(filepath.Join(srcDir, "pack_icon.png"), []byte("NEW"), 0644)
os.WriteFile(filepath.Join(dstDir, "pack_icon.png"), []byte("OLD"), 0644)
copyIfMissing(srcDir, dstDir, packIconPNG)
got, _ := os.ReadFile(filepath.Join(dstDir, "pack_icon.png"))
if string(got) != "OLD" {
t.Error("should not overwrite existing icon")
}
}
func TestCopyPackIcon_NoIcon(t *testing.T) {
tmp := t.TempDir()
err := copyIfMissing(filepath.Join(tmp, "src"), filepath.Join(tmp, "dst"), packIconPNG)
if err != nil {
t.Errorf("expected nil error when no icon exists, got %v", err)
}
}
// TestCopyIfMissing_Manifest: a manifest absent from contents.json must
// still land in the decrypted pack so it stays loadable.
func TestCopyIfMissing_Manifest(t *testing.T) {
tmp := t.TempDir()
src := filepath.Join(tmp, "src")
dst := filepath.Join(tmp, "dst")
os.MkdirAll(src, 0755)
os.MkdirAll(dst, 0755)
os.WriteFile(filepath.Join(src, manifestJSON), []byte(`{"header":{}}`), 0644)
if err := copyIfMissing(src, dst, manifestJSON); err != nil {
t.Fatalf("copyIfMissing: %v", err)
}
got, _ := os.ReadFile(filepath.Join(dst, manifestJSON))
if string(got) != `{"header":{}}` {
t.Errorf("manifest not copied: got %q", got)
}
}