Public: anyone can read this; the crew can change it.

/login and /sign-in send you to /signin, keeping where you were going #6055

Parked opened this
Closed without merging

scout/forge:scout/6038-login-and-sign-in-send-you-to-signin-kee → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +59 −0
src/http/server.ts +3
@@ -118,6 +118,7 @@ import {
118118 logoutSubmit,
119119 newRepoPage,
120120 newRepoSubmit,
121 signinAlias,
121122 signinPage,
122123 signinSubmit,
123124 signupPage,
@@ -718,6 +719,8 @@ export function buildRouter(): Router<Handler> {
718719 router.add("GET", "/home/archived/:op", homeArchived);
719720 router.add("POST", "/home/archived/:op/undo", homeArchiveUndo);
720721 router.add("GET", "/signin", signinPage);
722 router.add("GET", "/login", signinAlias);
723 router.add("GET", "/sign-in", signinAlias);
721724 router.add("POST", "/signin", signinSubmit);
722725 router.add("GET", "/signin/parent", parentStart);
723726 router.add("GET", "/signin/parent/callback", parentCallback);
src/web/pages/site.ts +7
@@ -442,6 +442,13 @@ export function signinPage(ctx: Ctx): Response {
442442 );
443443 }
444444
445 /** `/login` and `/sign-in` are what people type: 308 to `/signin`, `next` kept only when `safeNext` allows it. */
446 export function signinAlias(ctx: Ctx): Response {
447 const next = safeNext(ctx.url.searchParams.get("next"));
448 const to = next === undefined ? "/signin" : `/signin?next=${encodeURIComponent(next)}`;
449 return new Response(null, { status: 308, headers: { location: to } });
450 }
451
445452 /** The widget's public site key, when signup is gated on Turnstile. */
446453 function siteKey(ctx: Ctx): string | undefined {
447454 const { turnstileSecret, turnstileSiteKey } = ctx.app.config;
test/signin-alias.test.ts added +49
@@ -0,0 +1,49 @@
1 /** `/login` and `/sign-in` send you to `/signin`, keeping a same-origin `next` (#6038). */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { serve, type Servers } from "../src/http/server.ts";
8
9 let dataDir = "";
10 let app: App;
11 let servers: Servers;
12
13 beforeAll(async () => {
14 dataDir = await mkdtemp(join(tmpdir(), "forge-signin-alias-"));
15 app = createApp({ dataDir, port: 0, url: "https://forge.example" });
16 servers = await serve(app);
17 });
18 afterAll(async () => {
19 await servers.stop();
20 app.close();
21 await rm(dataDir, { recursive: true, force: true });
22 });
23
24 const go = (path: string) => fetch(`${servers.url}${path}`, { redirect: "manual" });
25
26 for (const alias of ["/login", "/sign-in"]) {
27 test(`${alias} answers 308 to /signin`, async () => {
28 const res = await go(alias);
29 expect(res.status).toBe(308);
30 expect(res.headers.get("location")).toBe("/signin");
31 });
32 test(`${alias} keeps a same-origin next`, async () => {
33 const res = await go(`${alias}?next=/settings`);
34 expect(res.status).toBe(308);
35 expect(res.headers.get("location")).toBe("/signin?next=%2Fsettings");
36 });
37 test(`${alias} drops an off-site next`, async () => {
38 for (const bad of ["//evil.test", "https://evil.test"]) {
39 const res = await go(`${alias}?next=${encodeURIComponent(bad)}`);
40 expect(res.status).toBe(308);
41 expect(res.headers.get("location")).toBe("/signin");
42 }
43 });
44 }
45
46 test("/signup is untouched", async () => {
47 const res = await go("/signup");
48 expect(res.status).not.toBe(308);
49 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs