Skip to content

Commit db243a7

Browse files
authored
CORS Proxy: fetch() with credentials: "include" (#66)
1 parent 39ca88b commit db243a7

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

packages/php-wasm/web/src/lib/chunked-decoder.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,17 @@ export class ChunkedDecoderStream extends TransformStream<
4747
}
4848

4949
// Look for CRLF after chunk size
50+
if (buffer.length < chunkBytesNb + 2) {
51+
// Not enough data, let's wait for more
52+
return;
53+
}
5054
if (
51-
buffer.length < chunkBytesNb + 2 ||
5255
buffer[chunkBytesNb] !== 13 || // \r
5356
buffer[chunkBytesNb + 1] !== 10 // \n
5457
) {
55-
return;
58+
throw new Error(
59+
'Invalid chunk size format. Expected CRLF after chunk size'
60+
);
5661
}
5762

5863
// Parse the chunk size
@@ -88,12 +93,15 @@ export class ChunkedDecoderStream extends TransformStream<
8893
}
8994
} else if (state === 'SCAN_CHUNK_TRAILER') {
9095
if (buffer.length < 2) {
96+
// Not enough data, let's wait for more
9197
return;
9298
}
9399

94100
if (buffer[0] !== 13 || buffer[1] !== 10) {
95101
// \r\n
96-
throw new Error('Expected CRLF after chunk data');
102+
throw new Error(
103+
'Invalid chunk trailer format. Expected CRLF after chunk data'
104+
);
97105
}
98106

99107
buffer = buffer.slice(2);

packages/php-wasm/web/src/lib/fetch-with-cors-proxy.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ export async function fetchWithCorsProxy(
1818
try {
1919
return await fetch(request1);
2020
} catch {
21+
// If the developer has explicitly allowed the request to pass the
22+
// credentials headers with the X-Cors-Proxy-Allowed-Request-Headers header,
23+
// then let's include those credentials in the fetch() request.
24+
const headers = new Headers(request2.headers);
25+
const corsProxyAllowedHeaders =
26+
headers.get('x-cors-proxy-allowed-request-headers')?.split(',') ||
27+
[];
28+
const requestIntendsToPassCredentials =
29+
corsProxyAllowedHeaders.includes('authorization') ||
30+
corsProxyAllowedHeaders.includes('cookie');
31+
2132
const newRequest = await cloneRequest(request2, {
2233
url: `${corsProxyUrl}${requestObject.url}`,
34+
...(requestIntendsToPassCredentials && { credentials: 'include' }),
2335
});
2436

2537
return await fetch(newRequest, init);

packages/playground/php-cors-proxy/cors-proxy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
header('Access-Control-Allow-Origin: ' . $origin);
2020
header('Access-Control-Allow-Credentials: true');
2121
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
22-
header('Access-Control-Allow-Headers: Accept, Authorization, Content-Type, git-protocol, wp_blog, wp_install');
22+
header('Access-Control-Allow-Headers: Accept, Authorization, Content-Type, git-protocol, wp_blog, wp_install, x-cors-proxy-allowed-request-headers');
2323
}
2424
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
2525
header("Allow: GET, POST, OPTIONS");

0 commit comments

Comments
 (0)