mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
fix: forward headers during requests where needed
this is mainly just so any ip headers like cf-connecting-ip are forwarded for rate limiting
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
import type { RequestHandler } from '@sveltejs/kit';
|
import type { RequestHandler } from '@sveltejs/kit';
|
||||||
import { apiUrl } from '$lib/server/api';
|
import { apiUrl } from '$lib/server/api';
|
||||||
|
|
||||||
export const GET: RequestHandler = async ({ params }) => {
|
export const GET: RequestHandler = async ({ params, request }) => {
|
||||||
const { id } = params;
|
const { id } = params;
|
||||||
|
const headers = new Headers(request.headers);
|
||||||
|
|
||||||
const res = await fetch(`${apiUrl}/api/paste/${id}/raw`);
|
const res = await fetch(`${apiUrl}/api/paste/${id}/raw`, {
|
||||||
|
headers: headers
|
||||||
|
});
|
||||||
|
|
||||||
if (res.status === 404) {
|
if (res.status === 404) {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ import type { Paste } from '$lib/types/paste';
|
|||||||
import { extractError } from '$lib/utils/misc';
|
import { extractError } from '$lib/utils/misc';
|
||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch }) => {
|
export const load: PageServerLoad = async ({ fetch, request }) => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/paste/recent');
|
const headers = new Headers(request.headers);
|
||||||
|
const res = await fetch('/api/paste/recent', { headers });
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const errorMsg = extractError(await res.json()) || 'Failed to fetch recent pastes.';
|
const errorMsg = extractError(await res.json()) || 'Failed to fetch recent pastes.';
|
||||||
throw error(res.status, errorMsg);
|
throw error(res.status, errorMsg);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { apiUrl } from '$lib/server/api';
|
|||||||
import { getServerToken, clearServerTokens, roles, getUserFromToken } from '$lib/utils/auth';
|
import { getServerToken, clearServerTokens, roles, getUserFromToken } from '$lib/utils/auth';
|
||||||
import type { Report } from '$lib/types/report';
|
import type { Report } from '$lib/types/report';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch, cookies, params, url }) => {
|
export const load: PageServerLoad = async ({ fetch, cookies, params, url, request }) => {
|
||||||
const token = getServerToken(cookies);
|
const token = getServerToken(cookies);
|
||||||
if (!token) throw error(401, 'Login required');
|
if (!token) throw error(401, 'Login required');
|
||||||
|
|
||||||
@@ -14,9 +14,11 @@ export const load: PageServerLoad = async ({ fetch, cookies, params, url }) => {
|
|||||||
throw error(401, 'Invalid token');
|
throw error(401, 'Invalid token');
|
||||||
}
|
}
|
||||||
const canEdit = (user.roles & roles.Admin) !== 0 || (user.roles & roles.Moderator) !== 0;
|
const canEdit = (user.roles & roles.Admin) !== 0 || (user.roles & roles.Moderator) !== 0;
|
||||||
|
const headers = new Headers(request.headers);
|
||||||
|
headers.set('Authorization', `Bearer ${token}`);
|
||||||
|
|
||||||
const res = await fetch(`${apiUrl}/api/report/${params.reportId}`, {
|
const res = await fetch(`${apiUrl}/api/report/${params.reportId}`, {
|
||||||
headers: { Authorization: `Bearer ${token}` }
|
headers: headers
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ function normalizeTarget(value: string | null): 'all' | 'pastes' | 'users' {
|
|||||||
return 'all';
|
return 'all';
|
||||||
}
|
}
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch, cookies, url }) => {
|
export const load: PageServerLoad = async ({ fetch, cookies, url, request }) => {
|
||||||
const token = getServerToken(cookies);
|
const token = getServerToken(cookies);
|
||||||
if (!token) throw error(401, 'Login required');
|
if (!token) throw error(401, 'Login required');
|
||||||
|
|
||||||
@@ -44,8 +44,11 @@ export const load: PageServerLoad = async ({ fetch, cookies, url }) => {
|
|||||||
if ((target === 'pastes' || target === 'all') && pasteId) query.set('pasteId', pasteId);
|
if ((target === 'pastes' || target === 'all') && pasteId) query.set('pasteId', pasteId);
|
||||||
if ((target === 'users' || target === 'all') && userUuid) query.set('userUUID', userUuid);
|
if ((target === 'users' || target === 'all') && userUuid) query.set('userUUID', userUuid);
|
||||||
|
|
||||||
|
const headers = new Headers(request.headers);
|
||||||
|
headers.set('Authorization', `Bearer ${token}`);
|
||||||
|
|
||||||
const reportsRes = await fetch(`${apiUrl}/api/report/${target}?${query.toString()}`, {
|
const reportsRes = await fetch(`${apiUrl}/api/report/${target}?${query.toString()}`, {
|
||||||
headers: { Authorization: `Bearer ${token}` }
|
headers: headers
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!reportsRes.ok) {
|
if (!reportsRes.ok) {
|
||||||
|
|||||||
@@ -34,15 +34,19 @@ function decodeJwtUuid(token?: string | null): string | null {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent }) => {
|
export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent, request }) => {
|
||||||
const token = getServerToken(cookies);
|
const token = getServerToken(cookies);
|
||||||
const { username } = params;
|
const { username } = params;
|
||||||
const { options } = await parent();
|
const { options } = await parent();
|
||||||
const jwtUuid = decodeJwtUuid(token);
|
const jwtUuid = decodeJwtUuid(token);
|
||||||
const asGuest = url.searchParams.get('guest') === '1';
|
const asGuest = url.searchParams.get('guest') === '1';
|
||||||
|
const headers = new Headers(request.headers);
|
||||||
|
if (token && !asGuest) {
|
||||||
|
headers.set('Authorization', `Bearer ${token}`);
|
||||||
|
}
|
||||||
|
|
||||||
const res = await fetch(`${apiUrl}/api/user/${username}`, {
|
const res = await fetch(`${apiUrl}/api/user/${username}`, {
|
||||||
headers: token && !asGuest ? { Authorization: `Bearer ${token}` } : undefined
|
headers: headers
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
@@ -72,19 +76,19 @@ export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (token && !asGuest && canModerate && (!reportsSubmitted || !isOwner)) {
|
if (token && !asGuest && canModerate && (!reportsSubmitted || !isOwner)) {
|
||||||
|
headers.set('Authorization', `Bearer ${token}`);
|
||||||
const submittedRes = await fetch(`${apiUrl}/api/user/${user.uuid}/reports/submitted`, {
|
const submittedRes = await fetch(`${apiUrl}/api/user/${user.uuid}/reports/submitted`, {
|
||||||
headers: { Authorization: `Bearer ${token}` }
|
headers: headers
|
||||||
});
|
});
|
||||||
if (submittedRes.ok) reportsSubmitted = await submittedRes.json();
|
if (submittedRes.ok) reportsSubmitted = await submittedRes.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (token && !asGuest && !isOwner) {
|
if (token && !asGuest && !isOwner && canModerate) {
|
||||||
if (canModerate) {
|
headers.set('Authorization', `Bearer ${token}`);
|
||||||
const r = await fetch(`${apiUrl}/api/user/${user.uuid}/reports`, {
|
const r = await fetch(`${apiUrl}/api/user/${user.uuid}/reports`, {
|
||||||
headers: { Authorization: `Bearer ${token}` }
|
headers: headers
|
||||||
});
|
});
|
||||||
if (r.ok) reportsTarget = await r.json();
|
if (r.ok) reportsTarget = await r.json();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ import { apiUrl } from '$lib/server/api';
|
|||||||
import { extractError } from '$lib/utils/misc';
|
import { extractError } from '$lib/utils/misc';
|
||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
|
|
||||||
export const load = async ({ url }) => {
|
export const load = async ({ url, request }) => {
|
||||||
const token = url.searchParams.get('token');
|
const token = url.searchParams.get('token');
|
||||||
if (token) {
|
if (token) {
|
||||||
const res = await fetch(`${apiUrl}/api/auth/verify-email?token=${token}`);
|
const headers = new Headers(request.headers);
|
||||||
|
const res = await fetch(`${apiUrl}/api/auth/verify-email?token=${token}`, { headers });
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const json = await res.json().catch();
|
const json = await res.json().catch();
|
||||||
const message = extractError(json);
|
const message = extractError(json);
|
||||||
|
|||||||
Reference in New Issue
Block a user