diff options
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/(app)/post/[...path]/+server.ts | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/routes/(app)/post/[...path]/+server.ts b/src/routes/(app)/post/[...path]/+server.ts index 84df9a36..530bd2ed 100644 --- a/src/routes/(app)/post/[...path]/+server.ts +++ b/src/routes/(app)/post/[...path]/+server.ts @@ -1,12 +1,29 @@ import { PUBLIC_COINOS_URL as base } from "$env/static/public"; -import { auth } from "$lib/utils"; +import { auth, ipHeaders } from "$lib/utils"; +import { error } from "@sveltejs/kit"; + +// Relays a signed-in browser's POST to the API. The API trusts callers that +// arrive without a client address (its own node containers), so this must +// always pass the visitor's address along, and must only ever produce a plain +// API path: no absolute URLs, no "..", no empty segments. +const segment = /^[A-Za-z0-9_.~@:-]+$/; +const internalOnly = new Set(["confirm"]); export async function POST({ cookies, fetch, params, request }) { - const { path } = params; + const parts = params.path.split("/"); + if ( + !parts.every((p) => segment.test(p) && p !== "." && p !== "..") || + internalOnly.has(parts[0].toLowerCase()) + ) + error(404, "Not found"); - return fetch(`${base}/${path}`, { + return fetch(`${base}/${parts.join("/")}`, { method: "POST", body: JSON.stringify(await request.json()), - headers: { "Content-Type": "application/json", ...auth(cookies) }, + headers: { + "Content-Type": "application/json", + ...ipHeaders(), + ...auth(cookies), + }, }); } |
