recorder.js CORS header implementation. Closes #4426
This commit is contained in:
1 parent
80d02254ae
commit
9e0034781a
7 files changed
+192
-20
No files matched your search
+26
-19
@@ -2,6 +2,7 @@ import { isbot } from 'isbot';
|
||||
import { serializeError } from 'serialize-error';
|
||||
import { z } from 'zod';
|
||||
import { HEATMAP_EVENT_TYPE } from '@/lib/constants';
|
||||
import { corsPreflight, withCorsHeaders } from '@/lib/cors';
|
||||
import { secret } from '@/lib/crypto';
|
||||
import { getClientInfo, hasBlockedIp } from '@/lib/detect';
|
||||
import { parseToken } from '@/lib/jwt';
|
||||
@@ -96,22 +97,28 @@ async function getRequestBodySize(request: Request): Promise<number | null> {
|
||||
}
|
||||
}
|
||||
|
||||
export function OPTIONS() {
|
||||
return corsPreflight();
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const requestBodySize = await getRequestBodySize(request);
|
||||
|
||||
if (requestBodySize && requestBodySize > MAX_RECORD_REQUEST_BYTES) {
|
||||
return payloadTooLarge({
|
||||
reason: 'payload_too_large',
|
||||
maxBytes: MAX_RECORD_REQUEST_BYTES,
|
||||
size: requestBodySize,
|
||||
});
|
||||
return withCorsHeaders(
|
||||
payloadTooLarge({
|
||||
reason: 'payload_too_large',
|
||||
maxBytes: MAX_RECORD_REQUEST_BYTES,
|
||||
size: requestBodySize,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const { body, error } = await parseRequest(request, schema, { skipAuth: true });
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
return withCorsHeaders(error());
|
||||
}
|
||||
|
||||
const { website: websiteId } = body.payload;
|
||||
@@ -119,20 +126,20 @@ export async function POST(request: Request) {
|
||||
const timestamp = body.payload.timestamp;
|
||||
|
||||
if (!events?.length) {
|
||||
return json({ ok: true });
|
||||
return withCorsHeaders(json({ ok: true }));
|
||||
}
|
||||
|
||||
// Parse cache token to get session info
|
||||
const cacheHeader = request.headers.get('x-umami-cache');
|
||||
|
||||
if (!cacheHeader) {
|
||||
return badRequest({ message: 'Missing session token.' });
|
||||
return withCorsHeaders(badRequest({ message: 'Missing session token.' }));
|
||||
}
|
||||
|
||||
const cache = (await parseToken(cacheHeader, secret())) as Cache | null;
|
||||
|
||||
if (!cache?.sessionId || !cache?.visitId) {
|
||||
return badRequest({ message: 'Invalid session token.' });
|
||||
return withCorsHeaders(badRequest({ message: 'Invalid session token.' }));
|
||||
}
|
||||
|
||||
const { sessionId, visitId } = cache;
|
||||
@@ -141,7 +148,7 @@ export async function POST(request: Request) {
|
||||
const website = await getWebsite(websiteId);
|
||||
|
||||
if (!website) {
|
||||
return badRequest({ message: 'Website not found.' });
|
||||
return withCorsHeaders(badRequest({ message: 'Website not found.' }));
|
||||
}
|
||||
|
||||
const recorderConfig = getRecorderConfig(website.replayConfig);
|
||||
@@ -149,7 +156,7 @@ export async function POST(request: Request) {
|
||||
const heatmapEnabled = recorderConfig.heatmapEnabled === true;
|
||||
|
||||
if (!website.recorderEnabled) {
|
||||
return json({ ok: false, reason: 'recorder_disabled' });
|
||||
return withCorsHeaders(json({ ok: false, reason: 'recorder_disabled' }));
|
||||
}
|
||||
|
||||
if (process.env.CLOUD_MODE) {
|
||||
@@ -160,7 +167,7 @@ export async function POST(request: Request) {
|
||||
: null;
|
||||
|
||||
if (!account?.isBusiness && !account?.isNoBilling) {
|
||||
return forbidden({ message: 'Business subscription required.' });
|
||||
return withCorsHeaders(forbidden({ message: 'Business subscription required.' }));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,16 +175,16 @@ export async function POST(request: Request) {
|
||||
const { ip, userAgent } = await getClientInfo(request, {});
|
||||
|
||||
if (!process.env.DISABLE_BOT_CHECK && isbot(userAgent)) {
|
||||
return json({ beep: 'boop' });
|
||||
return withCorsHeaders(json({ beep: 'boop' }));
|
||||
}
|
||||
|
||||
if (hasBlockedIp(ip)) {
|
||||
return forbidden();
|
||||
return withCorsHeaders(forbidden());
|
||||
}
|
||||
|
||||
if (body.type === 'record') {
|
||||
if (!replayEnabled) {
|
||||
return json({ ok: false, reason: 'replay_disabled' });
|
||||
return withCorsHeaders(json({ ok: false, reason: 'replay_disabled' }));
|
||||
}
|
||||
|
||||
const eventTimestamps = events
|
||||
@@ -203,11 +210,11 @@ export async function POST(request: Request) {
|
||||
endedAt,
|
||||
});
|
||||
|
||||
return json({ ok: true });
|
||||
return withCorsHeaders(json({ ok: true }));
|
||||
}
|
||||
|
||||
if (!heatmapEnabled) {
|
||||
return json({ ok: false, reason: 'heatmap_disabled' });
|
||||
return withCorsHeaders(json({ ok: false, reason: 'heatmap_disabled' }));
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -238,8 +245,8 @@ export async function POST(request: Request) {
|
||||
console.log('heatmap save failed', serializeError(e));
|
||||
}
|
||||
|
||||
return json({ ok: true });
|
||||
return withCorsHeaders(json({ ok: true }));
|
||||
} catch (e) {
|
||||
return serverError(e);
|
||||
return withCorsHeaders(serverError(e));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user