Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle URLs with weird characters
  • Loading branch information
regalijan committed Nov 2, 2024
1 parent e552593 commit 60eb7bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 9 additions & 4 deletions app/routes/short-links.tsx
Expand Up @@ -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";
Expand Down Expand Up @@ -103,7 +106,9 @@ export default function () {
<Td
onClick={() => {
navigator.clipboard.writeText(
`https://carcrushe.rs/${entry.path}`,
encodeURIComponent(
`https://carcrushe.rs/${entry.path}`,
),
);
alert("Link copied");
}}
Expand Down
@@ -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);

Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit 60eb7bc

Please sign in to comment.