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

Domain is blocked by CORS even though it is in the origin string? #73

Closed
vallamost opened this issue Jun 13, 2020 · 10 comments
Closed

Domain is blocked by CORS even though it is in the origin string? #73

vallamost opened this issue Jun 13, 2020 · 10 comments

Comments

@vallamost
Copy link

vallamost commented Jun 13, 2020

Hopefully a quick question from incorrect syntax. I am trying to only allow requests that are from my domain to succeed with fastify-cors. Requests are succeeding if I use an asterisk in the config but if I try and get specific on the domain it is failing.

This is the origin config I am trying to use:

  fastify.register(require('fastify-cors'), {
    origin: 'http://modfi-dev.nonset.com' // fails
  })

However when I use Firefox to initiate a request to my App I am getting this CORS error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://modfi-dev.nonset.com:4000/batch_stock_prices/?stocks=AAPL,MSFT,NFLX,TSLA,SBUX. 

(Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘http://modfi-dev.nonset.com’).

I've tried adding the port as well as trying a regex expression for the origin config but it doesn't seem to make a difference.

This is how I am attempting to make the request to the fastify server from my app

axios.get('http://' + appDomain +':4000/batch_stock_prices/?stocks=' + _stock, { timeout: 2000 })
...

appDomain is modfi-dev.nonset.com

Requests go through fine if I use an asterisk but that defeats the purpose of CORS:

  fastify.register(require('fastify-cors'), {
    origin: '*' // works fine
  })

Am I missing something obvious?

@mcollina
Copy link
Member

I think you need to include the port.

fastify.register(require('fastify-cors'), {
  origin: 'http://modfi-dev.nonset.com:4000'
})

@vallamost
Copy link
Author

vallamost commented Jun 15, 2020

I've tried that as well, I see the same issue, it seems like it's looking for an exact match:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://modfi-dev.nonset.com:4000/batch_stock_prices/?stocks=AAPL,MSFT,NFLX,TSLA,SBUX. 

(Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘http://modfi-dev.nonset.com:4000’)
  fastify.register(require('fastify-cors'), {
    origin: 'http://modfi-dev.nonset.com:4000'
  })

@Eomm
Copy link
Member

Eomm commented Jun 15, 2020

From docs, it is intended https://github.com/fastify/fastify-cors#options

string: strict match
you need to use a regexp: origin: /^http://modfi-dev.nonset.com:4000/

@vallamost
Copy link
Author

vallamost commented Jun 17, 2020

you need to use a regexp: origin: /^http://modfi-dev.nonset.com:4000/

Thanks for the response, the documentation made it sound like the context of the string match will match the domain name.

Regex is still throwing issues:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://modfi-dev.nonset.com:4000/batch_stock_prices/?stocks=AAPL,MSFT,NFLX,TSLA,SBUX. 

(Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘false’).

The regex I am trying:

  fastify.register(require('fastify-cors'), {
    origin: /^http:\/\/modfi-dev\.nonset\.com:4000/,
  })

I also tried getting more aggressive but it still ending up with a 'false' result

origin: /^http?:\/\/modfi-dev.nonset.com:4000\/[a-z]*.*/

Seems to match fine when testing here: https://regex101.com/

@Eomm
Copy link
Member

Eomm commented Jun 17, 2020

I would start from a playground like this since it seems to work:

const fastify = require('fastify')
const app = fastify({ logger: true })

app.register(require('fastify-cors'), {
  origin: /^http:\/\/modfi-dev\.nonset\.com:4000/
})

app.get('/', async () => 'hello')

;[
  '', // empty
  'http://modfi-dev.nonset.com:4000/batch_stock_prices/?stocks=AAPL,MSFT,NFLX,TSLA,SBUX',
  'http://modfi-dev.nonset.com'
].forEach(origin => {
  app.inject({
    url: '/',
    headers: { origin }
  }, (_, res) => {
    console.log(`origin [${origin}] = ${JSON.stringify(res.headers, null, 2)}`)
  })
})

The second one will print:

origin [http://modfi-dev.nonset.com:4000/batch_stock_prices/?stocks=AAPL,MSFT,NFLX,TSLA,SBUX] = {
  "vary": "Origin",
  "access-control-allow-origin": "http://modfi-dev.nonset.com:4000/batch_stock_prices/?stocks=AAPL,MSFT,NFLX,TSLA,SBUX",
  "content-type": "text/plain; charset=utf-8",
  "content-length": "5",
  "date": "Wed, 17 Jun 2020 07:49:11 GMT",
  "connection": "keep-alive"
}

@karrtopelka
Copy link

karrtopelka commented Sep 21, 2022

Hello, I have an issue with cors. My code is like in documentation or tutorials, but I still get error, even if origin is set to '*'. I've set:
fastify.register(cors, { origin: 'http://localhost:3000', credentials: true, })
but after trying to make a post method from frontend I get this:
image

I made a typescript project via fastify/cli, and put register in app.ts inside the app function
Any suggestions?

@Eomm
Copy link
Member

Eomm commented Sep 24, 2022

I don't understand: where did you set *?

Here should work:

fastify.register(cors, { origin: '*', credentials: true, })

Note that http://localhost:3000 !== http://localhost:3000/auth/login

In the worse scenario you can try the function:

function checkOrigin (origin, callback) {
   console.log({ origin })
   // do what you want
   callback(null, true)
}

fastify.register(cors, { origin: checkOrigin, credentials: true, })

@Uzlopak
Copy link
Contributor

Uzlopak commented Sep 24, 2022

localhost vs. 127.0.0.1

@karrtopelka
Copy link

I don't understand: where did you set *?

Here should work:

fastify.register(cors, { origin: '*', credentials: true, })

Note that http://localhost:3000 !== http://localhost:3000/auth/login

In the worse scenario you can try the function:

function checkOrigin (origin, callback) {
   console.log({ origin })
   // do what you want
   callback(null, true)
}

fastify.register(cors, { origin: checkOrigin, credentials: true, })

As I know combining '*' with credentials is bad, so it will not work, but I checked everything, like your suggestion, without credentials, with localhost, with 127.0.0.1, and so on.

In postman, everything works fine, but when I try to make a call from frontend, nothing works

@karrtopelka
Copy link

localhost vs. 127.0.0.1

I also tried it, and no success

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

No branches or pull requests

5 participants