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

nuxt-security breaks the app on Cloudflare Pages #137

Closed
LilaRest opened this issue Apr 4, 2023 · 13 comments
Closed

nuxt-security breaks the app on Cloudflare Pages #137

LilaRest opened this issue Apr 4, 2023 · 13 comments
Labels
bug Something isn't working

Comments

@LilaRest
Copy link

LilaRest commented Apr 4, 2023

Version

nuxt-security: 0.13.0
nuxt: 3.3.3

What is actually happening?

When hosting a Nuxt 3 app on Cloudflare Pages, enabling the nuxt-security module raises a Cannot convert object to primitive value error.

In addition, enabling the module will also break all the server API endpoints that return a non-string value (e.g. an endpoint /api/date that returns a Date object).

Disabling the module or hosting the app on Netlify or Vercel fixes the issue.

Steps to reproduce

  • Setup a minimal Nuxt 3 project
  • Install and enable nuxt-security
  • Build files for Cloudflare Pages using : NITRO_PRESET=cloudflare_pages npx nuxt build
  • Download the Cloudflare CLI : npm install -g wrangler
  • Run the app locally using : npx wrangler pages dev dist/
  • Observe the errors (which are the exact same in production)
[nuxt] [request error] [unhandled] [500] Cannot convert object to primitive value

Other

There is a related discussion on the Cloudflare's repo here : cloudflare/workers-sdk#2081

Thanks !

@LilaRest LilaRest added the bug Something isn't working label Apr 4, 2023
@LilaRest LilaRest changed the title Module break the app on Cloudflare Pages nuxt-security breaks the app on Cloudflare Pages Apr 4, 2023
@Baroshem
Copy link
Owner

Baroshem commented Apr 4, 2023

Hey,

thanks for reporting this issue! I will take a look at it in the upcoming days :)

@LilaRest
Copy link
Author

LilaRest commented Apr 4, 2023

Hey,

thanks for reporting this issue! I will take a look at it in the upcoming days :)

@Baroshem Thanks for that :)

@Baroshem
Copy link
Owner

Baroshem commented Apr 6, 2023

I am currently investigating this issue but I have a question about the API endpoints. Could you tell me what do you mean that they are breaking?

Like do you get a certain error when running them or something esle breaks?

@LilaRest
Copy link
Author

LilaRest commented Apr 6, 2023

@Baroshem Thanks ! Yeah sure.
I get the exact same error on my own API endpoints : [nuxt] [request error] [unhandled] [500] Cannot convert object to primitive value

By the way I tested again and its seems that any path under /api/ even the one I haven't registered (e.g., /api/notused or just /api/) are returning the same error

@Baroshem
Copy link
Owner

Baroshem commented Apr 6, 2023

I suppose it is related to one of my middlewares that are trying to convert the object (maybe a body or query) and it is failing because of different sfructue for Cloudflare, because as you mentioed it works correctly on other hosting providers

@LilaRest
Copy link
Author

LilaRest commented Apr 6, 2023

@Baroshem Yes that makes sense. Have you a precise idea of which middleware could cause that ?

@Baroshem
Copy link
Owner

Baroshem commented Apr 6, 2023

I would guess that XSS or Request Size. But I am not sure as I have not worked with Cloudflare that much.

I will check this out tomorrow probably as today I am quite busy with regular work.

If you have some time (and could help me debugging). Could you try to disable some of the middlewares to see which one is causing the problem?

You can do so by just setting a false as a value for certain middleware.

I will really appreciate it :)

@LilaRest
Copy link
Author

LilaRest commented Apr 6, 2023

@Baroshem I'm actually really busy too :|
I'll let you know if I find some time to perform those tests

@shadow81627
Copy link

I got an error log that looked like this

[nuxt] [request error] [unhandled] [500] Cannot convert object to primitive value
  at /node_modules/limiter/dist/esm/clock.js:5:43  
  at getMilliseconds (/node_modules/limiter/dist/esm/clock.js:17:1)  
  at new TokenBucket (/node_modules/limiter/dist/esm/TokenBucket.js:46:25)  
  at new RateLimiter (/node_modules/limiter/dist/esm/RateLimiter.js:17:28)  
  at Object.handler (/node_modules/nuxt-security/dist/runtime/server/middleware/rateLimiter.mjs:10:29)  

The error looks to be coming from RateLimiter getMilliseconds https://github.com/jhurliman/node-rate-limiter/blob/main/src/clock.ts#L21-L24

Disabling the rateLimiter middleware helps resolve my error.

@LilaRest
Copy link
Author

@shadow81627 Wow thanks for your investigation !
By the way I'm even not sure a local-storage based rate limiter is relevant when the app is hosted on serverless services like Cloudflare Pages. AFAIK, the instances of the app spawned dynamically (to fit the current traffic), don't share nor persist their local storage.

@Baroshem
Copy link
Owner

Hey @shadow81627 @LilaRest

Thanks for the investigation on that topic.

As this bug seems to be related with external dependency (either rate limiter or memory-cache) I would recommend you to disable this middleware by setting rateLimiter to false in the security conifguration of nuxt.config.ts file.

This also motivates me to create a dicsussion about deprecating the rate limiter functionality as it was supposed to work on the very basic examples and in more advanced cases it seems to be causing more problems than it solves. For real life applications, a seperate solution should be used that could help mitigate cases like DDoS attacks than this in memory rate limiter.

I will create this discussion in the upcoming days.

@Baroshem
Copy link
Owner

It seems that there is not a lot of traffic on the discussion about removing the rateLimiter so at this point, I can recommend you to disable the rateLimiter. The app should work ok right now. I will add a note in the documentation that this rateLimiter does not work in the Cloudflare Pages.

@Baroshem Baroshem closed this as not planned Won't fix, can't repro, duplicate, stale Apr 19, 2023
kyranet added a commit to skyra-project/memes.skyra.pw that referenced this issue May 31, 2023
@Baroshem
Copy link
Owner

Hey guys,

Could you add some comments in this discussion? #140

I am thinking about derecating the built in rate limiter due to the case that it creates more problems than it solves.

vejja added a commit that referenced this issue Jun 26, 2024
<!--- Provide a general summary of your changes in the title above -->

Closes #470

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [ ] Bug fix (a non-breaking change which fixes an issue)
- [x] New feature (a non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Description
<!--- Describe your changes in detail -->
<!--- Why is this change required? What problem does it solve? -->
<!--- If it resolves an open issue, please link to the issue here. For example "Resolves: #137" -->

This PR adds a new `owaspDefaults` option, which can take 2 possible values:
- `compatibility` (default): OWASP default settings are chosen to minimize the possibility of breaking the app. These default values are the same as in v1.
- `security`: OWASP default settings are chosen to maximize security. These default values will usually require some additional fine-tuning to ensure the app will run smoothly.

With `security` OWASP level, the following headers are modified:
1- `contentSecurityPolicy` blocks everything by default with `default-src: 'none'`. In addition, all `'unsafe-inline'` values are removed.
2- `crossOriginEmbedderPolicy` is set to `require-corp`
3- `strictTransportSecurity` has the `preload` flag
4- 'xFrameOptions` is set to `DENY`

## Checklist:
<!--- Put an `x` in all the boxes that apply. -->
<!--- If your change requires a documentation PR, please link it appropriately -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [x] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes (if not applicable, please state why)
vejja added a commit that referenced this issue Jul 30, 2024
<!--- Provide a general summary of your changes in the title above -->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [ ] Bug fix (a non-breaking change which fixes an issue)
- [x] New feature (a non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Description
<!--- Describe your changes in detail -->
<!--- Why is this change required? What problem does it solve? -->
<!--- If it resolves an open issue, please link to the issue here. For example "Resolves: #137" -->

Closes #494

This PR introduces support for Nuxt Server Components (a.k.a Islands).

## Checklist:
<!--- Put an `x` in all the boxes that apply. -->
<!--- If your change requires a documentation PR, please link it appropriately -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes (if not applicable, please state why)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants