This commit is contained in:
@@ -2,6 +2,8 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|||||||
import { corsAllMethods, runMiddleware } from '@/utils/cors';
|
import { corsAllMethods, runMiddleware } from '@/utils/cors';
|
||||||
import { KoSyncProxyPayload } from '@/types/kosync';
|
import { KoSyncProxyPayload } from '@/types/kosync';
|
||||||
|
|
||||||
|
const validEndpoints = [/\/users\/create/, /\/users\/auth/, /\/syncs\/progress/];
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
await runMiddleware(req, res, corsAllMethods);
|
await runMiddleware(req, res, corsAllMethods);
|
||||||
|
|
||||||
@@ -21,6 +23,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
return res.status(400).json({ error: 'serverUrl and endpoint are required' });
|
return res.status(400).json({ error: 'serverUrl and endpoint are required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!validEndpoints.some((regex) => regex.test(endpoint))) {
|
||||||
|
return res.status(400).json({ error: 'Invalid endpoint' });
|
||||||
|
}
|
||||||
|
|
||||||
const targetUrl = `${serverUrl.replace(/\/$/, '')}${endpoint}`;
|
const targetUrl = `${serverUrl.replace(/\/$/, '')}${endpoint}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -34,11 +40,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
body: clientBody ? JSON.stringify(clientBody) : null,
|
body: clientBody ? JSON.stringify(clientBody) : null,
|
||||||
});
|
});
|
||||||
|
|
||||||
const contentType = response.headers.get('content-type');
|
|
||||||
if (!contentType || !contentType.includes('application/json')) {
|
|
||||||
throw new Error('Invalid sync server response: Unexpected Content-Type.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.text();
|
const data = await response.text();
|
||||||
res.status(response.status);
|
res.status(response.status);
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ export class KOSyncClient {
|
|||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
accept: 'application/vnd.koreader.v1+json',
|
accept: 'application/vnd.koreader.v1+json',
|
||||||
|
...(method === 'GET' ? {} : { 'Content-Type': 'application/json' }),
|
||||||
...Object.fromEntries(headers.entries()),
|
...Object.fromEntries(headers.entries()),
|
||||||
},
|
},
|
||||||
body,
|
body,
|
||||||
|
|||||||
Reference in New Issue
Block a user