mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
fix(browser): keep malformed CDP response statuses unknown (#12296)
fix(browser): reject malformed CDP response statuses
This commit is contained in:
1 parent
c35e821db7
commit
2d524d8585
3 files changed
+61
-7
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
readCdpRequestWillBeSent,
|
||||
readCdpResponseReceived,
|
||||
} from "./CdpNetworkEventFacts.js";
|
||||
|
||||
const invalidStatus = {
|
||||
ok: false,
|
||||
error: { _tag: "InvalidCdpResponseStatus" },
|
||||
};
|
||||
|
||||
it.each([
|
||||
[undefined, { ok: true, value: null }],
|
||||
[null, invalidStatus],
|
||||
["200", invalidStatus],
|
||||
[0, invalidStatus],
|
||||
[99, invalidStatus],
|
||||
[200.5, invalidStatus],
|
||||
[1000, invalidStatus],
|
||||
[100, { ok: true, value: 100 }],
|
||||
[302, { ok: true, value: 302 }],
|
||||
[600, { ok: true, value: 600 }],
|
||||
[999, { ok: true, value: 999 }],
|
||||
] as const)(
|
||||
"reads CDP response status %s without losing other response facts",
|
||||
(status, expectedStatus) => {
|
||||
const fields = {
|
||||
url: "https://fixture.example/response",
|
||||
mimeType: "application/json",
|
||||
encodedDataLength: 0,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
};
|
||||
const reported = status === undefined ? fields : { ...fields, status };
|
||||
const normal = readCdpResponseReceived({
|
||||
requestId: "fixture",
|
||||
response: reported,
|
||||
});
|
||||
const { redirect } = readCdpRequestWillBeSent({
|
||||
requestId: "fixture",
|
||||
redirectResponse: reported,
|
||||
});
|
||||
expect(normal.response).toMatchObject({
|
||||
source: reported,
|
||||
url: { source: fields.url },
|
||||
status: expectedStatus,
|
||||
mimeType: fields.mimeType,
|
||||
encodedDataLength: fields.encodedDataLength,
|
||||
headers: fields.headers,
|
||||
});
|
||||
expect(redirect).toEqual({ kind: "response", response: normal.response });
|
||||
},
|
||||
);
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
browserResponseStatusSchema,
|
||||
observeBrowserUrl,
|
||||
type BrowserUrlObservation,
|
||||
type SanitizedBrowserUrl,
|
||||
@@ -59,13 +60,13 @@ const readCdpResponseFacts = (
|
||||
};
|
||||
};
|
||||
|
||||
/** Omission is unknown; a supplied non-number is a protocol failure. */
|
||||
/** Omission is unknown. A malformed supplied status is a protocol failure. */
|
||||
const readResponseStatus = (value: unknown): CdpResponseFacts["status"] => {
|
||||
if (value === undefined) return ok(null);
|
||||
const status = numberValue(value);
|
||||
return status === undefined
|
||||
? err({ _tag: "InvalidCdpResponseStatus" })
|
||||
: ok(status);
|
||||
const status = browserResponseStatusSchema.safeParse(value);
|
||||
return status.success
|
||||
? ok(status.data)
|
||||
: err({ _tag: "InvalidCdpResponseStatus" });
|
||||
};
|
||||
|
||||
type ScopedCdpResponse =
|
||||
|
||||
@@ -1123,7 +1123,7 @@ describe("redirect origin policy", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps response facts with an unknown optional status without changing request identity", async () => {
|
||||
it("keeps response facts when the reported status is malformed", async () => {
|
||||
const browser = await startFakeCdpBrowser({
|
||||
commandEvents(command, origin) {
|
||||
if (command.method !== "Network.enable") return undefined;
|
||||
@@ -1144,7 +1144,7 @@ it("keeps response facts with an unknown optional status without changing reques
|
||||
requestId: "partial-response",
|
||||
response: {
|
||||
url: `${origin}/reported-response`,
|
||||
status: "malformed",
|
||||
status: 200.5,
|
||||
mimeType: "application/json",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
|
||||
Reference in new issue
Block a user