Skip to content

Commit

Permalink
fix: check origin in the event endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
JuroUhlar committed Mar 5, 2024
1 parent 270bbbf commit fbb0f5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/client/components/playground/usePlaygroundSignals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function usePlaygroundSignals() {
} = useQuery<EventResponse | undefined>(
[requestId],
() =>
fetch(`/api/event/${agentResponse?.requestId}`).then((res) => {
fetch(`/api/event/${agentResponse?.requestId}`, { method: 'POST' }).then((res) => {
if (res.status !== 200) {
throw new Error(`${res.statusText}`);
}
Expand Down
14 changes: 14 additions & 0 deletions src/pages/api/event/[requestId].ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { isEventError } from '@fingerprintjs/fingerprintjs-pro-server-api';
import { NextApiRequest, NextApiResponse } from 'next';
import { fingerprintJsApiClient } from '../../../server/fingerprint-api';
import { ourOrigins } from '../../../server/server';

export default async function getFingerprintEvent(req: NextApiRequest, res: NextApiResponse) {
const { requestId } = req.query as { requestId: string };

/**
* In production, it's a good idea to validate the origin of the request,
* since this endpoint exposes the underlying authenticated Fingerprint Server API endpoint.
* It's just an extra layer of protection, you should primarily be using [Request filtering](https://dev.fingerprint.com/docs/request-filtering)
* to protect your Public API key from unauthorized usage.
*/
const origin = req.headers['origin'];
if (process.env.NODE_ENV === 'production' && !ourOrigins.includes(origin as string)) {
res.status(403).send({ message: `Origin ${origin} is not allowed to call this endpoint` });
return;
}

return await tryGetFingerprintEvent(res, requestId);
}

Expand Down

0 comments on commit fbb0f5c

Please sign in to comment.