Skip to content

Commit

Permalink
Merge pull request #624 from CodeForAfrica/fix/charterafrica_draft_mode
Browse files Browse the repository at this point in the history
Fix @/charterafrica security issue
  • Loading branch information
kilemensi authored Oct 4, 2023
2 parents ac3b62f + 71e28c4 commit aa24ccd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion apps/charterafrica/contrib/dokku/Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FROM codeforafrica/charterafrica-ui:0.1.9
FROM codeforafrica/charterafrica-ui:0.1.10
2 changes: 1 addition & 1 deletion apps/charterafrica/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "charterafrica",
"version": "0.1.9",
"version": "0.1.10",
"private": true,
"author": "Code for Africa <[email protected]>",
"description": "This is the official code for https://charter.africa site",
Expand Down
5 changes: 5 additions & 0 deletions apps/charterafrica/src/pages/api/v1/disable-draft.page.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 aa24ccd

Please sign in to comment.