Git Product home page Git Product logo

Comments (13)

mcollina avatar mcollina commented on June 14, 2024 1

@codyzu ptal

from fastify-cors.

zekth avatar zekth commented on June 14, 2024

your regex is not good, use:

new RegExp(/http:\/\/localhost/)

from fastify-cors.

DRoet avatar DRoet commented on June 14, 2024

RegExp should handle the escaping correctly, no?

console.log(new RegExp('http://localhost'))
// prints:  /http:\/\/localhost/

Which should turn it into: origin: /http:\/\/localhost/

from fastify-cors.

zekth avatar zekth commented on June 14, 2024

you wrote:

   origin: new RegExp(/http://localhost/),
// not 
   origin: new RegExp('http://localhost'),

from fastify-cors.

DRoet avatar DRoet commented on June 14, 2024

oh my bad, that was a copy/paste error for the reproduction on my end. I actually use an ENV variable to fill the RegExp. I edited the reproduction above.

from fastify-cors.

codyzu avatar codyzu commented on June 14, 2024

I can't reproduce this on my machine with fastify-cors 5.0.0.

Can you copy the request and response headers for the OPTIONS request in the chrome dev tools network tab (right-click on the OPTIONS request and copy the request/response headers)?

fastify-cors does not return 401, so I would be suspicious of the authentication plugin. What are you using for auth?

from fastify-cors.

DRoet avatar DRoet commented on June 14, 2024

Hmm you are right, I found the issue in my own preHandler hook that does the authentication. I was using req.context.config to figure out which route is being called.
This now differs in version 5.0.0 (maybe intentionally? should I be using a different Fastify method to get this?)

4.1.0:

fastify.addHook('preHandler', (req, reply, done) => {
   console.log(req.context.config)
   // { url: '/api/login', method: 'POST' }
})

5.0.0:

fastify.addHook('preHandler', (req, reply, done) => {
   console.log(req.context.config)
   // { url: '*', method: 'OPTIONS' }
})

from fastify-cors.

codyzu avatar codyzu commented on June 14, 2024

req.routerPath and req.routerMethod would be the official way to check which router is handling the route (in place of req.context).

The problem you are most likely seeing is that in fastify-cors 5.0.0 processing of a preflight request is done in a route: OPTIONS * and you should not enforce authentication on that route. Try adding this to your preHandler hook:

fastify.addHook('preHandler', (req, reply, done) => {
   // Don't authenticate preflight requests
   if(req.routerPath === '*' && req.routerMethod === 'OPTIONS') {
      return done();
   }

   // Your authentication code ...
})

It could also be resolved by not having a global preHandler hook for authentication and instead authenticating only the routes or plugins that require authentication.

@mcollina I believe this is a side effect of moving the preflight handling into a route (previously the response would have been handled in fastify-cors onRequest hook). Consumers need to be careful not to enforce authentication on the preflight route... Do you see this as a problem? I can add an example in the docs πŸ€”

from fastify-cors.

mcollina avatar mcollina commented on June 14, 2024

In theory, a cors OPTIONS request should include all headers needed for auth/what would be included in the original request. It should be ok to perform auth for those requests.. could you do a quick check with a browser for both jwt and cookie based solutions?

from fastify-cors.

DRoet avatar DRoet commented on June 14, 2024

thanks for the pointer to req.routerPath, I've added the preflight skip to the code on my end for now

from fastify-cors.

codyzu avatar codyzu commented on June 14, 2024

For simple CORS requests (requests that don't require pre-flighting), the headers are included.

For preflight requests (the CORS OPTIONS request), the browser removes the original request headers. Instead, it includes the name of the headers in Access-Control-Request-Headers header, but not the values. See here.

I tested this with JWT, but not yet cookies. In any case, it seems like it will continue to create problems for anyone who authenticates all requests.

from fastify-cors.

codyzu avatar codyzu commented on June 14, 2024

@DRoet this should be resolved with the 5.1.0 release of fastify-cors, returning the pre 5.0.0 behavior and removing the need to not authenticate OPTIONS requests. With the default options, fastify-cors should now automatically reply to preflight requests before the authentication plugin.

AFAIK this can be closed.

from fastify-cors.

DRoet avatar DRoet commented on June 14, 2024

thanks!

from fastify-cors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.