-
Notifications
You must be signed in to change notification settings - Fork 45
security: URL/path/JWT validation + proxy warning (APS-19008 partial / 19010 / 19011) #1141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rohannagariya1
wants to merge
4
commits into
master
Choose a base branch
from
security/cypress-cli-safe-subset
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e578fad
security: validate override/response URLs, config path, JWT; warn on …
Rohannagariya1 999a0b5
security(cli): suppress false-positive path-traversal Semgrep finding…
Rohannagariya1 12f73d3
[APS-19008] fix IPv6-loopback allowlist entry (::1 -> [::1])
Rohannagariya1 c5b17c2
Merge remote-tracking branch 'origin/master' into security/cypress-cl…
Rohannagariya1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| 'use strict'; | ||
|
|
||
| const path = require('path'); | ||
|
|
||
| /** | ||
| * Security validation helpers shared across the CLI. | ||
| * | ||
| * These guard the "untrusted edges" of the CLI: | ||
| * - override/response URLs that could redirect API traffic or uploads | ||
| * (APS-19010, APS-19011) | ||
| * - config-file paths that could escape the project directory (APS-19008) | ||
| * | ||
| * Kept dependency-free (stdlib only) so the logic can be unit tested without | ||
| * pulling in the CLI's network/config stack. | ||
| */ | ||
|
|
||
| // Hosts the CLI is allowed to talk to for API / upload endpoints. Covers | ||
| // production, staging (bsstag.com) and local development. Anything else is | ||
| // treated as attacker-controlled and rejected. | ||
| const ALLOWED_HOST_SUFFIXES = ['.browserstack.com', '.bsstag.com']; | ||
| // Note: URL parsing yields '[::1]' (bracketed) as the hostname for IPv6 loopback. | ||
| const ALLOWED_EXACT_HOSTS = ['browserstack.com', 'bsstag.com', 'localhost', '127.0.0.1', '[::1]']; | ||
|
|
||
| /** | ||
| * Returns true if the given URL points at a BrowserStack (prod/staging) host or | ||
| * localhost. Only http/https are accepted. Any parse failure returns false | ||
| * (fail-closed). | ||
| * @param {string} urlString | ||
| * @returns {boolean} | ||
| */ | ||
| function isAllowedBrowserstackUrl(urlString) { | ||
| if (typeof urlString !== 'string' || urlString.trim() === '') { | ||
| return false; | ||
| } | ||
| let parsed; | ||
| try { | ||
| parsed = new URL(urlString); | ||
| } catch (e) { | ||
| return false; | ||
| } | ||
| if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') { | ||
| return false; | ||
| } | ||
| const host = parsed.hostname.toLowerCase(); | ||
| if (ALLOWED_EXACT_HOSTS.includes(host)) { | ||
| return true; | ||
| } | ||
| return ALLOWED_HOST_SUFFIXES.some((suffix) => host.endsWith(suffix)); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a candidate path and asserts it stays inside baseDir. Used to stop | ||
| * config-file path traversal (e.g. --config-file ../../outside/browserstack.json). | ||
| * @param {string} candidatePath | ||
| * @param {string} baseDir defaults to process.cwd() | ||
| * @returns {boolean} | ||
| */ | ||
| function isPathInsideBase(candidatePath, baseDir) { | ||
| if (typeof candidatePath !== 'string' || candidatePath === '') { | ||
| return false; | ||
| } | ||
| // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- these resolves ARE the traversal guard: the value is normalized here only so the containment check below can reject anything outside `base`. | ||
| const base = path.resolve(baseDir || process.cwd()); | ||
|
|
||
| // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- see above; resolved path is validated by the startsWith(base) check, not used to read the FS unchecked. | ||
| const resolved = path.resolve(base, candidatePath); | ||
|
|
||
| // Must be the base itself or a descendant (base + separator prefix). | ||
| return resolved === base || resolved.startsWith(base + path.sep); | ||
| } | ||
|
|
||
| /** | ||
| * Structural (NOT cryptographic) validation of a JWT: three non-empty | ||
| * base64url segments. The CLI is not the token issuer and has no key to verify | ||
| * the signature, so this only rejects obviously-malformed / MITM-swapped | ||
| * garbage tokens. Defence-in-depth, not an integrity guarantee. | ||
| * @param {string} token | ||
| * @returns {boolean} | ||
| */ | ||
| function isWellFormedJwt(token) { | ||
| if (typeof token !== 'string') { | ||
| return false; | ||
| } | ||
| const parts = token.split('.'); | ||
| if (parts.length !== 3) { | ||
| return false; | ||
| } | ||
| return parts.every((p) => /^[A-Za-z0-9_-]+$/.test(p)); | ||
| } | ||
|
|
||
| module.exports = { | ||
| isAllowedBrowserstackUrl, | ||
| isPathInsideBase, | ||
| isWellFormedJwt, | ||
| ALLOWED_HOST_SUFFIXES, | ||
| ALLOWED_EXACT_HOSTS, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| 'use strict'; | ||
| const path = require('path'); | ||
| const { expect } = require('chai'); | ||
|
|
||
| const { | ||
| isAllowedBrowserstackUrl, | ||
| isPathInsideBase, | ||
| isWellFormedJwt, | ||
| } = require('../../../../bin/helpers/securityValidation'); | ||
|
|
||
| describe('securityValidation', () => { | ||
| describe('isAllowedBrowserstackUrl', () => { | ||
| it('accepts BrowserStack production and staging hosts', () => { | ||
| expect(isAllowedBrowserstackUrl('https://api.browserstack.com')).to.be.true; | ||
| expect(isAllowedBrowserstackUrl('https://api-cloud.browserstack.com/automate-frameworks/cypress/upload')).to.be.true; | ||
| expect(isAllowedBrowserstackUrl('https://staging.bsstag.com')).to.be.true; | ||
| expect(isAllowedBrowserstackUrl('https://browserstack.com')).to.be.true; | ||
| }); | ||
|
|
||
| it('accepts localhost for local development', () => { | ||
| expect(isAllowedBrowserstackUrl('http://localhost:3000')).to.be.true; | ||
| expect(isAllowedBrowserstackUrl('http://127.0.0.1:8080')).to.be.true; | ||
| }); | ||
|
|
||
| it('rejects arbitrary attacker hosts', () => { | ||
| expect(isAllowedBrowserstackUrl('https://attacker.example')).to.be.false; | ||
| expect(isAllowedBrowserstackUrl('https://evil.com')).to.be.false; | ||
| }); | ||
|
|
||
| it('rejects look-alike / suffix-spoofing hosts', () => { | ||
| // Not a real subdomain of browserstack.com — endsWith check uses a | ||
| // leading dot so this must be rejected. | ||
| expect(isAllowedBrowserstackUrl('https://browserstack.com.attacker.net')).to.be.false; | ||
| expect(isAllowedBrowserstackUrl('https://notbrowserstack.com')).to.be.false; | ||
| expect(isAllowedBrowserstackUrl('https://evilbrowserstack.com')).to.be.false; | ||
| }); | ||
|
|
||
| it('rejects non-http(s) schemes and malformed input', () => { | ||
| expect(isAllowedBrowserstackUrl('file:///etc/passwd')).to.be.false; | ||
| expect(isAllowedBrowserstackUrl('ftp://api.browserstack.com')).to.be.false; | ||
| expect(isAllowedBrowserstackUrl('not a url')).to.be.false; | ||
| expect(isAllowedBrowserstackUrl('')).to.be.false; | ||
| expect(isAllowedBrowserstackUrl(undefined)).to.be.false; | ||
| expect(isAllowedBrowserstackUrl(null)).to.be.false; | ||
| }); | ||
| }); | ||
|
|
||
| describe('isPathInsideBase', () => { | ||
| const base = path.resolve('/tmp/project'); | ||
|
|
||
| it('accepts paths inside the base directory', () => { | ||
| expect(isPathInsideBase('browserstack.json', base)).to.be.true; | ||
| expect(isPathInsideBase('sub/dir/browserstack.json', base)).to.be.true; | ||
| expect(isPathInsideBase(path.join(base, 'browserstack.json'), base)).to.be.true; | ||
| }); | ||
|
|
||
| it('accepts the base directory itself', () => { | ||
| expect(isPathInsideBase(base, base)).to.be.true; | ||
| }); | ||
|
|
||
| it('rejects path traversal outside the base directory', () => { | ||
| expect(isPathInsideBase('../../etc/passwd', base)).to.be.false; | ||
| expect(isPathInsideBase('../outside/browserstack.json', base)).to.be.false; | ||
| expect(isPathInsideBase('/etc/passwd', base)).to.be.false; | ||
| }); | ||
|
|
||
| it('rejects a sibling directory that shares a name prefix', () => { | ||
| // /tmp/project-evil must not be treated as inside /tmp/project. | ||
| expect(isPathInsideBase('/tmp/project-evil/x.json', base)).to.be.false; | ||
| }); | ||
|
|
||
| it('rejects empty / non-string input', () => { | ||
| expect(isPathInsideBase('', base)).to.be.false; | ||
| expect(isPathInsideBase(undefined, base)).to.be.false; | ||
| }); | ||
| }); | ||
|
|
||
| describe('isWellFormedJwt', () => { | ||
| it('accepts a structurally valid three-part token', () => { | ||
| expect(isWellFormedJwt('aaa.bbb.ccc')).to.be.true; | ||
| expect(isWellFormedJwt('eyJhbGci.eyJzdWIi.SflKxwRJ-abc_123')).to.be.true; | ||
| }); | ||
|
|
||
| it('rejects tokens without exactly three parts', () => { | ||
| expect(isWellFormedJwt('aaa.bbb')).to.be.false; | ||
| expect(isWellFormedJwt('aaa.bbb.ccc.ddd')).to.be.false; | ||
| expect(isWellFormedJwt('notajwt')).to.be.false; | ||
| }); | ||
|
|
||
| it('rejects tokens with empty or invalid segments', () => { | ||
| expect(isWellFormedJwt('aaa..ccc')).to.be.false; | ||
| expect(isWellFormedJwt('aaa.b b.ccc')).to.be.false; | ||
| expect(isWellFormedJwt('')).to.be.false; | ||
| expect(isWellFormedJwt(undefined)).to.be.false; | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.