Skip to content

Instantly share code, notes, and snippets.

@timcappalli
Created June 26, 2024 17:57
Show Gist options
  • Save timcappalli/58d3d345a4173effea63f2c5d31fbf3e to your computer and use it in GitHub Desktop.
Save timcappalli/58d3d345a4173effea63f2c5d31fbf3e to your computer and use it in GitHub Desktop.
WebAuthn PR 2040 - Related Origin Requests - Test Code
var psl = require('psl');
const MAX_LABELS = 5;
const WELL_KNOWN = {
origins: [
"https://shopping.sg",
"https://shopping.co.uk",
"https://otherdomain1.com",
"https://otherdomain2.com",
"https://otherdomain3.com",
"https://shopping.ie",
"https://otherdomain4.com",
"https://otherdomain5.com",
"https://otherdomain4.sg",
"https://shopping.ms"
]
}
function checkCurrentOrigin(callingOrigin) {
const labelsSeen = new Set();
console.log(`Calling Origin: ${callingOrigin}`);
for (const origin of WELL_KNOWN.origins) {
console.log("--------------")
console.log(`[0] START WK Origin: ${origin}`);
const url = new URL(origin);
console.log(`[1] WK URL: ${url}`);
let domain = url.hostname;
console.log(`[2] WK Domain: ${domain}`);
if (!domain) continue;
// grab eTLD+1 label
const parsed = psl.parse(domain)
console.log(`[3] WK Parsed: ${JSON.stringify(parsed)}`);
const label = parsed.sld;
console.log(`[4] WK Label: ${label}`);
if (labelsSeen.size >= MAX_LABELS && !labelsSeen.has(label)) {
console.log(`New label found, but limit exceeded, moving to next orign`);
continue
} ;
if (`${callingOrigin}` == `${url.origin}`) return true;
if (labelsSeen.has(label)) {
console.log(`[5] label found`);
console.log("moving to next origin...")
continue;
}
if (labelsSeen.size < MAX_LABELS) {
console.log(`[6] adding label: ${label}`);
labelsSeen.add(label);
}
console.log(`labels seen: ${Array.from(labelsSeen)}`);
console.log(`...got to end of loop`);
}
return false;
}
const callingOrigin = "https://shopping.ms"
console.log(checkCurrentOrigin(callingOrigin));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment