Skip to content

lib/blog.ts getPost accepts unvalidated string instead of Slug #67

Description

@thiras

`lib/blog.ts:158` types `getPost(slug: string)` and compares raw against entry slugs. `app/blog/[slug]/page.tsx:30,56` passes the un-narrowed `params.slug` straight in.

Today this is safe because `output: "export"` + `dynamicParams = false` means the framework only routes pre-validated slugs to this handler. But the function signature invites a regression if a future caller invokes it from an unguarded path (RSS feed, search, new route).

Fix

Call `parseSlug` at the top and return `null` on failure — matches the `Slug` brand already defined in the same file, costs ~3 lines, and locks the invariant at the function boundary instead of relying on transitive framework guarantees.

```ts
export function getPost(slug: string): PostSource | null {
const valid = parseSlug(slug);
if (!valid) return null;
return readEntries().find((e) => e.slug === valid) ?? null;
}
```

Found during PR review of #61.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions