forked from cshum/imagor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates_test.go
More file actions
554 lines (485 loc) · 13.6 KB
/
templates_test.go
File metadata and controls
554 lines (485 loc) · 13.6 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package imagor
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestRenderLandingPage(t *testing.T) {
tests := []struct {
name string
expectedStatus int
expectedType string
expectedBody []string
}{
{
name: "landing page renders successfully",
expectedStatus: http.StatusOK,
expectedType: "text/html",
expectedBody: []string{
"<title>imagor",
"<h1>imagor</h1>",
Version,
"imagor.net",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
renderLandingPage(w)
// Check status code
if w.Code != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, w.Code)
}
// Check content type
contentType := w.Header().Get("Content-Type")
if !strings.Contains(contentType, tt.expectedType) {
t.Errorf("Expected content type to contain %s, got %s", tt.expectedType, contentType)
}
// Check body content
body := w.Body.String()
for _, expected := range tt.expectedBody {
if !strings.Contains(body, expected) {
t.Errorf("Expected body to contain %q, but it didn't. Body: %s", expected, body)
}
}
})
}
}
func TestRenderUploadForm(t *testing.T) {
tests := []struct {
name string
path string
expectedStatus int
expectedType string
expectedBody []string
}{
{
name: "upload form for root path",
path: "/",
expectedStatus: http.StatusOK,
expectedType: "text/html",
expectedBody: []string{
"<title>imagor",
"Upload Endpoint",
Version,
"<form method=\"POST\"",
"enctype=\"multipart/form-data\"",
"<input type=\"file\"",
"name=\"image\"",
},
},
{
name: "upload form for resize path",
path: "/unsafe/200x300/",
expectedStatus: http.StatusOK,
expectedType: "text/html",
expectedBody: []string{
"<title>imagor",
"Upload Endpoint",
"/unsafe/200x300/",
"<form method=\"POST\"",
},
},
{
name: "upload form for filters path",
path: "/unsafe/filters:quality(80):format(webp)/",
expectedStatus: http.StatusOK,
expectedType: "text/html",
expectedBody: []string{
"Upload Endpoint",
"/unsafe/filters:quality(80):format(webp)/",
"filters",
"quality",
"webp",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
renderUploadForm(w, tt.path)
// Check status code
if w.Code != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, w.Code)
}
// Check content type
contentType := w.Header().Get("Content-Type")
if !strings.Contains(contentType, tt.expectedType) {
t.Errorf("Expected content type to contain %s, got %s", tt.expectedType, contentType)
}
// Check body content
body := w.Body.String()
for _, expected := range tt.expectedBody {
if !strings.Contains(body, expected) {
t.Errorf("Expected body to contain %q, but it didn't. Body: %s", expected, body)
}
}
})
}
}
func TestTemplateDataStructure(t *testing.T) {
// Test TemplateData structure
data := TemplateData{
Version: "1.0.0",
Path: "/test/path",
Params: "test params",
}
if data.Version != "1.0.0" {
t.Errorf("Expected Version to be '1.0.0', got %s", data.Version)
}
if data.Path != "/test/path" {
t.Errorf("Expected Path to be '/test/path', got %s", data.Path)
}
if data.Params != "test params" {
t.Errorf("Expected Params to be 'test params', got %v", data.Params)
}
}
func TestRenderUploadFormWithComplexPath(t *testing.T) {
// Test with a complex imagor path
path := "/unsafe/fit-in/300x200/filters:quality(90):format(jpeg):fill(white)/smart/"
w := httptest.NewRecorder()
renderUploadForm(w, path)
if w.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", w.Code)
}
body := w.Body.String()
// Should contain the path
if !strings.Contains(body, path) {
t.Error("Expected body to contain the path")
}
// Should contain some JSON structure
if !strings.Contains(body, "{") || !strings.Contains(body, "}") {
t.Error("Expected body to contain JSON structure")
}
}
func TestRenderUploadFormErrorHandling(t *testing.T) {
// Test with invalid path that might cause parsing issues
paths := []string{
"/invalid/path/with/special/chars",
"/unsafe/invalid-dimensions/",
"/unsafe/filters:invalid()/",
"",
}
for _, path := range paths {
t.Run("path_"+path, func(t *testing.T) {
w := httptest.NewRecorder()
// Should not panic
renderUploadForm(w, path)
// Should return some response
if w.Code == 0 {
t.Error("Expected some HTTP status code")
}
// Should have some content
if w.Body.Len() == 0 {
t.Error("Expected some response body")
}
})
}
}
func TestTemplateInitialization(t *testing.T) {
// Test that templates are properly initialized
if landingTemplate == nil {
t.Error("Expected landingTemplate to be initialized")
}
if uploadTemplate == nil {
t.Error("Expected uploadTemplate to be initialized")
}
}
func TestRenderUploadFormJSONParams(t *testing.T) {
// Test that imagorpath.Parse results are properly marshaled to JSON
path := "/unsafe/200x300/filters:quality(80)/"
w := httptest.NewRecorder()
renderUploadForm(w, path)
body := w.Body.String()
// Should contain valid JSON structure
if !strings.Contains(body, "{") || !strings.Contains(body, "}") {
t.Error("Expected body to contain JSON structure")
}
// Should contain some path information
if !strings.Contains(body, path) {
t.Error("Expected body to contain the path")
}
}
func TestRenderLandingPageContent(t *testing.T) {
w := httptest.NewRecorder()
renderLandingPage(w)
body := w.Body.String()
// Check for essential landing page elements
essentialElements := []string{
"<!DOCTYPE html>",
"<html",
"<head>",
"<body>",
"imagor",
Version,
}
for _, element := range essentialElements {
if !strings.Contains(body, element) {
t.Errorf("Expected landing page to contain %q", element)
}
}
}
func TestRenderUploadFormResponseHeaders(t *testing.T) {
w := httptest.NewRecorder()
renderUploadForm(w, "/unsafe/200x200/")
// Check Content-Type header
contentType := w.Header().Get("Content-Type")
if contentType != "text/html" {
t.Errorf("Expected Content-Type to be 'text/html', got %s", contentType)
}
}
func TestRenderLandingPageResponseHeaders(t *testing.T) {
w := httptest.NewRecorder()
renderLandingPage(w)
// Check Content-Type header
contentType := w.Header().Get("Content-Type")
if contentType != "text/html" {
t.Errorf("Expected Content-Type to be 'text/html', got %s", contentType)
}
}
func TestTemplateXSSPrevention(t *testing.T) {
tests := []struct {
name string
path string
expectedStatus int
shouldNotContain []string
}{
{
name: "script injection in path",
path: "/unsafe/<script>alert('xss')</script>/",
expectedStatus: http.StatusOK,
shouldNotContain: []string{
"<script>alert('xss')</script>",
"alert('xss')",
},
},
{
name: "javascript protocol injection",
path: "/unsafe/javascript:alert('xss')/",
expectedStatus: http.StatusOK,
shouldNotContain: []string{
"javascript:alert('xss')",
"alert('xss')",
},
},
{
name: "html entity injection",
path: "/unsafe/<script>alert('xss')</script>/",
expectedStatus: http.StatusOK,
shouldNotContain: []string{
"<script>alert('xss')</script>", // Should not contain unescaped script
"javascript:alert", // Should not contain executable javascript
},
},
{
name: "svg injection attempt",
path: "/unsafe/<svg onload=alert('xss')>/",
expectedStatus: http.StatusOK,
shouldNotContain: []string{
"<svg onload=alert('xss')>",
"onload=alert('xss')",
},
},
{
name: "img onerror injection",
path: "/unsafe/<img src=x onerror=alert('xss')>/",
expectedStatus: http.StatusOK,
shouldNotContain: []string{
"<img src=x onerror=alert('xss')>",
"onerror=alert('xss')",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
renderUploadForm(w, tt.path)
if w.Code != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, w.Code)
}
body := w.Body.String()
for _, forbidden := range tt.shouldNotContain {
if strings.Contains(body, forbidden) {
t.Errorf("Response body should not contain %q, but it did. Body: %s", forbidden, body)
}
}
// Ensure the response is still functional
if !strings.Contains(body, "Upload Endpoint") {
t.Error("Response should still contain 'Upload Endpoint'")
}
})
}
}
func TestTemplateInputValidation(t *testing.T) {
tests := []struct {
name string
path string
expectedStatus int
description string
}{
{
name: "null byte injection",
path: "/unsafe/test\x00malicious/",
expectedStatus: http.StatusOK,
description: "Should handle null bytes safely",
},
{
name: "control characters",
path: "/unsafe/test\x01\x02\x03/",
expectedStatus: http.StatusOK,
description: "Should handle control characters",
},
{
name: "unicode edge cases",
path: "/unsafe/test\u202e\u202d/",
expectedStatus: http.StatusOK,
description: "Should handle unicode direction override",
},
{
name: "path traversal attempt",
path: "/unsafe/../../../etc/passwd/",
expectedStatus: http.StatusOK,
description: "Should handle path traversal safely",
},
{
name: "extremely long path",
path: "/unsafe/" + strings.Repeat("a", 10000) + "/",
expectedStatus: http.StatusOK,
description: "Should handle very long paths",
},
{
name: "json breaking characters",
path: "/unsafe/test\"\\'/",
expectedStatus: http.StatusOK,
description: "Should handle JSON-breaking characters",
},
{
name: "newline injection",
path: "/unsafe/test\n\r/",
expectedStatus: http.StatusOK,
description: "Should handle newline characters",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
// Should not panic
renderUploadForm(w, tt.path)
if w.Code != tt.expectedStatus {
t.Errorf("Expected status %d, got %d for %s", tt.expectedStatus, w.Code, tt.description)
}
// Should still produce valid HTML
body := w.Body.String()
if !strings.Contains(body, "<!DOCTYPE html>") {
t.Error("Response should contain valid HTML doctype")
}
// Should contain essential form elements
if !strings.Contains(body, "<form") {
t.Error("Response should contain form element")
}
})
}
}
func TestTemplateJSONInjection(t *testing.T) {
tests := []struct {
name string
path string
shouldEscape []string
}{
{
name: "double quote injection",
path: "/unsafe/test\"injection/",
shouldEscape: []string{
"\"injection",
},
},
{
name: "backslash injection",
path: "/unsafe/test\\injection/",
shouldEscape: []string{
"\\injection",
},
},
{
name: "json control characters",
path: "/unsafe/test\b\f\n\r\t/",
shouldEscape: []string{
"\b", "\f", "\n", "\r", "\t",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
renderUploadForm(w, tt.path)
body := w.Body.String()
// Check that the JSON in the response is properly escaped
// Look for the JSON section in the HTML
if strings.Contains(body, "<pre>") {
// Find JSON content between <pre> tags
start := strings.Index(body, "<pre>")
end := strings.Index(body[start:], "</pre>")
if end != -1 {
jsonContent := body[start+5 : start+end]
// Verify it's valid JSON by attempting to parse
var parsed interface{}
if err := json.Unmarshal([]byte(jsonContent), &parsed); err != nil {
t.Errorf("JSON in template should be valid, but got error: %v", err)
}
}
}
})
}
}
func TestTemplateEdgeCases(t *testing.T) {
t.Run("empty template data", func(t *testing.T) {
w := httptest.NewRecorder()
renderUploadForm(w, "")
if w.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", w.Code)
}
})
t.Run("special characters in filenames", func(t *testing.T) {
specialChars := []string{
"/unsafe/test file with spaces.jpg/",
"/unsafe/test-file_with.special@chars.jpg/",
"/unsafe/测试文件.jpg/",
"/unsafe/файл.jpg/",
"/unsafe/🖼️.jpg/",
}
for _, path := range specialChars {
t.Run("path_"+path, func(t *testing.T) {
w := httptest.NewRecorder()
renderUploadForm(w, path)
if w.Code != http.StatusOK {
t.Errorf("Expected status 200 for path %s, got %d", path, w.Code)
}
body := w.Body.String()
if !strings.Contains(body, "Upload Endpoint") {
t.Errorf("Response should contain 'Upload Endpoint' for path %s", path)
}
})
}
})
t.Run("deeply nested filter parameters", func(t *testing.T) {
// Test with nested watermark filters
nestedPath := "/unsafe/filters:" +
"watermark(test1.png,center,center,50):" +
"watermark(test2.png,left,top,30):" +
"watermark(test3.png,right,bottom,70):" +
"fill(white):quality(90):format(jpeg)/" +
"test.jpg"
w := httptest.NewRecorder()
renderUploadForm(w, nestedPath)
if w.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", w.Code)
}
body := w.Body.String()
if !strings.Contains(body, "watermark") {
t.Error("Response should contain watermark parameters")
}
})
}