Skip to content

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

@andre-ticloudged

Description

@andre-ticloudged

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,
                })
            });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions