CORS Issue between a front-end request and two nodeJS servers [closed] - node.js

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 19 hours ago.
Improve this question
I am getting a CORS issue saying a header is missing, but the header is present in both server's code.
Here is what I am trying to do:
POST request from frontend to nodeServer1, then inside the endpoint I make a POST request to nodeServer2
And then a response is sent back to the front end.
My Access-Control-Allow-Origin policy is "*" for nodeServer1, and is "[nodeServer1URL]", "[frontendURL]" for nodeServer2.
When I make another request from the frontend to nodeServer1, I am not getting any issues. But for the request I'm trying to get here is the error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at "[nodeServer1EndPointURL]". (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 502.
nodeServer1 executes whatever is in the endpoint as expected. When I test locally the nodeServer2 endpoint it works as well, so it has to be CORS.
So the issue is most definitely with nodeServer2's policy, which is apparently missing. Even if I set the policy of nodeServer2 to "*" it fails with the same error. I don't know why.
Not sure why is the remote resource my full endpoint URL.
So my question is, how can I fix this CORS issue?

Related

CORS policy: No 'Access-Control-Allow-Origin' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
Improve this question
Im develop a react website with node js backend than react show some error link enter image description here
you are not useing cors or same url for frondend and backend thats why this error coming .For send data frontend to sarver you need use "cors" package.
install cors:
npm install cors
then requre it index.js file
const cors = require('cors')
app.use(cors())
That's to do with the backend. Your server isn't set up to receive requests from the URL you're using (localhost). If you're the one writting the server, look up the cors npm package to enable cross site requests. Otherwise, ask whoever is the author of the server.

how exactly CORS is improving security [duplicate]

This question already has answers here:
same-origin policy and CORS - what's the point?
(1 answer)
Same origin Policy and CORS (Cross-origin resource sharing)
(2 answers)
What is the issue CORS is trying to solve?
(3 answers)
Closed 12 months ago.
I know exactly how CORS is working, i know it's implemented on browsers, and i know it forbids XMLHTTPRequests to other origins unless the remote origin allows it using the response header Access-Control-Allow-Origin.
And of-course I heard it's because the "security reasons" that it's there.
The thing i don't get is how it's improving security.
So imagine we're in a browser and we have a malicious js file loaded in our page and it wants to send our local storage data or cookies to another origin (hacker origin). so the hacker simply set the "Access-Control-Allow-Origin" to * and he's good to go! so what CORS did exactly here?
Somewhere i read that CORS is there because of "intellectual property" and that makes sense somehow, so some remote servers don't want to answer to requests from other clients. that's ok.. . but for security reasons!? I don't get that part.
I would appreciate if anyone could help me with this.

Nest api connectivity issue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I've tried all of the steps in the instructions, but always get an error at #5 when it is supposed to redirect to google.com.
https://developers.google.com/nest/device-access/authorize#link_your_account
The error:
Authorization Error
Error 400: redirect_uri_mismatch
The redirect URI in the request, https://www.google.com, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}
I've ensured I have the right numbers- without spaces in the url and have put it in notepad and deleted all of the returns so it is one line of text.
any ideas?
What do you have listed in the Authorized Redirect URIs for your OAuth client in GCP, and what is the exact PCM URL you used? You shouldn't get this error if the redirect_uri and client_id parameters in the PCM URL matches what you have in GCP for the client.

CORS locking HTTP request to Elasticsearch server on Angular served on Firebase [duplicate]

This question already has answers here:
How does the 'Access-Control-Allow-Origin' header work?
(19 answers)
Why doesn't adding CORS headers to an OPTIONS route allow browsers to access my API?
(36 answers)
firebase hosting blocking script due to CORS issue
(8 answers)
Closed 4 years ago.
First of all, I'm sure that this is a duplicate question, but I'm a bit far from web techs, and couldn't understand what the hell is CORS, why it is blocking a simple HTTP request and how can I bypass it.
I'm trying to create an Angular 5 + Node.js web app, hosted on Firebase Hosting, and have an Elasticsearch instance on Google Cloud Platform. All I need to do is send 2 very basic HTTP requests to the ES instance from this web app. I'm already sending these requests from mobile apps and Postman app, and there is no problem. But web app logs Preflight response is not successful error. I googled the error and see that it is thrown by CORS. As I said, I have no idea what the hell is CORS and how to bypass it. Any help please (simple help to a non-web-developer). Thank you.
Shortly, CORS (Cross-Origin Resource Sharing) is a security mechanism enforced by default by the browser which specifies what resources (on which servers) your application can use (make requests for). This is a good explanation of CORS - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS.
So you have to set on your server the domain of your application as being one of the domains allowed to make calls. If you do so, the preflight request will return with an Access-Control-Allow-Origin header set and the browser allows the actual request to be sent.
You can have a look here to see how to do it in Firebase - https://groups.google.com/forum/#!msg/firebase-talk/oSPWMS7MSNA/RnvU6aqtFwAJ

CORS in OAuth: Response to preflight request doesn't pass access control check

So I'm trying to implement the OAuth 2 flow, while my webapp is the server that give away authorization code/access token.
Cors error happens when sending the code back to third-party website(zapier in this case):
XMLHttpRequest cannot load https://zapier.com/dashboard/auth/oauth/return/App505CLIAPI/?code=somecode&state=somestate. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://myurl' is therefore not allowed access.
If I manually open a new tab, paste that zapier uri, everything works perfectly.
Seems like a typical CORS issue, but none the popular solutions works for me:
Add Access-Control-Allow-Origin: I'm using this oauth2orize
library, and sending reponse to preflight seems also part of the
library. So I cannot add header.
Using cors: Have tried app.use(cors()) and app.options('*',
cors()) which should apply to all routes, but simply not working.
My webapp sits on a node express server, in front of which there's an nginx proxy server.
Any idea where the problem might be is appreciated.
The issue that error message indicates isn’t caused by the app code running at https://myurl/. Instead it’s just that https://zapier.com/dashboard/auth/… doesn’t seem to support CORS.
Specifically, the response from that https://zapier.com/dashboard/auth/… URL doesn’t include the Access-Control-Allow-Origin response header, so your browser won’t let your frontend JavaScript code access the response.
It seems like that is all intentional on the part of Zapier—they don’t intend for that auth endpoint to be accessed from frontend AJAX/XHR/Fetch code running in a browser. Instead I guess it’s intended that you only access that auth endpoint from your backend code. Or something.
Anyway there is no way from your side that you can fix the fact the response from that Zapier API endpoint doesn’t include Access-Control-Allow-Origin.
And as long as it doesn’t include Access-Control-Allow-Origin, your browser blocks your frontend code from being able to get to the response—and there’s no way to get your browser to behave otherwise as long as your frontend code is trying to hit that API endpoint directly.
So the only solution is to not hit that API endpoint directly from your frontend code but to instead either set up a proxy and change your frontend code to make the request through that, or else just handle it in some other way in your existing backend code, as mentioned above.
The answer at Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not? gives some details on how you can set up a special CORS proxy, if you want to go that route.

Resources