Skip to content
Myr

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

2 files+95 added−5 removed

modified:source/core/Ky.ts+16 added-5 removed
81 unmodified lines
82 };83};8485const objectToString = Object.prototype.toString;86const leadingC0ControlOrSpacePattern = /^[\0-\u0020]+/g;87const asciiTabOrNewLinePattern = /[\t\n\r]/g;88const schemePattern = /^[a-z][\d+.a-z-]*:/i;89const malformedHttpProtocolPattern = /^https?:(?!\/\/)/i;9091const isRequestInstance = (value: unknown): value is Request =>92 value instanceof globalThis.Request || objectToString.call(value) === '[object Request]';93
2 unmodified lines
96// body consumption, `json()` decoration, and any enabled stream features.97const isResponseInstance = (value: unknown): value is Response =>98 value instanceof globalThis.Response || objectToString.call(value) === '[object Response]';99100const isAbsoluteInput = (input: string): boolean =>101 schemePattern.test(input);102103const normalizeInputForProtocolCheck = (input: string): string =>104 input.replaceAll(leadingC0ControlOrSpacePattern, '').replaceAll(asciiTabOrNewLinePattern, '');105106const cloneSearchParametersForInitHook = (searchParameters: SearchParamsOption | undefined): SearchParamsOption | undefined => {107 if (Array.isArray(searchParameters)) {108 return searchParameters.map(parameter => [...parameter]) as SearchParamsOption;109 }
276 unmodified lines
386 this.#input = `${normalizedPrefix}/${normalizedInput}`;387 }388389 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);391385 if (!absoluteInput) {392 if (malformedHttpProtocolPattern.test(normalizedInput)) {393 throw new TypeError('`input` url protocol must be followed by `//` when using `baseUrl`');394 }395396 if (!isAbsoluteInput(normalizedInput)) {397 this.#input = new URL(this.#input, (new Request(this.#options.baseUrl)).url);398 }399 }400 }
751 unmodified lines
modified:test/base-url.ts+79 added