Skip to content
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

fix: throw invalid argument for header #2246

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 67 additions & 18 deletions src/bidiMapper/modules/network/NetworkProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export class NetworkProcessor {
NetworkProcessor.parseUrlString(params.url);
}

if (params.headers) {
NetworkProcessor.validateHeaders(params.headers);
}

const request = this.#getBlockedRequestOrFail(networkId, [
Network.InterceptPhase.BeforeRequestSent,
]);
Expand All @@ -94,12 +98,20 @@ export class NetworkProcessor {

// TODO: Set / expand.
// ; Step 9. cookies
await request.continueRequest({
url,
method,
headers,
postData: getCdpBodyFromBiDiBytesValue(body),
});
try {
await request.continueRequest({
url,
method,
headers,
postData: getCdpBodyFromBiDiBytesValue(body),
});
} catch (error) {
// https://source.chromium.org/chromium/chromium/src/+/main:content/browser/devtools/protocol/fetch_handler.cc;l=169
if ((error as any)?.message.includes('Invalid header')) {
throw new InvalidArgumentException('Tried setting invalid header');
}
throw error;
}

return {};
}
Expand All @@ -109,6 +121,10 @@ export class NetworkProcessor {
): Promise<EmptyResult> {
const {request: networkId, statusCode, reasonPhrase, headers} = params;

if (params.headers) {
NetworkProcessor.validateHeaders(params.headers);
}

const responseHeaders: Protocol.Fetch.HeaderEntry[] | undefined =
cdpFetchHeadersFromBidiNetworkHeaders(headers);

Expand Down Expand Up @@ -212,15 +228,17 @@ export class NetworkProcessor {
request: networkId,
} = params;

if (params.headers) {
NetworkProcessor.validateHeaders(params.headers);
}

// TODO: Step 6
// https://w3c.github.io/webdriver-bidi/#command-network-continueResponse

const responseHeaders: Protocol.Fetch.HeaderEntry[] | undefined =
cdpFetchHeadersFromBidiNetworkHeaders(headers);

// TODO: Set / expand.
// ; Step 10. cookies
// ; Step 11. credentials
const request = this.#getBlockedRequestOrFail(networkId, [
Network.InterceptPhase.BeforeRequestSent,
Network.InterceptPhase.ResponseStarted,
Expand All @@ -238,22 +256,29 @@ export class NetworkProcessor {
return {};
}

// If we con't modify the response
// Just continue the request
// If we don't modify the response
// just continue the request
if (!body && !headers) {
await request.continueRequest();
return {};
}

const responseCode = statusCode ?? request.statusCode ?? 200;

await request.provideResponse({
responseCode,
responsePhrase,
responseHeaders,
body: getCdpBodyFromBiDiBytesValue(body),
});

try {
await request.provideResponse({
responseCode,
responsePhrase,
responseHeaders,
body: getCdpBodyFromBiDiBytesValue(body),
});
} catch (error) {
// https://source.chromium.org/chromium/chromium/src/+/main:content/browser/devtools/protocol/fetch_handler.cc;l=169
if ((error as any)?.message.includes('Invalid header')) {
throw new InvalidArgumentException('Tried setting invalid header');
}
throw error;
}
return {};
}

Expand Down Expand Up @@ -300,11 +325,35 @@ export class NetworkProcessor {
return request;
}

/**
* Validate https://fetch.spec.whatwg.org/#header-value
*/
static validateHeaders(headers: Network.Header[]) {
for (const header of headers) {
let headerValue: string;
if (header.value.type === 'string') {
headerValue = header.value.value;
} else {
headerValue = atob(header.value.value);
}

if (
headerValue !== headerValue.trim() ||
headerValue.includes('\n') ||
headerValue.includes('\0')
) {
throw new InvalidArgumentException(
`Header value '${headerValue}' is not acceptable value`
);
}
}
}

/**
* Attempts to parse the given url.
* Throws an InvalidArgumentException if the url is invalid.
*/
static parseUrlString(url: string): URL {
static parseUrlString(url: string) {
try {
return new URL(url);
} catch (error) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading