Skip to content

Commit

Permalink
Await the valid path checker
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Jan 10, 2024
1 parent 40d951d commit a094b99
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
3 changes: 2 additions & 1 deletion app/(public)/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class RedirectError extends Error {
const fetchNodeData = async (params: Params): Promise<{ node: StanfordNode, fullWidth: boolean }> => {
const draftMode = isDraftMode();
const path = getPathFromContext({params});
if (!pathIsValid(path)) throw new Error();
const valid = await pathIsValid(path)
if (!valid) throw new Error();

const pathInfo = await translatePathFromContext({params}, {draftMode});

Expand Down
19 changes: 1 addition & 18 deletions app/api/draft/route.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// route handler with secret and slug
import {draftMode} from 'next/headers'
import {redirect} from 'next/navigation'
import {getResourceByPath} from "@/lib/drupal/get-resource";
import {DrupalNode} from "next-drupal";

export async function GET(request: Request) {
// Parse query string parameters
Expand All @@ -17,20 +15,5 @@ export async function GET(request: Request) {
}
// Enable Draft Mode by setting the cookie
draftMode().enable()

if (slug.startsWith('/admin/paragraph')) {
redirect(slug);
}
// Fetch the headless CMS t44343o check if the provided `slug` exists
// getPostBySlug would implement the required fetching logic to the headless CMS
const node = await getResourceByPath<DrupalNode>(slug, {draftMode: true})

// If the slug doesn't exist prevent draft mode from being enabled
if (!node) {
return new Response('Invalid slug', {status: 401})
}

// Redirect to the path from the fetched post
// We don't redirect to searchParams.slug as that might lead to open redirect vulnerabilities
redirect(node.path.alias)
redirect(slug)
}

1 comment on commit a094b99

@vercel
Copy link

@vercel vercel bot commented on a094b99 Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.