Skip to content

Commit

Permalink
Fix security issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kilemensi committed Oct 4, 2023
1 parent ac3b62f commit 3beec33
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions apps/charterafrica/src/pages/api/v1/draft/disable-draft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// By default, the Draft Mode session ends when the browser is closed.
// This method clears it manually / on demand.
export default function handler(req, res) {
res.setDraftMode({ enable: false });
}
12 changes: 10 additions & 2 deletions apps/charterafrica/src/pages/api/v1/draft/index.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ export default async function handler(req, res) {
// make sure the user requesting to preview, is logged into Payload
// See "Tip" on: https://payloadcms.com/docs/authentication/overview#token-based-auth
if (!req.user) {
return res.status(500).json({ message: "UNAUTHORIZED_USER" });
return res.status(401).json({ message: "UNAUTHORIZED_USER" });
}
const { slug } = req.query;
res.setDraftMode({ enable: true });

return res.redirect(slug);
// Guard against open redirect vulnerabilities
// Since slug will be a path, redirect to pathname instead of original slug
// just in case
const appUrl = new URL(process.env.NEXT_PUBLIC_APP_URL);
const requestedUrl = new URL(slug, appUrl);
if (requestedUrl.origin !== appUrl.origin) {
return res.status(401).json({ message: "UNAUTHORIZED_REDIRECT" });
}
return res.redirect(requestedUrl.pathname);
}

0 comments on commit 3beec33

Please sign in to comment.