Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 118 additions & 60 deletions apps/cloud/src/pages.ts

Large diffs are not rendered by default.

80 changes: 43 additions & 37 deletions apps/cloud/src/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ async function servePublic(request: Request, env: Env): Promise<Response> {
}

// Localify's own paths, on every URL. A link shared while the URL had a
// password still carries it after `#`, and redirecting to `/#` empties the
// fragment, which the browser would carry to the app. The access page shows
// the warning itself, so a link that carries the password takes one click.
// password still carries it after `#`, and redirecting to `<next>#` empties
// the fragment, which the browser would carry to the app. A link that
// carries the password signs in and acknowledges the warning at once
// (ADR 0036).
if (url.pathname === ACCESS_PATH) {
if (site.protected) return handleAccess(request, env, site, host);
await request.body?.cancel();
return redirect("/#");
return redirect(`${safeNext(url.searchParams.get("next"), host)}#`);
}
if (url.pathname === NOTICE_PATH) return handleNotice(request, env, site, host);

Expand Down Expand Up @@ -321,11 +322,12 @@ async function hasAccess(request: Request, env: Env, site: Site, host: string):

/**
* `GET /__localify/access` is the page a shared link opens, with the password
* after `#`. Never a redirect: the browser would carry the fragment to the
* app's page. It posts the password by itself once the warning is
* acknowledged; before that, it shows the warning and waits for `Continue`,
* which posts the acknowledgement with the password. `POST` checks the
* password, sets the cookies, and redirects to `next`.
* after `#` and the page to land on in `?next=`. Never a redirect: the browser
* would carry the fragment to the app's page. It posts the password by
* itself, with the warning's form token when this browser has not
* acknowledged the warning yet (ADR 0036). Opened without a password, it
* shows the warning's text, or the plain password page once acknowledged.
* `POST` checks the password, sets the cookies, and redirects to `next`.
*/
async function handleAccess(
request: Request,
Expand All @@ -334,21 +336,23 @@ async function handleAccess(
host: string,
): Promise<Response> {
const method = request.method;
const acknowledged =
request.headers.has(SKIP_WARNING) || (await hasNoticeCookie(request, env, site, host));
// The warning's text and a fresh form token, for a browser that has not
// acknowledged the warning.
const notice = async () =>
acknowledged
? undefined
: { login: site.ownerLogin, token: await signNoticeForm(setting(env, "COOKIE_KEY"), host) };

if (method === "GET" || method === "HEAD") {
const acknowledged =
request.headers.has(SKIP_WARNING) || (await hasNoticeCookie(request, env, site, host));
return passwordPage({
host,
next: "/",
next: safeNext(new URL(request.url).searchParams.get("next"), host),
status: 200,
method,
fromLink: true,
notice: acknowledged
? undefined
: {
login: site.ownerLogin,
token: await signNoticeForm(setting(env, "COOKIE_KEY"), host),
},
notice: await notice(),
});
}
if (method !== "POST") return redirect("/");
Expand All @@ -363,41 +367,43 @@ async function handleAccess(
next: "/",
status: 429,
error: "Too many attempts. Wait a minute, then try again.",
notice: await notice(),
});
}
const form = await readForm(request);
if (!form) return plain(413, "The form is too large.");
const next = safeNext(form.get("next"), host);
// The shared link's page posts the warning's `Continue` with the password.
// The acknowledgement holds whether or not the password is right, and only
// when it comes from this exact host.
const notice = (await acknowledgedHere(request, env, host, form.get("notice")))
? await noticeCookie(env, site, host)
: null;

const password = form.get("password") ?? "";
const valid =
site.passwordHash !== null &&
site.passwordSalt !== null &&
password !== "" &&
(await verifyPassword(password, { hash: site.passwordHash, salt: site.passwordSalt }));
let response: Response;
if (valid) {
const value = await signAccess(setting(env, "COOKIE_KEY"), {
host,
urlId: site.id,
version: site.passwordVersion,
});
response = redirect(next);
response.headers.append("set-cookie", accessCookieHeader(value));
} else {
response = passwordPage({
if (!valid) {
// The link's page posts by itself, so a wrong password acknowledges
// nothing: the page comes back with the warning when it was not seen.
return passwordPage({
host,
next,
error: "That password is not right. Check it and try again.",
error:
form.get("from") === "link"
? "The password in this link no longer works. Ask for a new link, or enter the password."
: "That password is not right. Check it and try again.",
notice: await notice(),
});
}
if (notice) response.headers.append("set-cookie", notice);
const value = await signAccess(setting(env, "COOKIE_KEY"), {
host,
urlId: site.id,
version: site.passwordVersion,
});
const response = redirect(next);
response.headers.append("set-cookie", accessCookieHeader(value));
// The warning's form token counts only from this exact host.
if (await acknowledgedHere(request, env, host, form.get("notice"))) {
response.headers.append("set-cookie", await noticeCookie(env, site, host));
}
return response;
}

Expand Down
66 changes: 51 additions & 15 deletions apps/cloud/test/public.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ describe("the password", () => {
expect(machineCalls()).toHaveLength(0);
});

it("lands a shared link on the page in ?next=, on this host only", async () => {
const cases: [string, string][] = [
["/orders/42?tab=items", "/orders/42?tab=items"],
["https://evil.example/", "/"],
["//evil.example/", "/"],
];
for (const [next, expected] of cases) {
const query = new URLSearchParams({ next }).toString();
const page = await visit(context, `https://${host()}/__localify/access?${query}`);
const html = await page.text();
expect(html, next).toContain(`name="next" value="${expected.replaceAll("&", "&amp;")}"`);
}
});

it("redirects only to a path on the same host", async () => {
const response = await signInWithPassword(host(), "https://evil.example/");
expect(response.headers.get("location")).toBe("/");
Expand Down Expand Up @@ -266,6 +280,8 @@ describe("the password", () => {
expect(response.status, method).toBe(303);
expect(response.headers.get("location"), method).toBe("/#");
}
const deep = await visit(context, `https://${host()}/__localify/access?next=%2Forders%2F42`);
expect(deep.headers.get("location")).toBe("/orders/42#");
expect(machineCalls()).toHaveLength(0);
});
});
Expand Down Expand Up @@ -816,21 +832,25 @@ describe("caching of the app's responses", () => {
});

describe("a shared link", () => {
it("shows the warning with the password filled in, for one click", async () => {
const page = await visit(context, `https://${host()}/__localify/access`, {
it("signs in by itself on a first visit, with the warning's token, and no click", async () => {
const page = await visit(context, `https://${host()}/__localify/access?next=%2Fdocs`, {
headers: navigation,
});
expect(page.status).toBe(200);
const html = await page.text();
// Without the fragment, or without scripts, the page is the warning with
// a password field.
expect(html).toContain("Before you continue");
expect(html).toContain("teo-goulois");
expect(html).toContain('href="mailto:abuse@localify.dev"');
expect(html).toContain('action="/__localify/access"');
expect(html).toContain('name="next" value="/docs"');
expect(html).toMatch(/name="notice" value="\d+\.[0-9a-f]{64}"/);
expect(html).toContain('name="password"');
expect(html).toContain("Signing in to");
expect(html).toContain("location.hash");
expect(html).toContain("history.replaceState");
expect(html).not.toContain("submit()");
expect(html).toContain("form.submit()");
const nonce = /<script nonce="([0-9a-f-]{36})">/.exec(html)?.[1];
expect(nonce).toBeDefined();
expect(page.headers.get("content-security-policy")).toContain(`script-src 'nonce-${nonce}'`);
Expand All @@ -839,19 +859,24 @@ describe("a shared link", () => {
expect(machineCalls()).toHaveLength(0);
});

async function postLink(origin: string, notice: string, password = PASSWORD): Promise<Response> {
async function postLink(
origin: string,
notice: string,
password = PASSWORD,
next = "/",
): Promise<Response> {
return visit(context, `https://${host()}/__localify/access`, {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded", origin },
body: new URLSearchParams({ next: "/", notice, password }).toString(),
body: new URLSearchParams({ next, notice, password, from: "link" }).toString(),
});
}

it("sets both cookies on Continue, and then opens the app", async () => {
it("sets both cookies, and then opens the app on the page in next", async () => {
const notice = await formToken(`https://${host()}/__localify/access`);
const response = await postLink(`https://${host()}`, notice);
const response = await postLink(`https://${host()}`, notice, PASSWORD, "/docs?page=2");
expect(response.status).toBe(303);
expect(response.headers.get("location")).toBe("/");
expect(response.headers.get("location")).toBe("/docs?page=2");
const cookies = response.headers.getSetCookie();
expect(cookies.map((cookie) => cookie.split("=")[0])).toEqual([ACCESS_COOKIE, NOTICE_COOKIE]);
const cookie = cookies.map((value) => value.split(";")[0]).join("; ");
Expand All @@ -874,16 +899,17 @@ describe("a shared link", () => {
]);
});

it("keeps the acknowledgement when the password is wrong", async () => {
it("acknowledges nothing when the password is wrong, and shows the warning again", async () => {
const notice = await formToken(`https://${host()}/__localify/access`);
const response = await postLink(`https://${host()}`, notice, "wrong");
const response = await postLink(`https://${host()}`, notice, "wrong", "/docs");
expect(response.status).toBe(401);
const cookies = response.headers.getSetCookie();
expect(cookies).toHaveLength(1);
expect(cookies[0]).toMatch(new RegExp(`^${NOTICE_COOKIE}=`));
expect(response.headers.getSetCookie()).toEqual([]);
const html = await response.text();
expect(html).toContain("Password required");
expect(html).not.toContain('name="notice"');
expect(html).toContain("Before you continue");
expect(html).toContain("The password in this link no longer works");
expect(html).toMatch(/name="notice" value="\d+\.[0-9a-f]{64}"/);
expect(html).toContain('name="next" value="/docs"');
expect(html).not.toContain("<script");
});

it("signs in by itself once the warning is acknowledged", async () => {
Expand All @@ -896,6 +922,16 @@ describe("a shared link", () => {
expect(html).toContain("Password required");
expect(html).not.toContain('name="notice"');
expect(html).toContain("form.submit()");

const wrong = await visit(context, `https://${host()}/__localify/access`, {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded", cookie },
body: "password=wrong&next=%2F",
});
const again = await wrong.text();
expect(again).toContain("Password required");
expect(again).toContain("That password is not right");
expect(again).not.toContain('name="notice"');
});
});

Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

- A public URL opens a project domain from another device, at `https://<slug>.localify.fyi`, behind a password by default. It needs a Localify Cloud account (GitHub sign-in) and the paid Pro plan: $7.99 a month or $79 a year (€7.99 or €79 in the EU and EEA), with a 7-day free trial, 2 machines serving at the same time and 10 public URLs. Add-ons, with an active Pro plan: +1 serving machine for $3 (€3) a month, +10 public URLs for $2 (€2) a month. Anyone with a GitHub account can sign up: a person runs `localify login`, then `localify subscribe` to start the trial; an agent never starts a checkout. `localify billing` opens the billing portal. The rest of Localify is free and needs no account. Pricing: https://localify.dev/pricing
- `localify login` prints a GitHub device code; the person enters it at https://github.com/login/device from any device, so an agent can sign in a remote machine without anyone at its screen.
- `localify public-url create <project-domain>` prints the URL, its password and a link that carries the password. `--wildcard` on a project's namespace (`<project>.localify`) also reaches every service and worktree, at `<slug>--<label>.localify.fyi`.
- `localify public-url create <project-domain>` prints the URL, its password and a link that carries the password. `--wildcard` on a project's namespace (`<project>.localify`) also reaches every service and worktree, at `<slug>--<label>.localify.fyi`. `localify public-url link <page>` gives the link that signs in and opens one page.
- Apps that check `Origin`, such as Better Auth, must also trust their own public URL in development, for example `https://<slug>.localify.fyi` and `https://<slug>--*.localify.fyi`: writes keep the public origin. Not every `*.localify.fyi` origin, which would let other people's public URLs post to the app's local domain.

## Docs
Expand Down
12 changes: 12 additions & 0 deletions changes/open-a-page-of-a-public-url-already-signed-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
type: new
link: docs/README.md
---

## Open any page of a public URL already signed in

A link with the password now signs a browser in with no click, even the first time it
opens the URL, and `localify public-url link https://admin.journal.localify/orders/42`
gives the link to one exact page. Coding agents get the same through `public_url_link`,
so an agent can put you on the page it means from your phone. The password, warning and
error pages of public URLs also look like Localify now, in light and dark.
9 changes: 5 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,11 @@ localify public-url list
- **Protected by default.** Each URL asks for a password, generated unless you pass
`--password`. A browser that enters it stays signed in for 7 days on that host; each
wildcard host asks once. The link with the password,
`https://<slug>.localify.fyi/__localify/access#<password>`, signs a browser in without
typing it. The password sits after `#`, so it never reaches a server. The machine
that set the password keeps it to give that link again, in the app and in
`public-url list --json`. `protect` sets a new password and signs every browser out.
`https://<slug>.localify.fyi/__localify/access#<password>`, signs a browser in with no
click and no typing. The password sits after `#`, so it never reaches a server. The
machine that set the password keeps it to give that link again, in the app and in
`public-url list --json`. `localify public-url link https://web.journal.localify/orders/42`
gives the link to one page: it signs in on the right public host and lands there. `protect` sets a new password and signs every browser out.
`unprotect`, or `--public` at creation, opens the URL to everyone.
- **Wildcard.** `--wildcard` on a project's namespace, such as `journal.localify`,
reaches every service and worktree under it: `journal--web.localify.fyi` opens
Expand Down
2 changes: 1 addition & 1 deletion docs/adr/0021-public-urls-are-protected-by-default.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
> **Amended.** [ADR 0026](0026-abuse-controls-and-shared-cookies-before-the-public-suffix-list.md) puts a warning page before the password, so a link that carries the password waits for one click.
> **Amended.** [ADR 0026](0026-abuse-controls-and-shared-cookies-before-the-public-suffix-list.md) puts a warning page before the password, so a link that carries the password waits for one click. [ADR 0036](0036-a-link-with-the-password-signs-in-without-a-click.md) removes that click again.

# Public URLs Are Protected by Default

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
> **Amended.** [ADR 0036](0036-a-link-with-the-password-signs-in-without-a-click.md) lets a link that carries the password sign in and acknowledge the warning without a click.

# Abuse Controls, and Shared Cookies Until the Public Suffix List

Opening public URLs to anyone who pays brings strangers under `localify.fyi`. Localify answers with controls on every public URL, and accepts one risk it cannot remove yet. This amends ADR 0022, which relied on everyone being invited, and ADR 0021, whose links that carry the password signed a browser in without a click.
Expand Down
11 changes: 11 additions & 0 deletions docs/adr/0036-a-link-with-the-password-signs-in-without-a-click.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# A Link With the Password Signs In Without a Click

A link that carries a public URL's password used to wait for one click on a browser's first visit to the host: ADR 0026 put the warning page before it, so an attacker could not protect a URL and send visitors straight to a fake page. In practice the person opening these links is almost always the owner, or someone the owner works with, opening a page an agent prepared on their phone or laptop. That click was the one step left between the link and the page. This amends ADR 0026.

**The link signs in and acknowledges the warning together.** `https://<host>/__localify/access#<password>` posts the password by itself, with the warning's form token, whether or not the browser has seen the warning. A right password sets both cookies and opens the app; a wrong one acknowledges nothing and shows the warning with a password field. The warning still comes first for every other visitor: an open URL, a link without the password, a URL typed by hand.

**The link lands on a page.** `?next=<path>` on the access path names the page to open after signing in, on that same host. `localify public-url link <page>`, and the MCP tool `public_url_link`, turn a local page such as `https://admin.louez.localify/orders/42` into that link on its public host, so an agent can put a person on the exact page it means.

**The pages say less.** The warning shows the host, the owner's GitHub login and one line asking to trust the sender; the rest of what ADR 0026 lists sits behind a `?` next to the heading. The password page shows the host and the field. Every Worker page uses Localify's look.

**The accepted risk.** Someone who protects a URL can send a link that opens a fake page with no warning. We accept it for the reasons ADR 0026 accepted shared cookies: every owner has paid with a card, is known by a GitHub account, and can be banned; every page the Worker serves links to `abuse@localify.dev`; and the password still decides who gets in. If phishing through links shows up, the link goes back to waiting for one click.
Loading
Loading