balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

Using CRSF set to true returns forbidden as response on api calls #7324

Open andre-ticloudged opened 7 months ago

andre-ticloudged commented 7 months ago

Node version: 20.10.0 Sails version (sails): 1.5.8 ORM hook version (sails-hook-orm): 4.0.0 Sockets hook version (sails-hook-sockets):2.0.0 Grunt hook version (sails-hook-grunt): 5.0.0 Uploads hook version (sails-hook-uploads): 0.4.3 DB adapter & version :sails-mongo@2.0.1


Hello, i'm having a problem with the CSRF configuration, the requests always hits the HTTP 403 forbidden response. Thanks in advance!

Following the docs, i set the route to get the crsf token to this:

'GET /csrfToken': { action: 'security/grant-csrf-token' }


And the cors configuration to:

cors: {
     allRoutes: true,
     allowCredentials: true,
     allowOrigins: ['http://127.0.0.1:3000', 'http://localhost:3000'],
     allowRequestHeaders: 'content-type,x-csrf-token,authorization',
     allowRequestMethods: 'GET,POST,PUT,DELETE,OPTIONS,PATCH'
   },
  csrf: true,


I am able to acess the crsf token in the url http://localhost:1337/csrfToken
The request to the api are being made like this:

const request = await fetch(http://localhost:1337/api/v1/my-endpoint`, {
                method: 'POST',
                credentials:"same-origin",
                headers: {
                    'X-CSRF-Token': csrfToken,
                    'Content-Type': 'application/json',
                    "authorization" : `Bearer ${sessionValues.token}`,
                },
                body: 
                    JSON.stringify({
                    _csrf: csrfToken,
                })
            });
sailsbot commented 7 months ago

@andre-ticloudged Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

G0x209C commented 4 months ago

What is the code doing around it? Have you been able to solve it? Have you made sure that the CSRF token is always used once, and once only? They are consumed.

But without a modicum of context it's difficult to assert why you're getting a 403.

I also see you're setting it in the headers, maybe don't do that since the documentation specifies it must be part of the body or the request's url_query.