Skip to content

Instantly share code, notes, and snippets.

@dotproto
Last active April 9, 2024 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotproto/b7492afe26b9eb49154eb4dd8c546f43 to your computer and use it in GitHub Desktop.
Save dotproto/b7492afe26b9eb49154eb4dd8c546f43 to your computer and use it in GitHub Desktop.
CORS Test (Chrome, Firefox) - This extension will begin performing HTTP requests against a bespoke test server immediately after installation. To view the results, open a devtools session for the extension's background context and look through the console output.
globalThis.browser ??= globalThis.chrome;
let testCounter = 0;
let corsTest = async (
{
client: {
forcePreflight = false,
mode = undefined,
},
server: {
cors = false,
},
} = {}
) => {
const options = {};
let url = new URL("https://dotproto-cors-test.glitch.me/script.js");
// Include timestamp to prevent cache hits
url.searchParams.set('time', Date.now());
let corsValue = 'false';
let corsColor = 'slategray';
if (cors) {
url.searchParams.set('cors', true);
corsValue = 'true';
corsColor = 'deepskyblue';
}
let modeValue = 'none';
if (mode) {
options.mode = mode;
modeValue = mode;
}
let modeColor = 'slategray';
switch (mode) {
case 'no-cors':
modeColor = 'orange';
break;
case 'cors':
modeColor = 'salmon';
break;
}
let preflightValue = false;
let preflightColor = 'slategray';
if (forcePreflight) {
options.headers = { 'unsafe-header': true };
preflightValue = true;
preflightColor = 'gold';
}
console.groupCollapsed(`Test ${(++testCounter).toString().padStart(2, '0')} - ${url} ${JSON.stringify(options)}`);
console.log(`Request --> CORS mode: %c${modeValue.padStart(7, ' ')}%c | force preflight: %c${preflightValue}`, `color: ${modeColor};`, `color: initial;`, `color: ${preflightColor}`);
console.log(`Response <-- CORS headers: %c${corsValue.padStart(5, ' ')}`, `color: ${corsColor}`);
console.log(`URL: ${url}`);
console.groupEnd();
let resultValue = 'SUCCESS';
let resultColor = 'aquamarine';
try {
await fetch(url, options);
} catch {
resultValue = 'ERROR';
resultColor = 'red';
}
console.log(`%c${resultValue}`, `color: ${resultColor}`);
await sleep();
}
browser.runtime.onInstalled.addListener(async () => {
let result;
// Set 1
await corsTest({
client: { mode: false, forcePreflight: false },
server: { cors: false },
});
await corsTest({
client: { mode: false, forcePreflight: true },
server: { cors: false },
});
await corsTest({
client: { mode: false, forcePreflight: false },
server: { cors: true },
});
await corsTest({
client: { mode: false, forcePreflight: true },
server: { cors: true },
});
// Set 2
await corsTest({
client: { mode: 'no-cors', forcePreflight: false },
server: { cors: false },
});
await corsTest({
client: { mode: 'no-cors', forcePreflight: true },
server: { cors: false },
});
await corsTest({
client: { mode: 'no-cors', forcePreflight: false },
server: { cors: true },
});
await corsTest({
client: { mode: 'no-cors', forcePreflight: true },
server: { cors: true },
});
// Set 3
await corsTest({
client: { mode: 'cors', forcePreflight: false },
server: { cors: false },
});
await corsTest({
client: { mode: 'cors', forcePreflight: true },
server: { cors: false },
});
await corsTest({
client: { mode: 'cors', forcePreflight: false },
server: { cors: true },
});
await corsTest({
client: { mode: 'cors', forcePreflight: true },
server: { cors: true },
});
});
async function sleep(time = 1000) {
return new Promise(resolve => {
setTimeout(() => resolve(), time);
});
}
{
"name": "CORS test",
"version": "1.0",
"manifest_version": 3,
"background": {
"scripts": ["background.js"],
"service_worker": "background.js"
},
"action": {},
"permissions": ["*://*/*"],
"host_permissions": ["*://*/*"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment