Skip to content

[fresh] Form CSRF reader throws on a malformed Cookie header #2632

Closed

Description

@rickylabs

Summary

readCsrfToken throws when the request carries a Cookie header that @std/http cannot parse. The page then answers 500 instead of treating the request as having no token. Forms that disabled CSRF are affected too.

Evidence (main at b0eb079, same on v0.0.9)

  • packages/fresh/src/application/form/validation/csrf.ts:28-30 calls getCookies(request.headers) unguarded.
  • getCookies throws SyntaxError for headers such as Cookie: =x, Cookie: a=1;=x, and an empty Cookie header.
  • Call sites:
    • packages/fresh/src/application/builders/define-page/builder/form-support.ts:68 (POST). It runs before the csrf === false check on line 69, so forms that opt out also throw.
    • packages/fresh/src/application/builders/define-page/builder/mod.tsx:355 (GET loader).
  • auth-kv-oauth: parseCookieHeader throws on one undecodable foreign cookie and loses the whole header #2115 fixed the same class of bug in auth-kv-oauth.

Reproduction

Send POST to any definePage form route with the header Cookie: =x.

Expected behavior

An unparsable Cookie header counts as "no token": a POST with CSRF enabled is rejected as a CSRF failure, a POST with csrf: false proceeds, and a GET renders with a fresh token.

Acceptance

  • readCsrfToken returns undefined for an unparsable Cookie header, proven by a unit test that fails on current main.
  • A POST with a malformed Cookie header gets the CSRF rejection response, not a 500.
  • A form with csrf: false accepts a POST with a malformed Cookie header.
  • A GET with a malformed Cookie header renders and sets a fresh token.

Acceptance verified by independent evaluation of PR #2640: #2640 (comment)

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions