diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/hooks.server.ts | 2 | ||||
| -rw-r--r-- | src/lib/utils.ts | 2 | ||||
| -rw-r--r-- | src/routes/(app)/post/[...path]/+server.ts | 25 |
3 files changed, 23 insertions, 6 deletions
diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 5e0d9e90..a7e97394 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -6,7 +6,7 @@ setIpGetter(() => ipStore.getStore()); export const handle: Handle = async ({ event, resolve }) => { // Pages render client-side only, so the settings layout's login redirect - // never reaches crawlers; Google indexed /settings/shopify as an empty + // never reaches crawlers; Google indexed a settings page as an empty // "Untitled" page. Redirect signed-out visitors on the server instead. const { pathname } = event.url; if ( diff --git a/src/lib/utils.ts b/src/lib/utils.ts index b6bbdb53..15ffb7f3 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -27,7 +27,7 @@ export const setIpGetter = (fn: () => string | undefined) => { getIp = fn; }; -const ipHeaders = (): Record<string, string> => { +export const ipHeaders = (): Record<string, string> => { const ip = getIp?.(); return ip ? { "cf-connecting-ip": ip } : {}; }; 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), + }, }); } |
