From 60eb7bca6478cd9cbfa43ae510d999a2d3fefef6 Mon Sep 17 00:00:00 2001 From: Regalijan Date: Sat, 2 Nov 2024 00:46:02 -0400 Subject: [PATCH] Handle URLs with weird characters --- app/routes/short-links.tsx | 13 +++++++++---- functions/api/short-links/{[id].ts => [[id]].ts} | 8 ++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) rename functions/api/short-links/{[id].ts => [[id]].ts} (86%) diff --git a/app/routes/short-links.tsx b/app/routes/short-links.tsx index a245dc8..85f0372 100644 --- a/app/routes/short-links.tsx +++ b/app/routes/short-links.tsx @@ -54,9 +54,12 @@ export default function () { const toast = useToast(); async function deleteLink(path: string) { - const deleteResp = await fetch(`/api/short-links/${path}`, { - method: "DELETE", - }); + const deleteResp = await fetch( + `/api/short-links/${encodeURIComponent(path)}`, + { + method: "DELETE", + }, + ); if (!deleteResp.ok) { let error = "Unknown error"; @@ -103,7 +106,9 @@ export default function () { { navigator.clipboard.writeText( - `https://carcrushe.rs/${entry.path}`, + encodeURIComponent( + `https://carcrushe.rs/${entry.path}`, + ), ); alert("Link copied"); }} diff --git a/functions/api/short-links/[id].ts b/functions/api/short-links/[[id]].ts similarity index 86% rename from functions/api/short-links/[id].ts rename to functions/api/short-links/[[id]].ts index b04bac3..c419738 100644 --- a/functions/api/short-links/[id].ts +++ b/functions/api/short-links/[[id]].ts @@ -1,7 +1,7 @@ import { jsonError } from "../../common.js"; export async function onRequestDelete(context: RequestContext) { - const path = context.params.id; + const path = decodeURIComponent(context.params.id as string); if (typeof path !== "string") return jsonError("Invalid path", 400); @@ -42,7 +42,11 @@ export async function onRequestPatch(context: RequestContext) { await context.env.D1.prepare( "UPDATE short_links SET path = ? WHERE path = ? AND user = ?;", ) - .bind(path, context.params.id, context.data.current_user.id) + .bind( + path, + decodeURIComponent(context.params.id as string), + context.data.current_user.id, + ) .run(); return new Response(null, {