|
| 1 | +'use strict'; |
| 2 | +const path = require('path'); |
| 3 | +const { expect } = require('chai'); |
| 4 | + |
| 5 | +const { |
| 6 | + isAllowedBrowserstackUrl, |
| 7 | + isPathInsideBase, |
| 8 | + isWellFormedJwt, |
| 9 | +} = require('../../../../bin/helpers/securityValidation'); |
| 10 | + |
| 11 | +describe('securityValidation', () => { |
| 12 | + describe('isAllowedBrowserstackUrl', () => { |
| 13 | + it('accepts BrowserStack production and staging hosts', () => { |
| 14 | + expect(isAllowedBrowserstackUrl('https://api.browserstack.com')).to.be.true; |
| 15 | + expect(isAllowedBrowserstackUrl('https://api-cloud.browserstack.com/automate-frameworks/cypress/upload')).to.be.true; |
| 16 | + expect(isAllowedBrowserstackUrl('https://staging.bsstag.com')).to.be.true; |
| 17 | + expect(isAllowedBrowserstackUrl('https://browserstack.com')).to.be.true; |
| 18 | + }); |
| 19 | + |
| 20 | + it('accepts localhost for local development', () => { |
| 21 | + expect(isAllowedBrowserstackUrl('http://localhost:3000')).to.be.true; |
| 22 | + expect(isAllowedBrowserstackUrl('http://127.0.0.1:8080')).to.be.true; |
| 23 | + }); |
| 24 | + |
| 25 | + it('rejects arbitrary attacker hosts', () => { |
| 26 | + expect(isAllowedBrowserstackUrl('https://attacker.example')).to.be.false; |
| 27 | + expect(isAllowedBrowserstackUrl('https://evil.com')).to.be.false; |
| 28 | + }); |
| 29 | + |
| 30 | + it('rejects look-alike / suffix-spoofing hosts', () => { |
| 31 | + // Not a real subdomain of browserstack.com — endsWith check uses a |
| 32 | + // leading dot so this must be rejected. |
| 33 | + expect(isAllowedBrowserstackUrl('https://browserstack.com.attacker.net')).to.be.false; |
| 34 | + expect(isAllowedBrowserstackUrl('https://notbrowserstack.com')).to.be.false; |
| 35 | + expect(isAllowedBrowserstackUrl('https://evilbrowserstack.com')).to.be.false; |
| 36 | + }); |
| 37 | + |
| 38 | + it('rejects non-http(s) schemes and malformed input', () => { |
| 39 | + expect(isAllowedBrowserstackUrl('file:///etc/passwd')).to.be.false; |
| 40 | + expect(isAllowedBrowserstackUrl('ftp://api.browserstack.com')).to.be.false; |
| 41 | + expect(isAllowedBrowserstackUrl('not a url')).to.be.false; |
| 42 | + expect(isAllowedBrowserstackUrl('')).to.be.false; |
| 43 | + expect(isAllowedBrowserstackUrl(undefined)).to.be.false; |
| 44 | + expect(isAllowedBrowserstackUrl(null)).to.be.false; |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('isPathInsideBase', () => { |
| 49 | + const base = path.resolve('/tmp/project'); |
| 50 | + |
| 51 | + it('accepts paths inside the base directory', () => { |
| 52 | + expect(isPathInsideBase('browserstack.json', base)).to.be.true; |
| 53 | + expect(isPathInsideBase('sub/dir/browserstack.json', base)).to.be.true; |
| 54 | + expect(isPathInsideBase(path.join(base, 'browserstack.json'), base)).to.be.true; |
| 55 | + }); |
| 56 | + |
| 57 | + it('accepts the base directory itself', () => { |
| 58 | + expect(isPathInsideBase(base, base)).to.be.true; |
| 59 | + }); |
| 60 | + |
| 61 | + it('rejects path traversal outside the base directory', () => { |
| 62 | + expect(isPathInsideBase('../../etc/passwd', base)).to.be.false; |
| 63 | + expect(isPathInsideBase('../outside/browserstack.json', base)).to.be.false; |
| 64 | + expect(isPathInsideBase('/etc/passwd', base)).to.be.false; |
| 65 | + }); |
| 66 | + |
| 67 | + it('rejects a sibling directory that shares a name prefix', () => { |
| 68 | + // /tmp/project-evil must not be treated as inside /tmp/project. |
| 69 | + expect(isPathInsideBase('/tmp/project-evil/x.json', base)).to.be.false; |
| 70 | + }); |
| 71 | + |
| 72 | + it('rejects empty / non-string input', () => { |
| 73 | + expect(isPathInsideBase('', base)).to.be.false; |
| 74 | + expect(isPathInsideBase(undefined, base)).to.be.false; |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('isWellFormedJwt', () => { |
| 79 | + it('accepts a structurally valid three-part token', () => { |
| 80 | + expect(isWellFormedJwt('aaa.bbb.ccc')).to.be.true; |
| 81 | + expect(isWellFormedJwt('eyJhbGci.eyJzdWIi.SflKxwRJ-abc_123')).to.be.true; |
| 82 | + }); |
| 83 | + |
| 84 | + it('rejects tokens without exactly three parts', () => { |
| 85 | + expect(isWellFormedJwt('aaa.bbb')).to.be.false; |
| 86 | + expect(isWellFormedJwt('aaa.bbb.ccc.ddd')).to.be.false; |
| 87 | + expect(isWellFormedJwt('notajwt')).to.be.false; |
| 88 | + }); |
| 89 | + |
| 90 | + it('rejects tokens with empty or invalid segments', () => { |
| 91 | + expect(isWellFormedJwt('aaa..ccc')).to.be.false; |
| 92 | + expect(isWellFormedJwt('aaa.b b.ccc')).to.be.false; |
| 93 | + expect(isWellFormedJwt('')).to.be.false; |
| 94 | + expect(isWellFormedJwt(undefined)).to.be.false; |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments