Code Monkey home page Code Monkey logo

node-http-cors's Introduction

Cross-Origin Resource Sharing

Simplifies the handling of Cross-origin Resource Sharing (CORS).

Features

  • Use CORS as a middleware in Express.
  • Set the CORS headers of an HTTP response.
  • Configure CORS not only for the entire server, but also for individual routes.

Note

This is a lightweight alternative library to cors with no dependencies.

Information

The Same-Origin Policy (SOP) is a browser security mechanism that restricts some HTTP requests made between different origins. It helps isolate potentially malicious documents and reducing possible attack vectors. Two URLs are part of the same origin if they have the same scheme, host, and port.

The following interactions are not affected by SOP: links, redirects, form submissions and HTML embedding.

The CORS consists of a set of HTTP headers that indicates whether a request can be shared cross-origin.

A preflight request is a "light" HTTP request using the OPTIONS method that checks to see if the CORS protocol is understood in order to determine if the actual request is safe to send, this also helps to prevent the server from unnecessarily performing actions from untrusted origins.

Requests that meet all of the following conditions do not trigger a preflight request:

Note

If the user agent does not trigger a preflight request (i.e. the request meets the criteria for a simple request), the server will not have the possibility to block the actual request beforehand, which will cause the request to be processed normally, although the response will be blocked by the browser if it does not satisfy the CORS.

The Access-Control-Expose-Headers and Access-Control-Allow-Credentials headers can also be included in both types of requests.

The Access-Control-Allow-Credentials header works in conjunction with the credentials mode of the Fetch API.

  • Tells browsers whether to expose the response to the frontend JavaScript code.
  • In a preflight request, indicates whether the actual request can be made using credentials.

Note

  • Credentials are cookies, authorization headers, or TLS client certificates.
  • Credentials are used when the user agent should send or receive them in cross-origin requests.
  • By default, credentials are sent if the request is on the same origin as the calling script.
  • Some cookies may still be sent if the request is same-site, regardless of whether it is cross-origin or not.
// Using Fetch with credentials.
await fetch('http://example.com/', {
    credentials: 'include'
});

Warning

Sharing responses and allowing requests with credentials is rather unsafe, and extreme care has to be taken to avoid the confused deputy problem. See CORS protocol and credentials.

In addition to CORS, the HTTP Content Security Policy (CSP) response header extends an additional level of resource-level security to a website by preventing content from another source from being loaded. This helps guard against Cross-site Scripting (XSS) attacks.

Further reading:

Installation

npm i flipeador/node-http-cors#semver:^1.0.0

Examples

Express

import express from 'express';
import cors from '@flipeador/node-http-cors';

const app = express();

app.use(cors({
    // Indicates the allowed origins.
    // The wildcard (*) cannot be used with credentials.
    origin: [
        /^https?:\/\/localhost:\d+/u,
        /^https?:\/\/192.168.\d+.\d+:\d+/u,
        'https://www.example.com'
    ],
    // Allows cross-origin credentials.
    // await fetch(..., { credentials: 'include' });
    credentials: true
}));
app.use(express.json());

app.listen(8080, () => {
    console.log('Server is running!');
});

License

This project is licensed under the Apache License 2.0. See the license file for details.

node-http-cors's People

Contributors

flipeador avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.