Reject malformed HTTP URLs with `baseUrl`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
ecurity regression test
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 3419113 · Browse files · 2 files·+95 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+95 added−5 removed
- tests:+79 added−0 removed
- other:+16 added−5 removed
Provenance
- Commit
be60db5- Tree
b15b679- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
3419113
2 files changed+95 added−5 removed
modified:source/core/Ky.ts+16 added-5 removed
81 unmodified lines82 };83 };84 85 const objectToString = Object.prototype.toString;86+const leadingC0ControlOrSpacePattern = /^[\0-\u0020]+/g;87+const asciiTabOrNewLinePattern = /[\t\n\r]/g;88+const schemePattern = /^[a-z][\d+.a-z-]*:/i;89+const malformedHttpProtocolPattern = /^https?:(?!\/\/)/i;90 91 const isRequestInstance = (value: unknown): value is Request =>92 value instanceof globalThis.Request || objectToString.call(value) === '[object Request]';93 2 unmodified lines96 // body consumption, `json()` decoration, and any enabled stream features.97 const isResponseInstance = (value: unknown): value is Response =>98 value instanceof globalThis.Response || objectToString.call(value) === '[object Response]';99 100+const isAbsoluteInput = (input: string): boolean =>101+ schemePattern.test(input);102+103+const normalizeInputForProtocolCheck = (input: string): string =>104+ input.replaceAll(leadingC0ControlOrSpacePattern, '').replaceAll(asciiTabOrNewLinePattern, '');105+106 const cloneSearchParametersForInitHook = (searchParameters: SearchParamsOption | undefined): SearchParamsOption | undefined => {107 if (Array.isArray(searchParameters)) {108 return searchParameters.map(parameter => [...parameter]) as SearchParamsOption;109 }276 unmodified lines386 this.#input = `${normalizedPrefix}/${normalizedInput}`;387 }388 389 if (this.#options.baseUrl) {380- let absoluteInput: URL | undefined;381- try {382- absoluteInput = new URL(this.#input);383- } catch {}390+ const normalizedInput = normalizeInputForProtocolCheck(this.#input);391 385- if (!absoluteInput) {392+ if (malformedHttpProtocolPattern.test(normalizedInput)) {393+ throw new TypeError('`input` url protocol must be followed by `//` when using `baseUrl`');394+ }395+396+ if (!isAbsoluteInput(normalizedInput)) {397 this.#input = new URL(this.#input, (new Request(this.#options.baseUrl)).url);398 }399 }400 }751 unmodified linesmodified:test/base-url.ts+79 added
45 unmodified lines46474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113219 unmodified lines45 unmodified lines
await server.close();});
test('baseUrl rejects slashless HTTP URLs', async t => { let fetchCallCount = 0; const fetch: typeof globalThis.fetch = async () => { fetchCallCount++; return new Response('attacker'); };
const inputs = [ 'http:example.com/collect', 'https:example.com/collect', 'HTTP:example.com/collect', 'HTTPS:example.com/collect', ' http:example.com/collect', ' https:example.com/collect', '\0http:example.com/collect', '\0https:example.com/collect', '\thttp:example.com/collect', '\thttps:example.com/collect', '\nhttp:example.com/collect', '\nhttps:example.com/collect', '\rhttp:example.com/collect', '\rhttps:example.com/collect', 'h\tttp:example.com/collect', 'h\tttps:example.com/collect', 'http:\nexample.com/collect', 'https:\nexample.com/collect', 'http:\rexample.com/collect', 'https:\rexample.com/collect', String.raw`http:\example.com/collect`, String.raw`https:\example.com/collect`, String.raw`http:\\example.com/collect`, String.raw`https:\\example.com/collect`, String.raw`http:/\example.com/collect`, String.raw`https:/\example.com/collect`, String.raw`http:\/example.com/collect`, String.raw`https:\/example.com/collect`, ];
for (const input of inputs) { t.throws( () => { void ky(input, { baseUrl: 'https://trusted.test/api/', fetch, headers: { authorization: 'Bearer secret', }, }); }, { instanceOf: TypeError, message: '`input` url protocol must be followed by `//` when using `baseUrl`', }, ); }
let absoluteUrl: string | undefined; const absoluteFetch: typeof globalThis.fetch = async input => { absoluteUrl = new Request(input).url; return new Response('ok'); };
await t.notThrowsAsync(ky('http://example.test/collect', { baseUrl: 'https://trusted.test/api/', fetch: absoluteFetch, }).text()); t.is(absoluteUrl, 'http://example.test/collect');
await t.notThrowsAsync(ky('https://example.test/collect', { baseUrl: 'https://trusted.test/api/', fetch: absoluteFetch, }).text()); t.is(absoluteUrl, 'https://example.test/collect');
await Promise.resolve();
t.is(fetchCallCount, 0);});
test('baseUrl is exposed on normalized hook options', async t => { const server = await createHttpTestServer(); let receivedBaseUrl: URL | string | undefined;
19 unmodified lines