Skip to content

Commit ccceaab

Browse files
committed
validate request headers
1 parent f78eccc commit ccceaab

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

src/components/SuggestionForm.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
const res = await fetch('/api/issue', {
3434
method: 'POST',
3535
body: JSON.stringify({ title, body, url: page.url.href, name }),
36+
headers: { 'Content-Type': 'application/json' },
3637
})
3738
3839
const res_json = await res.json()

src/routes/api/issue/+server.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
GITHUB_REPO,
1111
TITLE_MAX_LENGTH,
1212
NAME_MAX_LENGTH,
13+
ORIGIN,
1314
} from './config'
1415
import { flag_violation, is_blocked, has_profanity, rate_limit } from '$lib/server/redis'
1516

@@ -72,6 +73,16 @@ async function parse_data(
7273
): Promise<
7374
{ error: string } | { title: string; body: string; url: string; name: string }
7475
> {
76+
const content_type = request.headers.get('Content-Type')
77+
if (content_type !== 'application/json') {
78+
return { error: 'Forbidden' }
79+
}
80+
81+
const origin = request.headers.get('origin') ?? ''
82+
if (!request.url.startsWith(origin)) {
83+
return { error: 'Forbidden' }
84+
}
85+
7586
let data
7687
try {
7788
data = await request.json()

src/routes/api/issue/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export const GITHUB_REPO = 'CatDat'
55
export const TITLE_MAX_LENGTH = 50
66
export const BODY_MAX_LENGTH = 10000
77
export const NAME_MAX_LENGTH = 50
8+
export const ORIGIN = 'https://catdat.app'

0 commit comments

Comments
 (0)