-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidation_test.go
More file actions
483 lines (455 loc) · 14.3 KB
/
Copy pathvalidation_test.go
File metadata and controls
483 lines (455 loc) · 14.3 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package main
import (
"arker/internal/utils"
"os"
"path/filepath"
"testing"
)
// withMediaCookies configures a cookie jar for the duration of a test. Routing
// depends on it: login-only sites are not queued when no jar is available.
func withMediaCookies(t *testing.T) {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "cookies.txt")
if err := os.WriteFile(path, []byte("# Netscape HTTP Cookie File\n"), 0o600); err != nil {
t.Fatalf("write cookies: %v", err)
}
if _, err := utils.InitYtDlpCookies(path, "", dir); err != nil {
t.Fatalf("init cookies: %v", err)
}
t.Cleanup(func() { utils.InitYtDlpCookies("", "", dir) })
}
func TestURLValidation(t *testing.T) {
tests := []struct {
name string
url string
shouldError bool
errorMsg string
}{
{
name: "Valid HTTPS URL",
url: "https://example.com",
shouldError: false,
},
{
name: "Valid HTTP URL",
url: "http://example.com/path",
shouldError: false,
},
{
name: "Empty URL",
url: "",
shouldError: true,
errorMsg: "URL cannot be empty",
},
{
name: "Localhost",
url: "http://localhost:8080",
shouldError: true,
errorMsg: "requests to localhost are not allowed",
},
{
name: "127.0.0.1",
url: "http://127.0.0.1:3000",
shouldError: true,
errorMsg: "requests to localhost are not allowed",
},
{
name: "Private IP 192.168.x.x",
url: "http://192.168.1.1",
shouldError: true,
errorMsg: "requests to private/internal IP addresses are not allowed",
},
{
name: "Private IP 10.x.x.x",
url: "http://10.0.0.1",
shouldError: true,
errorMsg: "requests to private/internal IP addresses are not allowed",
},
{
name: "File protocol",
url: "file:///etc/passwd",
shouldError: true,
errorMsg: "only HTTP and HTTPS protocols are allowed",
},
{
name: "FTP protocol",
url: "ftp://example.com",
shouldError: true,
errorMsg: "only HTTP and HTTPS protocols are allowed",
},
{
name: "URL without protocol - should auto-add",
url: "example.com",
shouldError: false,
},
{
name: "URL without protocol with path",
url: "example.com/path",
shouldError: false,
},
{
name: "URL with suspicious pattern",
url: "http://example.com/file://attack",
shouldError: true,
errorMsg: "protocol file:// is not allowed",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := utils.ValidateURL(tt.url)
if tt.shouldError {
if err == nil {
t.Errorf("Expected error for URL %s, but got none", tt.url)
return
}
if tt.errorMsg != "" && !contains(err.Error(), tt.errorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", tt.errorMsg, err.Error())
}
} else {
if err != nil {
t.Errorf("Expected no error for URL %s, but got: %v", tt.url, err)
}
}
})
}
}
func TestArchiveRequestValidation(t *testing.T) {
tests := []struct {
name string
request utils.ArchiveRequest
shouldError bool
}{
{
name: "Valid request with no types",
request: utils.ArchiveRequest{
URL: "https://example.com",
},
shouldError: false,
},
{
name: "Valid request with types",
request: utils.ArchiveRequest{
URL: "https://example.com",
Types: []string{"mhtml", "screenshot"},
},
shouldError: false,
},
{
name: "Empty URL",
request: utils.ArchiveRequest{
URL: "",
},
shouldError: true,
},
{
name: "Invalid archive type",
request: utils.ArchiveRequest{
URL: "https://example.com",
Types: []string{"invalid-type"},
},
shouldError: true,
},
{
name: "SSRF attempt",
request: utils.ArchiveRequest{
URL: "http://localhost:8080",
},
shouldError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.request.Validate()
if tt.shouldError {
if err == nil {
t.Errorf("Expected error for request %+v, but got none", tt.request)
}
} else {
if err != nil {
t.Errorf("Expected no error for request %+v, but got: %v", tt.request, err)
}
}
})
}
}
func TestGitURLDetection(t *testing.T) {
tests := []struct {
name string
url string
expected bool
}{
// Should be detected as Git repositories
{"GitHub repo", "https://github.com/gamerwaves/reponame", true},
{"GitHub repo with path", "https://github.com/user/repo/tree/main", true},
{"GitLab repo", "https://gitlab.com/user/project", true},
{"Bitbucket repo", "https://bitbucket.org/user/repo", true},
{"Codeberg repo", "https://codeberg.org/user/repo", true},
{"Tangled repo", "https://tangled.org/dunkirk.sh/akami", true},
{"Tangled repo with at-handle", "https://tangled.org/@dunkirk.sh/akami", true},
{"Tangled repo commit", "https://tangled.org/dunkirk.sh/akami/commit/45f6aaec", true},
{"Tangled repo blob", "https://tangled.org/@dunkirk.sh/akami/blob/main/README.md", true},
{"Direct git URL", "https://example.com/repo.git", true},
{"Git subdomain", "https://git.example.com/repo", true},
// Should NOT be detected as Git repositories
{"GitHub user profile", "https://github.com/gamerwaves", false},
{"GitHub settings", "https://github.com/settings", false},
{"GitHub explore", "https://github.com/explore", false},
{"GitHub marketplace", "https://github.com/marketplace", false},
{"GitHub organizations", "https://github.com/organizations", false},
{"GitLab user profile", "https://gitlab.com/username", false},
{"Tangled user profile", "https://tangled.org/@dunkirk.sh", false},
{"Tangled settings", "https://tangled.org/settings/profile", false},
{"Tangled URL in query", "https://example.com/?next=https://tangled.org/user/repo", false},
{"Regular website", "https://example.com", false},
{"YouTube", "https://youtube.com/watch?v=123", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := utils.IsGitURL(tt.url)
if result != tt.expected {
t.Errorf("IsGitURL(%q) = %v, expected %v", tt.url, result, tt.expected)
}
})
}
}
func TestGetArchiveTypes(t *testing.T) {
// Cookies configured: login-only sites (Instagram, X) are routed.
withMediaCookies(t)
tests := []struct {
name string
url string
expected []string
}{
{
name: "GitHub repository",
url: "https://github.com/user/repo",
expected: []string{"mhtml", "screenshot", "git"},
},
{
name: "GitHub user profile",
url: "https://github.com/gamerwaves",
expected: []string{"mhtml", "screenshot"},
},
{
name: "Tangled repository file",
url: "https://tangled.org/@dunkirk.sh/akami/blob/main/README.md",
expected: []string{"mhtml", "screenshot", "git"},
},
{
name: "YouTube video",
url: "https://youtube.com/watch?v=123",
expected: []string{"mhtml", "screenshot", "yt-dlp"},
},
{
name: "Instagram reel",
url: "https://www.instagram.com/reel/DPAid-WDi67/",
expected: []string{"mhtml", "screenshot", "yt-dlp"},
},
{
// A feed post is usually a photo or a carousel, which yt-dlp
// cannot download at all. It goes to gallery-dl instead, and
// must NOT also get a yt-dlp item that is guaranteed to fail.
name: "Instagram post",
url: "https://www.instagram.com/p/ABC123/",
expected: []string{"mhtml", "screenshot", "gallery-dl"},
},
{
name: "Instagram TV post",
url: "https://www.instagram.com/tv/ABC123/",
expected: []string{"mhtml", "screenshot", "gallery-dl"},
},
{
name: "TikTok video",
url: "https://www.tiktok.com/@voidfrida/video/7495821166951566599",
expected: []string{"mhtml", "screenshot", "yt-dlp"},
},
{
name: "Facebook reel",
url: "https://www.facebook.com/reel/1714939389160829/",
expected: []string{"mhtml", "screenshot", "yt-dlp"},
},
{
name: "X status",
url: "https://x.com/someone/status/1234567890",
expected: []string{"mhtml", "screenshot", "gallery-dl"},
},
{
name: "Reddit submission",
url: "https://www.reddit.com/r/pics/comments/abc123/a_title/",
expected: []string{"mhtml", "screenshot", "gallery-dl"},
},
{
name: "Bluesky post",
url: "https://bsky.app/profile/someone.bsky.social/post/abc123",
expected: []string{"mhtml", "screenshot", "gallery-dl"},
},
{
// A profile page is not a single post; archiving it via
// gallery-dl would pull down the whole account.
name: "Instagram profile",
url: "https://www.instagram.com/someone/",
expected: []string{"mhtml", "screenshot"},
},
{
name: "Regular website",
url: "https://example.com",
expected: []string{"mhtml", "screenshot"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := utils.GetArchiveTypes(tt.url)
if len(result) != len(tt.expected) {
t.Errorf("GetArchiveTypes(%q) = %v, expected %v", tt.url, result, tt.expected)
return
}
for i, archiveType := range tt.expected {
if result[i] != archiveType {
t.Errorf("GetArchiveTypes(%q) = %v, expected %v", tt.url, result, tt.expected)
break
}
}
})
}
}
// Without a cookie jar, a login-only site cannot be archived at all, so no
// gallery-dl item is created: it would be a guaranteed failure that still
// spends requests against a site that rate-limits us for them.
func TestGetArchiveTypesSkipsLoginOnlySitesWithoutCookies(t *testing.T) {
loginOnly := []string{
"https://www.instagram.com/p/ABC123/",
"https://www.instagram.com/tv/ABC123/",
"https://x.com/someone/status/123",
"https://twitter.com/someone/status/123",
"https://www.pinterest.com/pin/123/",
}
for _, url := range loginOnly {
types := utils.GetArchiveTypes(url)
for _, archiveType := range types {
if archiveType == utils.ArchiveTypeGalleryDl {
t.Errorf("GetArchiveTypes(%q) = %v, want no gallery-dl item without cookies", url, types)
}
}
}
// Sites that work anonymously keep their gallery-dl item.
for _, url := range []string{
"https://imgur.com/a/Kn9lB",
"https://bsky.app/profile/a.bsky.social/post/abc",
"https://www.reddit.com/r/pics/comments/abc/title/",
} {
found := false
for _, archiveType := range utils.GetArchiveTypes(url) {
if archiveType == utils.ArchiveTypeGalleryDl {
found = true
}
}
if !found {
t.Errorf("GetArchiveTypes(%q) dropped gallery-dl, but the site works anonymously", url)
}
}
// An Instagram reel is unaffected: it goes to yt-dlp, which archives it
// anonymously.
reelTypes := utils.GetArchiveTypes("https://www.instagram.com/reel/ABC123/")
found := false
for _, archiveType := range reelTypes {
if archiveType == utils.ArchiveTypeYtDlp {
found = true
}
}
if !found {
t.Errorf("GetArchiveTypes(reel) = %v, want a yt-dlp item", reelTypes)
}
}
func TestIsInstagramURL(t *testing.T) {
tests := []struct {
name string
url string
expected bool
}{
// Should be detected as Instagram URLs
{"Instagram reel", "https://www.instagram.com/reel/DPAid-WDi67/", true},
{"Instagram reel without www", "https://instagram.com/reel/ABC123/", true},
{"Instagram post", "https://www.instagram.com/p/XYZ789/", true},
{"Instagram TV", "https://www.instagram.com/tv/DEF456/", true},
// Should NOT be detected as Instagram URLs
{"Instagram profile", "https://www.instagram.com/username/", false},
{"Instagram home", "https://www.instagram.com/", false},
{"Instagram stories", "https://www.instagram.com/stories/username/", false},
{"Regular website", "https://example.com", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := utils.IsInstagramURL(tt.url)
if result != tt.expected {
t.Errorf("IsInstagramURL(%q) = %v, expected %v", tt.url, result, tt.expected)
}
})
}
}
func TestIsTikTokURL(t *testing.T) {
tests := []struct {
name string
url string
expected bool
}{
// Should be detected as TikTok URLs
{"TikTok video", "https://www.tiktok.com/@voidfrida/video/7495821166951566599", true},
{"TikTok video without www", "https://tiktok.com/@user/video/123456789", true},
{"TikTok video with mobile", "https://m.tiktok.com/@user/video/987654321", true},
{"TikTok vm short link", "https://vm.tiktok.com/ZNRK8UVae/", true},
{"TikTok vt short link", "https://vt.tiktok.com/ZSAbCdEfG/", true},
{"TikTok t short link", "https://www.tiktok.com/t/ZTAbCdEfG/", true},
// Should NOT be detected as TikTok URLs
{"TikTok profile", "https://www.tiktok.com/@username", false},
{"TikTok home", "https://www.tiktok.com/", false},
{"TikTok discover", "https://www.tiktok.com/discover", false},
{"Regular website", "https://example.com", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := utils.IsTikTokURL(tt.url)
if result != tt.expected {
t.Errorf("IsTikTokURL(%q) = %v, expected %v", tt.url, result, tt.expected)
}
})
}
}
func TestIsFacebookURL(t *testing.T) {
tests := []struct {
name string
url string
expected bool
}{
// Should be detected as Facebook video URLs
{"Facebook reel", "https://www.facebook.com/reel/1714939389160829/", true},
{"Facebook reel without www", "https://facebook.com/reel/123456789/", true},
{"Facebook watch", "https://www.facebook.com/watch/?v=1234567890", true},
{"Facebook page video", "https://www.facebook.com/hackclub/videos/1234567890/", true},
{"fb.watch short link", "https://fb.watch/abcDEF123/", true},
// Should NOT be detected as Facebook video URLs
{"Facebook profile", "https://www.facebook.com/zuck", false},
{"Facebook home", "https://www.facebook.com/", false},
{"Regular website", "https://example.com", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := utils.IsFacebookURL(tt.url)
if result != tt.expected {
t.Errorf("IsFacebookURL(%q) = %v, expected %v", tt.url, result, tt.expected)
}
})
}
}
func contains(s, substr string) bool {
return len(s) >= len(substr) && s[:len(substr)] == substr || len(s) > len(substr) && s[len(s)-len(substr):] == substr ||
(len(s) > len(substr) && findSubstring(s, substr))
}
func findSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}