From 9f4790326fe8c7625f4678d91b9288c3542d1d57 Mon Sep 17 00:00:00 2001 From: Xylight <80978739+Xyphyn@users.noreply.github.com> Date: Wed, 25 Jun 2025 19:01:00 +0000 Subject: [PATCH] fix: improper route gating (#596) --- src/routes/comment/[instance]/+page.ts | 6 +++++- src/routes/inbox/+page.ts | 3 ++- src/routes/moderation/+page.ts | 7 +++---- src/routes/post/[instance]/+page.ts | 22 +++++++++------------- src/routes/profile/+layout.ts | 3 +++ src/routes/saved/+page.ts | 4 +++- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/routes/comment/[instance]/+page.ts b/src/routes/comment/[instance]/+page.ts index e46a04b7..34a426bf 100644 --- a/src/routes/comment/[instance]/+page.ts +++ b/src/routes/comment/[instance]/+page.ts @@ -1,3 +1,4 @@ +import { resolveRoute } from '$app/paths' import { instance } from '$lib/instance.svelte' import { redirect } from '@sveltejs/kit' @@ -5,6 +6,9 @@ export function load({ params }) { // If you somehow got to /comment/instance, it likely means you passed in an ID, not an instance. redirect( 302, - `/comment/${encodeURIComponent(instance.data)}/${params.instance}`, + resolveRoute('/comment/[instance]/[id]', { + instance: instance.data.toLowerCase(), + id: params.instance, + }), ) } diff --git a/src/routes/inbox/+page.ts b/src/routes/inbox/+page.ts index 4a3044d2..a5d9f5b4 100644 --- a/src/routes/inbox/+page.ts +++ b/src/routes/inbox/+page.ts @@ -7,11 +7,12 @@ import { generalizePrivateMessage, } from '$lib/lemmy/inbox.js' import { ReactiveState } from '$lib/promise.svelte.js' +import { error } from '@sveltejs/kit' type InboxFeedType = 'replies' | 'mentions' | 'messages' | 'all' export async function load({ url, fetch }) { - if (!profile.data.jwt) return + if (!profile.data.jwt) error(401) const type: InboxFeedType = (url.searchParams.get('type') as InboxFeedType) || 'all' diff --git a/src/routes/moderation/+page.ts b/src/routes/moderation/+page.ts index 2cf4cb88..9d80bd28 100644 --- a/src/routes/moderation/+page.ts +++ b/src/routes/moderation/+page.ts @@ -7,18 +7,17 @@ import { generalizePrivateMessageReport, } from '$lib/lemmy/report.js' import { ReactiveState } from '$lib/promise.svelte' +import { error } from '@sveltejs/kit' type ReportListType = 'unread' | 'all' export async function load({ url, fetch }) { + if (!profile.data.jwt) error(401) + const page = Number(url.searchParams.get('page')) || 1 const type: ReportListType = (url.searchParams.get('type') as ReportListType) || 'unread' - const jwt = profile.data?.jwt - - if (!jwt) return { type: type, page: page } - // can they please // make a sane, consistent, and simple API for once // 3 separate requests? really? diff --git a/src/routes/post/[instance]/+page.ts b/src/routes/post/[instance]/+page.ts index dc0ea7d1..b26cf07a 100644 --- a/src/routes/post/[instance]/+page.ts +++ b/src/routes/post/[instance]/+page.ts @@ -1,21 +1,17 @@ +import { resolveRoute } from '$app/paths' import { profile } from '$lib/auth.svelte.js' import { error, redirect } from '@sveltejs/kit' -export function load({ params, url }) { +export function load({ params }) { if (Number(params.instance)) { - const split = url.pathname.split('/') - - split.splice( - 2, - 0, - `${encodeURIComponent(profile.data.instance)?.toLowerCase()}`, + redirect( + 302, + resolveRoute(`/post/[instance]/[id]`, { + instance: profile.data.instance.toLowerCase(), + id: params.instance, + }), ) - - const newUrl = new URL(url) - newUrl.pathname = split.join('/') - - redirect(302, newUrl.toString()) } - error(404, 'Not found') + error(404) } diff --git a/src/routes/profile/+layout.ts b/src/routes/profile/+layout.ts index 38bb0a6a..5c67d2ac 100644 --- a/src/routes/profile/+layout.ts +++ b/src/routes/profile/+layout.ts @@ -1,10 +1,13 @@ import { profile } from '$lib/auth.svelte' import { client } from '$lib/lemmy.svelte.js' +import { error } from '@sveltejs/kit' // disable ssr, as the server cannot be authenticated export const ssr = false export async function load({ fetch }) { + if (!profile.data.jwt) error(401) + const my_user = // profile.data?.user ?? (await client({ auth: profile.data?.jwt, func: fetch }).getSite()).my_user diff --git a/src/routes/saved/+page.ts b/src/routes/saved/+page.ts index 31634890..6a44f43b 100644 --- a/src/routes/saved/+page.ts +++ b/src/routes/saved/+page.ts @@ -1,5 +1,6 @@ import { profile } from '$lib/auth.svelte' import { getClient } from '$lib/lemmy.svelte.js' +import { error } from '@sveltejs/kit' import type { CommentView, ListingType, PostView } from 'lemmy-js-client' function getSavedItemPublished(item: PostView | CommentView) { @@ -11,7 +12,8 @@ function getSavedItemPublished(item: PostView | CommentView) { } export async function load({ url, fetch }) { - if (!profile) return { posts: [], page: 0 } + if (!profile.data.jwt) throw error(401) + const page = Number(url.searchParams.get('page')) || 1 const type: 'comments' | 'posts' | 'all' = (url.searchParams.get('type') as 'comments' | 'posts' | 'all') || 'all'