|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { isValidManifestUrl, TRUSTED_PREFIX } from './isValidManifestUrl.js'; |
| 4 | + |
| 5 | +describe('isValidManifestUrl', () => { |
| 6 | + describe('valid URLs', () => { |
| 7 | + it('should accept a URL directly under the trusted prefix', () => { |
| 8 | + const url = `${TRUSTED_PREFIX}samples/my-sample/manifest.json`; |
| 9 | + assert.equal(isValidManifestUrl(url), true); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should accept a URL in a nested path under the trusted prefix', () => { |
| 13 | + const url = `${TRUSTED_PREFIX}samples/app/deep/path/manifest.json`; |
| 14 | + assert.equal(isValidManifestUrl(url), true); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should accept the trusted prefix itself', () => { |
| 18 | + assert.equal(isValidManifestUrl(TRUSTED_PREFIX), true); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('path traversal attacks', () => { |
| 23 | + it('should reject a URL with ../ that traverses to another repository', () => { |
| 24 | + const url = 'https://raw.githubusercontent.com/pnp/mgt-samples/main/../../../attacker/repo/main/manifest.json'; |
| 25 | + assert.equal(isValidManifestUrl(url), false); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should reject a URL with encoded traversal segments (%2e%2e)', () => { |
| 29 | + const url = 'https://raw.githubusercontent.com/pnp/mgt-samples/main/%2e%2e/%2e%2e/%2e%2e/attacker/repo/main/evil.js'; |
| 30 | + assert.equal(isValidManifestUrl(url), false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should reject a URL that uses ../ to escape to a sibling repo', () => { |
| 34 | + const url = 'https://raw.githubusercontent.com/pnp/mgt-samples/main/../other-repo/main/payload.json'; |
| 35 | + assert.equal(isValidManifestUrl(url), false); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should reject a URL with multiple ../ segments', () => { |
| 39 | + const url = 'https://raw.githubusercontent.com/pnp/mgt-samples/main/../../evil-org/evil-repo/main/manifest.json'; |
| 40 | + assert.equal(isValidManifestUrl(url), false); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('different origins and invalid URLs', () => { |
| 45 | + it('should reject a URL from a completely different domain', () => { |
| 46 | + const url = 'https://evil.com/pnp/mgt-samples/main/manifest.json'; |
| 47 | + assert.equal(isValidManifestUrl(url), false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should reject a URL from a different GitHub user/org', () => { |
| 51 | + const url = 'https://raw.githubusercontent.com/attacker/malicious-repo/main/manifest.json'; |
| 52 | + assert.equal(isValidManifestUrl(url), false); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should reject a URL that only partially matches the prefix', () => { |
| 56 | + const url = 'https://raw.githubusercontent.com/pnp/mgt-samples-evil/main/manifest.json'; |
| 57 | + assert.equal(isValidManifestUrl(url), false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should reject a data: URI', () => { |
| 61 | + const url = 'data:application/json;base64,W3sicHJldmlldyI6eyJqcyI6Imh0dHA6Ly9ldmlsLmNvbS9ldmlsLmpzIn19XQ=='; |
| 62 | + assert.equal(isValidManifestUrl(url), false); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should reject a javascript: URI', () => { |
| 66 | + const url = 'javascript:alert(1)'; |
| 67 | + assert.equal(isValidManifestUrl(url), false); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('null/empty/malformed inputs', () => { |
| 72 | + it('should reject null', () => { |
| 73 | + assert.equal(isValidManifestUrl(null), false); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should reject undefined', () => { |
| 77 | + assert.equal(isValidManifestUrl(undefined), false); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should reject empty string', () => { |
| 81 | + assert.equal(isValidManifestUrl(''), false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should reject a malformed URL', () => { |
| 85 | + assert.equal(isValidManifestUrl('not-a-url'), false); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should reject a relative path', () => { |
| 89 | + assert.equal(isValidManifestUrl('../../../etc/passwd'), false); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments