A cookie's SameSite setting decides whether the browser sends it with requests that start on another site, which is what CSRF relies on.
What to build
Decide, for each request, whether the browser sends a cookie with the given SameSite setting.
Requirements
- Each line is
setting secure kind: setting is Strict, Lax or None; secure is yes or no; kind issame-site,cross-link(a top-level GET navigation from another site, such as clicking a link), orcross-post(any other cross-site request, such as a form POST or an image). - A cookie with SameSite=None but not Secure is rejected by the browser: print
rejected (None needs Secure). - Otherwise: same-site requests always send it; Strict never sends on cross-site requests; Lax sends only on cross-link; None sends on everything.
- Print
sentornot sent.
Input
One line per question.
Output
One line per question.
Constraints
1 to 30 lines, all valid.
Example
Input:
Lax yes cross-post
Lax yes cross-link
Output:
not sent
sent
Why it works
CSRF needs the login cookie on a request that started on another site; Lax and Strict stop it on cross-site posts, which is why they protect against most forged form submissions, while None restores the old behaviour.
Examples
Example 1 · Input
Lax yes cross-post Lax yes cross-link
Expected output
not sent sent
Example 2 · Input
Strict yes cross-link None yes cross-post
Expected output
not sent sent
Hints
Hint 1
Handle the None-without-Secure case first.
Solution
Show one correct solution (try it yourself first)
let line = prompt();
while (line !== null) {
const [setting, secure, kind] = line.trim().split(/\s+/);
let answer;
if (setting === "None" && secure !== "yes") answer = "rejected (None needs Secure)";
else if (kind === "same-site") answer = "sent";
else if (setting === "Strict") answer = "not sent";
else if (setting === "Lax") answer = kind === "cross-link" ? "sent" : "not sent";
else answer = "sent";
console.log(answer);
line = prompt();
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step