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

Restrict CORS to Specific Origins #10

Open
samanthasbytes opened this issue Dec 5, 2024 · 0 comments
Open

Restrict CORS to Specific Origins #10

samanthasbytes opened this issue Dec 5, 2024 · 0 comments
Assignees
Labels
security Related to security vulnerabilities or improvements.

Comments

@samanthasbytes
Copy link
Owner

samanthasbytes commented Dec 5, 2024

The app currently allows access from all origins using app.use(cors());. This is not best practice and poses a security risk. Update the CORS configuration to restrict access to trusted origins only.

Important Note Regarding CORS

Once you’ve set up your site’s name, you need to add your site’s URL to CORS’ allowed origins; otherwise, you’ll get CORS-related errors once you try to open your app’s URL.

Open your index.js file in your myFlix API project folder. If you have the following code as your CORS configuration, then it means you’ve allowed a specific set of origins to access your API.

const cors = require('cors');
let allowedOrigins = [...];

app.use(cors({
  origin: (origin, callback) => {
    if (!origin) return callback(null, true);
    if (allowedOrigins.indexOf(origin) === -1) { // If a specific origin isn’t found on the list of allowed origins
      let message = 'The CORS policy for this application doesn’t allow access from origin ' + origin;
      return callback(new Error(message), false);
    }
    return callback(null, true);
  }
}));

This means that you’ll need to add your site’s URL to the allowedOrigins array. If your site’s URL on Netlify is, for example, https://my-awesome-site123.netlify.app, then your array would need to look something like this:

let allowedOrigins = ['http://localhost:8080', 'http://testsite.com', 'http://localhost:1234', 'https://my-awesome-site123.netlify.app'];

Once done, commit and push the change to your myFlix API GitHub repository (make sure to switch the repository first in GitHub Desktop), then push the same change to Heroku by running git push heroku main from inside your myFlix API project folder in the terminal.

If, on the other hand, you’ve allowed access from all origins by having app.use(cors()); as your CORS configuration code (instead of the code listed earlier), then you shouldn’t get any CORS errors (but, as you know, this way isn’t recommended!).

@samanthasbytes samanthasbytes added the security Related to security vulnerabilities or improvements. label Dec 5, 2024
@samanthasbytes samanthasbytes self-assigned this Dec 5, 2024
@samanthasbytes samanthasbytes changed the title CORS Restrict CORS to Specific Origins Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
security Related to security vulnerabilities or improvements.
Projects
None yet
Development

No branches or pull requests

1 participant