Skip to content
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
160 changes: 147 additions & 13 deletions scripts/product-proof-cloud-playwright.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const routeNetworkFailureLifecycleStages = new Set([
'correlated-cdp-complete-only',
'correlated-mediated-complete-only',
'correlated-both-complete',
'browser-managed-preflight-before-response',
'browser-managed-preflight-response-only',
Comment thread
coderabbitai[bot] marked this conversation as resolved.
'multiple-open',
]);
const routeNetworkOpenLifecycleStages = new Set([
Expand All @@ -51,8 +53,14 @@ const routeNetworkOpenLifecycleStages = new Set([
'correlated-both-responses',
'correlated-cdp-complete-only',
'correlated-mediated-complete-only',
'browser-managed-preflight-before-response',
'browser-managed-preflight-response-only',
'multiple-open',
]);
const browserManagedPreflightLifecycleStages = new Set([
'browser-managed-preflight-before-response',
'browser-managed-preflight-response-only',
]);
const stagingSupabaseOrigin = 'https://geskfgwohbzmcmmjtuxm.supabase.co';
const stagingSupabaseProjectRef = 'geskfgwohbzmcmmjtuxm';
export const PRODUCT_PROOF_SUPABASE_CLIENT_INFO = 'supabase-js-web/2.101.1';
Expand Down Expand Up @@ -420,6 +428,7 @@ function normalizeProductProofRouteNetworkFailureDiagnostic(value) {
const policyLabel = value.policy_label;
const mediatedHeadersPending = lifecycleStage === 'mediated-headers-pending';
const pendingCdpCorrelation = lifecycleStage === 'pending-cdp-correlation';
const browserManagedPreflight = browserManagedPreflightLifecycleStages.has(lifecycleStage);
const multipleOpen = lifecycleStage === 'multiple-open';
if (
!['cdp', 'mediated', 'multiple'].includes(adapter) ||
Expand All @@ -428,6 +437,7 @@ function normalizeProductProofRouteNetworkFailureDiagnostic(value) {
(typeof policyLabel !== 'string' || !safeLabelPattern.test(policyLabel))) ||
(mediatedHeadersPending && adapter !== 'mediated') ||
(pendingCdpCorrelation && (adapter !== 'cdp' || policyLabel !== null)) ||
(browserManagedPreflight && adapter !== 'cdp') ||
(multipleOpen && (adapter !== 'multiple' || policyLabel !== null)) ||
(!mediatedHeadersPending && !pendingCdpCorrelation && !multipleOpen &&
(adapter === 'multiple' || policyLabel === null)) ||
Expand Down Expand Up @@ -486,6 +496,8 @@ function normalizeProductProofRouteNetworkFailureDiagnostic(value) {
openLifecycleStage === 'mediated-headers-pending';
const openPendingCdpCorrelation =
openLifecycleStage === 'pending-cdp-correlation';
const openBrowserManagedPreflight =
browserManagedPreflightLifecycleStages.has(openLifecycleStage);
if (
!['cdp', 'mediated'].includes(openAdapter) ||
!routeNetworkOpenLifecycleStages.has(openLifecycleStage) ||
Expand All @@ -496,6 +508,7 @@ function normalizeProductProofRouteNetworkFailureDiagnostic(value) {
(openMediatedHeadersPending && openAdapter !== 'mediated') ||
(openPendingCdpCorrelation &&
(openAdapter !== 'cdp' || openPolicyLabel !== null)) ||
(openBrowserManagedPreflight && openAdapter !== 'cdp') ||
(!openMediatedHeadersPending &&
!openPendingCdpCorrelation &&
openPolicyLabel === null)
Expand Down Expand Up @@ -1657,6 +1670,90 @@ function normalizeCdpResourceType(value) {
return value.toLowerCase();
}

export const PRODUCT_PROOF_PLAYWRIGHT_MANAGED_PREFLIGHT_CONTRACT = Object.freeze({
initiator_type: 'preflight',
method: 'OPTIONS',
playwright_version: '1.62.1',
request_resource_type: 'other',
response_resource_type: 'preflight',
response_status: 204,
});

const cdpIdentityHeaderNames = new Set([
'accept-profile',
'access-control-request-headers',
'access-control-request-method',
'content-profile',
'content-type',
'origin',
'prefer',
'range',
'range-unit',
]);
const cdpCredentialHeaderNames = new Set(['apikey', 'authorization', 'cookie']);
const cdpForbiddenHeaderNames = new Set([
'if-match',
'if-modified-since',
'if-none-match',
'if-range',
'if-unmodified-since',
'proxy-authorization',
'set-cookie',
'x-access-token',
'x-api-key',
'x-auth-token',
'x-http-method-override',
'x-method-override',
'x-session-credential',
'x-supabase-api-key',
'x-supabase-authorization',
]);

function normalizeCdpRequestHeaderPosture(value) {
if (value === undefined) {
return Object.freeze({
credentialHeaderPresent: false,
forbiddenHeaderPresent: false,
headers: Object.freeze({}),
});
}
if (!isPlainObject(value) || Object.keys(value).length > 256) fail('cdp_event_invalid');
const headers = {};
const seen = new Set();
let credentialHeaderPresent = false;
let forbiddenHeaderPresent = false;
for (const [rawName, rawValue] of Object.entries(value)) {
if (
typeof rawName !== 'string' ||
rawName.length < 1 ||
typeof rawValue !== 'string' ||
/[\r\n\u0000]/u.test(rawName) ||
/[\r\n\u0000]/u.test(rawValue)
) {
fail('cdp_event_invalid');
}
const name = rawName.toLowerCase();
if (seen.has(name)) fail('cdp_event_invalid');
seen.add(name);
if (cdpCredentialHeaderNames.has(name)) credentialHeaderPresent = true;
if (cdpForbiddenHeaderNames.has(name)) forbiddenHeaderPresent = true;
if (cdpIdentityHeaderNames.has(name)) {
if (
Buffer.byteLength(rawName, 'utf8') > 256 ||
Buffer.byteLength(rawValue, 'utf8') > 8192
) {
fail('cdp_event_invalid');
}
headers[name] = rawValue;
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return Object.freeze({
credentialHeaderPresent,
forbiddenHeaderPresent,
headers: Object.freeze(headers),
});
}

function fixedNetworkFailureClass({ blockedReason, canceled, corsErrorStatus, errorText }) {
if (isPlainObject(corsErrorStatus)) return 'cors';
if (typeof blockedReason === 'string' && blockedReason.length > 0) return 'blocked';
Expand Down Expand Up @@ -3180,35 +3277,72 @@ export async function attachProductProofCdpObserver(contextObserver, page, asset
session.on(
'Network.requestWillBeSent',
guardListener('cdp_request_listener_failed', (event) => {
assetVerifier.requestWillBeSent(event);
const request = event.request;
const requestId = event.requestId;
const resourceType = event.type;
const initiator = event.initiator;
const redirectResponse = event.redirectResponse;
const method = request?.method;
const postData = request?.postData ?? null;
const url = request?.url;
const headerPosture = normalizeCdpRequestHeaderPosture(request?.headers);
const initiatorType = initiator?.type === 'preflight' ? 'preflight' : 'other';
const redirected = redirectResponse !== undefined;
assetVerifier.requestWillBeSent({
...(redirected ? { redirectResponse: {} } : {}),
request: { method, postData, url },
requestId,
type: resourceType,
});
emit({
headers: {},
credentialHeaderPresent: headerPosture.credentialHeaderPresent,
forbiddenHeaderPresent: headerPosture.forbiddenHeaderPresent,
headers: headerPosture.headers,
initiatorType,
kind: 'cdp-request',
method: event.request?.method,
postData: event.request?.postData ?? null,
requestId: event.requestId,
resourceType: normalizeCdpResourceType(event.type),
url: event.request?.url,
method,
postData,
redirected,
requestId,
resourceType: normalizeCdpResourceType(resourceType),
url,
});
}),
);
session.on(
'Network.responseReceived',
guardListener('cdp_response_listener_failed', (event) => {
assetVerifier.responseReceived(event);
const requestId = event.requestId;
const response = event.response;
const resourceType = event.type;
const fromDiskCache = response?.fromDiskCache === true;
const fromPrefetchCache = response?.fromPrefetchCache === true;
const fromServiceWorker = response?.fromServiceWorker === true;
const status = response?.status;
const url = response?.url;
assetVerifier.responseReceived({
requestId,
response: { fromDiskCache, fromPrefetchCache, fromServiceWorker, status, url },
type: resourceType,
});
emit({
fromDiskCache,
fromPrefetchCache,
fromServiceWorker,
kind: 'cdp-response',
requestId: event.requestId,
resourceType: normalizeCdpResourceType(event.type),
status: event.response?.status,
url: event.response?.url,
requestId,
resourceType: normalizeCdpResourceType(resourceType),
status,
url,
});
}),
);
session.on(
'Network.requestServedFromCache',
guardListener('cdp_cache_listener_failed', (event) => {
assetVerifier.requestServedFromCache(event);
const requestId = event.requestId;
assetVerifier.requestServedFromCache({ requestId });
emit({ kind: 'cdp-served-from-cache', requestId });
}),
);
session.on(
Expand Down
Loading