Skip to content

Commit 5b5cfe3

Browse files
committed
Require authentication before processing query params
Added checks in +page.svelte and models/[...model]/+page.svelte to require authentication if 'q', 'prompt', or 'attachments' query parameters are present. This prevents unauthenticated users from processing these parameters and ensures proper redirection to login when needed. Also improved error handling for non-JSON error responses.
1 parent 031ca48 commit 5b5cfe3

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/routes/+page.svelte

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import { onMount, tick } from "svelte";
1616
import { loading } from "$lib/stores/loading.js";
1717
import { loadAttachmentsFromUrls } from "$lib/utils/loadAttachmentsFromUrls";
18+
import { requireAuthUser } from "$lib/utils/auth";
1819
1920
let { data } = $props();
2021
@@ -52,7 +53,16 @@
5253
});
5354
5455
if (!res.ok) {
55-
const errorMessage = (await res.json()).message || ERROR_MESSAGES.default;
56+
let errorMessage = ERROR_MESSAGES.default;
57+
try {
58+
const json = await res.json();
59+
errorMessage = json.message || errorMessage;
60+
} catch {
61+
// Response wasn't JSON (e.g., HTML error page)
62+
if (res.status === 401) {
63+
errorMessage = "Authentication required";
64+
}
65+
}
5666
error.set(errorMessage);
5767
console.error("Error while creating conversation: ", errorMessage);
5868
return;
@@ -78,8 +88,17 @@
7888
7989
onMount(async () => {
8090
try {
91+
// Check if auth is required before processing any query params
92+
const hasQ = page.url.searchParams.has("q");
93+
const hasPrompt = page.url.searchParams.has("prompt");
94+
const hasAttachments = page.url.searchParams.has("attachments");
95+
96+
if ((hasQ || hasPrompt || hasAttachments) && requireAuthUser()) {
97+
return; // Redirecting to login, will return to this URL after
98+
}
99+
81100
// Handle attachments parameter first
82-
if (page.url.searchParams.has("attachments")) {
101+
if (hasAttachments) {
83102
const result = await loadAttachmentsFromUrls(page.url.searchParams);
84103
files = result.files;
85104

src/routes/models/[...model]/+page.svelte

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import { pendingMessage } from "$lib/stores/pendingMessage";
1313
import { sanitizeUrlParam } from "$lib/utils/urlParams";
1414
import { loadAttachmentsFromUrls } from "$lib/utils/loadAttachmentsFromUrls";
15+
import { requireAuthUser } from "$lib/utils/auth";
1516
1617
let { data } = $props();
1718
@@ -64,8 +65,17 @@
6465
6566
onMount(async () => {
6667
try {
68+
// Check if auth is required before processing any query params
69+
const hasQ = page.url.searchParams.has("q");
70+
const hasPrompt = page.url.searchParams.has("prompt");
71+
const hasAttachments = page.url.searchParams.has("attachments");
72+
73+
if ((hasQ || hasPrompt || hasAttachments) && requireAuthUser()) {
74+
return; // Redirecting to login, will return to this URL after
75+
}
76+
6777
// Handle attachments parameter first
68-
if (page.url.searchParams.has("attachments")) {
78+
if (hasAttachments) {
6979
const result = await loadAttachmentsFromUrls(page.url.searchParams);
7080
files = result.files;
7181

0 commit comments

Comments
 (0)