feat(web): let users set their own photo on the account page

Pick a PNG/JPEG/WebP file, crop it to a square on a canvas (drag, wheel,
slider or keyboard), export a 256x256 PNG and send it to Hub. Adds remove
photo, error states, RU/EN copy and privacy policy text about the stored
photo and 30-day backups.
This commit is contained in:
Loki 2026-10-24 21:48:05 +02:00
parent a4cae66336
commit 4c06a17d7e
10 changed files with 739 additions and 14 deletions

View file

@ -8,6 +8,8 @@ import {
ApiError,
requestEmailCode,
verifyEmailCode,
uploadAvatar,
removeAvatar,
} from "../src/api";
import { messages, detectLanguage } from "../src/i18n";
@ -173,3 +175,70 @@ test("email verification accepts only an authenticated session and keeps the sta
(error: unknown) => error instanceof ApiError && error.status === 400,
);
});
test("own avatars are same-origin content-hash paths only", () => {
const own = `/v1/avatars/${"a1".repeat(32)}`;
assert.equal(safeAvatar(own), own);
for (const value of [
"/v1/avatars/abc",
`/v1/avatars/${"A".repeat(64)}`,
`/v1/avatars/${"a".repeat(64)}/x`,
`//evil.example/v1/avatars/${"a".repeat(64)}`,
`https://evil.example/v1/avatars/${"a".repeat(64)}`,
"/v1/auth/session",
])
assert.equal(safeAvatar(value), null);
});
test("avatar upload sends the PNG with CSRF and keeps the Hub error code", async () => {
const own = `/v1/avatars/${"b2".repeat(32)}`;
let seen: { path: string; init: RequestInit } | undefined;
const png = new Blob([new Uint8Array([1, 2, 3])], { type: "image/png" });
const url = await uploadAvatar(png, "csrf-fixture", {
fetcher: async (path, init) => {
seen = { path, init };
return Response.json({ avatar_url: own });
},
});
assert.equal(url, own);
assert.ok(seen);
assert.equal(seen.path, "/v1/account/avatar");
assert.equal(seen.init.method, "POST");
assert.equal(seen.init.credentials, "same-origin");
assert.equal(seen.init.body, png);
const headers = new Headers(seen.init.headers);
assert.equal(headers.get("Content-Type"), "image/png");
assert.equal(headers.get("X-CSRF-Token"), "csrf-fixture");
await assert.rejects(
uploadAvatar(png, "c", {
fetcher: async () =>
Response.json({ error: "too_large" }, { status: 413 }),
}),
(error: unknown) =>
error instanceof ApiError &&
error.status === 413 &&
error.detail === "too_large",
);
await assert.rejects(
uploadAvatar(png, "c", {
fetcher: async () =>
Response.json({ avatar_url: "https://evil.example/x" }),
}),
);
});
test("avatar removal returns the provider avatar only when it is a safe URL", async () => {
const github = "https://avatars.githubusercontent.com/u/1?v=4";
let method = "";
const result = await removeAvatar("c", {
fetcher: async (_path, init) => {
method = String(init.method);
return Response.json({ avatar_url: github });
},
});
assert.equal(method, "DELETE");
assert.equal(result, github);
assert.equal(
await removeAvatar("c", {
fetcher: async () => Response.json({ avatar_url: "javascript:alert(1)" }),
}),
null,
);
});