CS Fundamentals Trainer · JavaScript · Security
Predict the output: three ways to check a message's origin.
const TRUSTED = 'https://app.example.com';
const looseCheck = (origin) => origin.startsWith(TRUSTED);
const regexCheck = (origin) => /https:\/\/app.example.com/.test(origin);
const strictCheck = (origin) => origin === TRUSTED;
for (const origin of ['https://app.example.com', 'https://app.example.com.evil.net', 'https://appxexample.com']) {
console.log(looseCheck(origin), regexCheck(origin), strictCheck(origin));
}
Difficulty: medium