|
| 1 | +describe('Middleware Security Tests', () => { |
| 2 | + beforeEach(() => { |
| 3 | + cy.visit('http://localhost:3000'); |
| 4 | + }); |
| 5 | + |
| 6 | + afterEach(() => { |
| 7 | + cy.a11yCheck(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should prevent SSRF through middleware headers', () => { |
| 11 | + // Test that sensitive headers are not reflected back |
| 12 | + const sensitiveHeaders = { |
| 13 | + 'X-Forwarded-Host': 'evil.com', |
| 14 | + 'X-Forwarded-Proto': 'http', |
| 15 | + 'X-Real-IP': '127.0.0.1', |
| 16 | + 'X-Forwarded-For': '192.168.1.1, evil.com', |
| 17 | + Host: 'attacker.site', |
| 18 | + }; |
| 19 | + |
| 20 | + Object.entries(sensitiveHeaders).forEach(([headerName, headerValue]) => { |
| 21 | + cy.request({ |
| 22 | + url: 'http://localhost:3000', |
| 23 | + headers: { |
| 24 | + [headerName]: headerValue, |
| 25 | + }, |
| 26 | + failOnStatusCode: false, |
| 27 | + }).then((response) => { |
| 28 | + // Check that sensitive headers are not reflected in response |
| 29 | + const responseHeaders = Object.keys(response.headers).map((key) => key.toLowerCase()); |
| 30 | + const responseHeaderValues = Object.values(response.headers).join(' '); |
| 31 | + |
| 32 | + // Ensure the malicious header value is not reflected back |
| 33 | + expect(responseHeaderValues).to.not.include(headerValue); |
| 34 | + |
| 35 | + // Check that response doesn't contain redirect to external domain |
| 36 | + if (response.status >= 300 && response.status < 400) { |
| 37 | + const location = response.headers.location; |
| 38 | + if (location) { |
| 39 | + expect(location).to.not.include('evil.com'); |
| 40 | + expect(location).to.not.include('attacker.site'); |
| 41 | + } |
| 42 | + } |
| 43 | + }); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should validate redirect destinations in middleware', () => { |
| 48 | + // Test that redirects don't allow SSRF |
| 49 | + const maliciousRedirects = [ |
| 50 | + 'http://evil.com', |
| 51 | + 'https://attacker.site/steal-data', |
| 52 | + 'ftp://internal-server.local', |
| 53 | + 'file:///etc/passwd', |
| 54 | + 'gopher://localhost:25', |
| 55 | + ]; |
| 56 | + |
| 57 | + maliciousRedirects.forEach((redirectUrl) => { |
| 58 | + cy.request({ |
| 59 | + url: `http://localhost:3000?redirect=${encodeURIComponent(redirectUrl)}`, |
| 60 | + followRedirect: false, |
| 61 | + failOnStatusCode: false, |
| 62 | + }).then((response) => { |
| 63 | + if (response.status >= 300 && response.status < 400) { |
| 64 | + const location = response.headers.location; |
| 65 | + if (location) { |
| 66 | + // Should not redirect to external or malicious URLs |
| 67 | + expect(location).to.not.include('evil.com'); |
| 68 | + expect(location).to.not.include('attacker.site'); |
| 69 | + expect(location).to.not.match(/^(ftp|file|gopher):/); |
| 70 | + |
| 71 | + // Should only redirect to same origin or relative paths |
| 72 | + if (typeof location === 'string' && location.startsWith('http')) { |
| 73 | + expect(location).to.match(/^https?:\/\/localhost(:\d+)?/); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should sanitize request headers in middleware processing', () => { |
| 82 | + // Test that middleware doesn't process dangerous header combinations |
| 83 | + cy.request({ |
| 84 | + url: 'http://localhost:3000', |
| 85 | + headers: { |
| 86 | + 'X-Forwarded-Host': 'evil.com', |
| 87 | + 'X-Forwarded-Proto': 'javascript', |
| 88 | + 'X-Original-URL': '/admin/secret', |
| 89 | + 'X-Rewrite-URL': '/../../etc/passwd', |
| 90 | + }, |
| 91 | + failOnStatusCode: false, |
| 92 | + }).then((response) => { |
| 93 | + // Verify response doesn't expose internal paths or dangerous content |
| 94 | + expect(response.body).to.not.include('/etc/passwd'); |
| 95 | + expect(response.body).to.not.include('/admin/secret'); |
| 96 | + expect(response.status).to.not.equal(500); // Should handle gracefully |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments