You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CORS is a [Node.js](https://nodejs.org/en/) package for providing a [Connect](https://github.com/senchalabs/connect)/[Express](https://expressjs.com/) middleware that can be used to enable [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) with various options.
8
+
CORS is a [Node.js](https://nodejs.org/en/) middleware for [Express](https://expressjs.com/)/[Connect](https://github.com/senchalabs/connect) that sets [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) response headers. These headers tell browsers which origins can read responses from your server.
9
+
10
+
> [!IMPORTANT]
11
+
> **How CORS Works:** This package sets response headers—it doesn't block requests. CORS is enforced by browsers: they check the headers and decide if JavaScript can read the response. Non-browser clients (curl, Postman, other servers) ignore CORS entirely. See the [MDN CORS guide](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) for details.
9
12
10
13
*[Installation](#installation)
11
14
*[Usage](#usage)
@@ -16,6 +19,7 @@ CORS is a [Node.js](https://nodejs.org/en/) package for providing a [Connect](ht
16
19
*[Enabling CORS Pre-Flight](#enabling-cors-pre-flight)
17
20
*[Customizing CORS Settings Dynamically per Request](#customizing-cors-settings-dynamically-per-request)
18
21
*[Configuration Options](#configuration-options)
22
+
*[Common Misconceptions](#common-misconceptions)
19
23
*[License](#license)
20
24
*[Original Author](#original-author)
21
25
@@ -38,14 +42,15 @@ var express = require('express')
38
42
var cors =require('cors')
39
43
var app =express()
40
44
45
+
// Adds headers: Access-Control-Allow-Origin: *
41
46
app.use(cors())
42
47
43
48
app.get('/products/:id', function (req, res, next) {
44
-
res.json({msg:'This is CORS-enabled for all origins!'})
49
+
res.json({msg:'Hello'})
45
50
})
46
51
47
52
app.listen(80, function () {
48
-
console.log('CORS-enabled web server listening on port 80')
53
+
console.log('web server listening on port 80')
49
54
})
50
55
```
51
56
@@ -56,12 +61,13 @@ var express = require('express')
56
61
var cors =require('cors')
57
62
var app =express()
58
63
64
+
// Adds headers: Access-Control-Allow-Origin: *
59
65
app.get('/products/:id', cors(), function (req, res, next) {
60
-
res.json({msg:'This is CORS-enabled for a Single Route'})
66
+
res.json({msg:'Hello'})
61
67
})
62
68
63
69
app.listen(80, function () {
64
-
console.log('CORS-enabled web server listening on port 80')
70
+
console.log('web server listening on port 80')
65
71
})
66
72
```
67
73
@@ -79,12 +85,13 @@ var corsOptions = {
79
85
optionsSuccessStatus:200// some legacy browsers (IE11, various SmartTVs) choke on 204
origin:'http://mydomain.com',// Allow only a specific origin
186
-
credentials:true, // Enable cookies and credentials
194
+
origin:'http://mydomain.com',
195
+
credentials:true
187
196
};
188
197
} else {
189
-
corsOptions = { origin:'*' }; // Allow all origins for other routes
198
+
// Access-Control-Allow-Origin: *
199
+
corsOptions = { origin:'*' };
190
200
}
191
201
callback(null, corsOptions);
192
202
};
193
203
194
204
app.use(cors(dynamicCorsOptions));
195
205
196
206
app.get('/auth/connect/twitter', function (req, res) {
197
-
res.send('CORS dynamically applied for Twitter authentication.');
207
+
res.send('Hello');
198
208
});
199
209
200
210
app.get('/public', function (req, res) {
201
-
res.send('Public data with open CORS.');
211
+
res.send('Hello');
202
212
});
203
213
204
214
app.listen(80, function () {
205
-
console.log('CORS-enabled web server listening on port 80')
215
+
console.log('web server listening on port 80')
206
216
})
207
217
```
208
218
@@ -235,7 +245,19 @@ The default configuration is the equivalent of:
235
245
}
236
246
```
237
247
238
-
For details on the effect of each CORS header, read [this](https://web.dev/articles/cross-origin-resource-sharing) article.
248
+
## Common Misconceptions
249
+
250
+
### "CORS blocks requests from disallowed origins"
251
+
252
+
**No.** Your server receives and processes every request. CORS headers tell the browser whether JavaScript can read the response—not whether the request is allowed.
253
+
254
+
### "CORS protects my API from unauthorized access"
255
+
256
+
**No.** CORS is not access control. Any HTTP client (curl, Postman, another server) can call your API regardless of CORS settings. Use authentication and authorization to protect your API.
257
+
258
+
### "Setting `origin: 'http://example.com'` means only that domain can access my server"
259
+
260
+
**No.** It means browsers will only let JavaScript from that origin read responses. The server still responds to all requests.
In addition to providing a static object, you can also pass a callback function to dynamically generate the cookie options for each request. The callback receives the `req` object as its argument and should return an object containing the cookie settings.
53
+
54
+
```js
55
+
var app =express()
56
+
app.use(session({
57
+
secret:'keyboard cat',
58
+
resave:false,
59
+
saveUninitialized:true,
60
+
cookie:function(req) {
61
+
var match =req.url.match(/^\/([^/]+)/);
62
+
return {
63
+
path: match ?'/'+ match[1] :'/',
64
+
httpOnly:true,
65
+
secure:req.secure||false,
66
+
maxAge:60000
67
+
}
68
+
}
69
+
}))
70
+
```
71
+
52
72
The following are options that can be set in this object.
53
73
54
74
##### cookie.domain
@@ -130,6 +150,7 @@ By default, this is `false`.
130
150
-`'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
131
151
-`'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
132
152
-`'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
153
+
-`'auto'` will set the `SameSite` attribute to `None` for secure connections and `Lax` for non-secure connections.
133
154
134
155
More information about the different enforcement levels can be found in
135
156
[the specification][rfc-6265bis-03-4.1.2.7].
@@ -141,6 +162,14 @@ the future. This also means many clients may ignore this attribute until they un
141
162
that requires that the `Secure` attribute be set to `true` when the `SameSite` attribute has been
142
163
set to `'none'`. Some web browsers or other clients may be adopting this specification.
143
164
165
+
The `cookie.sameSite` option can also be set to the special value `'auto'` to have
166
+
this setting automatically match the determined security of the connection. When the connection
167
+
is secure (HTTPS), the `SameSite` attribute will be set to `None` to enable cross-site usage.
168
+
When the connection is not secure (HTTP), the `SameSite` attribute will be set to `Lax` for
169
+
better security while maintaining functionality. This is useful when the Express `"trust proxy"`
170
+
setting is properly setup to simplify development vs production configuration, particularly
171
+
for SAML authentication scenarios.
172
+
144
173
##### cookie.secure
145
174
146
175
Specifies the `boolean` value for the `Secure``Set-Cookie` attribute. When truthy,
The Express team and community take all security bugs in Express seriously.
311
-
Thank you for improving the security of Express. We appreciate your efforts and
312
-
responsible disclosure and will make every effort to acknowledge your
313
-
contributions.
311
+
> [!IMPORTANT]
312
+
> Before reporting a vulnerability, please review the [Express Threat Model](#the-express-threat-model) to check if the issue falls within Express's security scope.
314
313
315
-
Report security bugs by emailing `express-security@lists.openjsf.org`.
314
+
The Express team and community take all security vulnerabilities seriously.
315
+
Thank you for improving the security of Express and related projects.
316
+
We appreciate your efforts in responsible disclosure and will make every effort
317
+
to acknowledge your contributions.
316
318
317
-
To ensure the timely response to your report, please ensure that the entirety
318
-
of the report is contained within the email body and not solely behind a web
319
-
link or an attachment.
319
+
A [Security triage team member](https://github.com/expressjs/security-wg#security-triage-team-expressjssecurity-triage)
320
+
or [the repo captain](https://github.com/expressjs/discussions/blob/master/docs/contributing/captains_and_committers.md)
321
+
will acknowledge your report as soon as possible.
322
+
These timelines may extend when our triage
323
+
volunteers are away on holiday, particularly at the end of the year.
320
324
321
-
The lead maintainer will acknowledge your email within 48 hours, and will send a
322
-
more detailed response within 48 hours indicating the next steps in handling
323
-
your report. After the initial reply to your report, the security team will
325
+
After the initial reply to your report, the security team will
324
326
endeavor to keep you informed of the progress towards a fix and full
325
327
announcement, and may ask for additional information or guidance.
326
328
327
-
Report security bugs in third-party modules to the person or team maintaining
328
-
the module.
329
+
> [!NOTE]
330
+
> You can find more information about our process in [this guide](https://github.com/expressjs/security-wg/blob/main/docs/incident_response_plan.md)
329
331
330
-
### Pre-release Versions
331
332
332
-
Alpha and Beta releases are unstable and **not suitable for production use**.
333
-
Vulnerabilities found in pre-releases should be reported according to the [Reporting a Bug](#reporting-a-bug) section.
334
-
Due to the unstable nature of the branch it is not guaranteed that any fixes will be released in the next pre-release.
333
+
#### Reporting Security Bugs via GitHub Security Advisory (Preferred)
334
+
335
+
The preferred way to report security vulnerabilities is through
1. Visit the **Security** tab of the affected repository on GitHub.
343
+
2. Click **Report a vulnerability** and follow the provided steps.
344
+
345
+
This process applies to any repositories within the Express ecosystem.
346
+
If you are unsure whether a repository falls under this policy,
347
+
feel free to reach out via email.
348
+
349
+
#### Reporting via Email
350
+
351
+
If you prefer, you can also report security issues by emailing `express-security@lists.openjsf.org`.
352
+
353
+
To ensure a timely response, please include all relevant details directly in the email body rather than linking to external sources or attaching files.
354
+
355
+
The lead maintainer will acknowledge your email within 48 hours and provide an initial response outlining the next steps. The security team will keep you updated on the progress and may request additional details.
356
+
357
+
#### Third-Party Modules
358
+
359
+
If the security issue pertains to a third-party module that is not directly maintained within the Express ecosystem, please report it to the maintainers of that module.
335
360
336
361
### Disclosure Policy
337
362
@@ -344,15 +369,19 @@ involving the following steps:
344
369
* Prepare fixes for all releases still under maintenance. These fixes will be
345
370
released as fast as possible to npm.
346
371
347
-
### The Express Threat Model
348
-
349
-
We are currently working on a new version of the security model, the most updated version can be found [here](https://github.com/expressjs/security-wg/blob/main/docs/ThreatModel.md)
350
-
351
372
### Comments on this Policy
352
373
353
374
If you have suggestions on how this process could be improved please submit a
354
375
pull request.
355
376
377
+
### The Express Threat Model
378
+
379
+
The Express threat model defines the boundaries of what the framework considers its security responsibility. It establishes which elements are trusted (such as the developer, the runtime environment, and application code) versus untrusted (such as data from network connections). Issues arising from trusted elements are considered out of scope, while Express is responsible for safely handling untrusted data.
380
+
381
+
Many commonly reported concerns fall outside Express's security scope and are the responsibility of the application developer. Such as prototype pollution from unsanitized user input, misconfigured static file serving, or issues in third-party dependencies.
382
+
383
+
For complete details, see the [Express Threat Model](https://github.com/expressjs/security-wg/blob/main/docs/ThreatModel.md).
384
+
356
385
----
357
386
# Contributing to Expressjs.com {#expressjs-website-contributing}
0 commit comments