Skip to content

Commit

Permalink
fix: Only allow URLs used by the website when forwarding to onshape API
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 committed Sep 2, 2024
1 parent 55b5afd commit 64fc6c6
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions web/server/api/onshape/[...path].ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { FetchError } from 'ofetch';
import useExtendedNitroApp from '~/server/composables/useExtendedNitroApp';

const ALLOWED_URL_REGEX = [
/assemblies\/d\/[0-9a-z]+\/w\/[0-9a-z]+\/e\/[0-9a-z]+\/bom\?indented=false/,
/documents\/[0-9a-z]+/,
/parts\/d\/[0-9a-z]+\/[vw]\/[0-9a-z]+\/e\/[0-9a-z]+\/partid\/[a-zA-Z]{3}\/boundingboxes\?configuration=.*/,
];

export default defineEventHandler(async (event) => {
const { onshape } = useExtendedNitroApp();
const url = event.node.req.originalUrl!.replace('/api/onshape/', '');

if (!ALLOWED_URL_REGEX.find((regex) => regex.test(url))) {
setResponseStatus(event, 400);
return {
message: 'URL is not allowed',
url,
};
}

try {
return await onshape.fetch(url);
} catch (err) {
Expand Down

0 comments on commit 64fc6c6

Please sign in to comment.