Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type bug introduced in v4.3.0 in secureHeaders middleware #2693

Closed
MonsterDeveloper opened this issue May 17, 2024 · 5 comments
Closed

Type bug introduced in v4.3.0 in secureHeaders middleware #2693

MonsterDeveloper opened this issue May 17, 2024 · 5 comments
Labels

Comments

@MonsterDeveloper
Copy link
Contributor

What version of Hono are you using?

4.3.0

What runtime/platform is your app running on?

Bun

What steps can reproduce the bug?

  1. Install version 4.3.0 of Hono.
  2. Create a TS file with an app that uses secureHeaders middleware and another arbitrary middleware that sets the context (I used @hono/clerk-auth as an example). Also in this file you'll mount the routes from another file with app.route().
  3. In another file, create another app and try to access the context value with context.get from this other middleware, and see it fail, as the context only contains "secureHeadersNonce" variable.

4.3.0 CodeSandbox with a bug: https://codesandbox.io/p/devbox/hono-4-3-0-not-working-9kg2s7

4.2.9 CodeSandbox working fine: https://codesandbox.io/p/devbox/hono-4-2-9-working-vlx83h

What is the expected behavior?

One can access the context variable in another file.

What do you see instead?

No response

Additional information

I confirmed that the issue lies in the secureHeaders middleware, as by commenting it out everything works fine as before. I suspect these lines to be the issue from PR #2577:

declare module '../../context' {
  interface ContextVariableMap {
    secureHeadersNonce?: string
  }
}

It seems like this is overwriting the context variables set by other middleware, therefore it's impossible to access variables from other middleware without importing it directly (which would be an unused import technically).

@MonsterDeveloper
Copy link
Contributor Author

As a matter of fact, I checked other built-in middleware (like timing) that also set context variables via declare module '../../context' and the issue seems to also be present.

I assume this declaration overwrites 3rd party middleware variable as they set it via declare module 'hono'.

I guess changing the declaration method should help.

@MonsterDeveloper
Copy link
Contributor Author

I can confirm that changing declare module '../../context' to declare module 'hono' solved the issue. Will create a PR now.

@yusukebe
Copy link
Member

Hi @MonsterDeveloper !

I think this issue was fixed with the new version v4.4.0. Can you try it?

@MonsterDeveloper
Copy link
Contributor Author

Seems to be working, thanks!

@yusukebe
Copy link
Member

Yeess 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants