Commit e4002f0
committed
feat: add CORS-aware ETag generation modes
Add support for including response headers in ETag calculation to prevent
cache conflicts when serving content to multiple origins through CDNs.
This addresses an issue where CDNs return 304 Not Modified responses that
omit CORS headers, causing browsers to apply cached CORS headers from a
different origin, resulting in CORS errors.
New ETag modes:
- 'weak-cors': Weak ETag including Access-Control-Allow-Origin header
- 'strong-cors': Strong ETag including Access-Control-Allow-Origin header
The implementation:
- Extends createETagGenerator to accept includeHeaders option
- Updates res.send() to pass response headers to ETag function
- Maintains full backward compatibility with existing ETag modes
- Falls back to body-only hashing when CORS headers are not present
Usage:
app.set('etag', 'weak-cors');
app.use(function(req, res) {
res.set('Access-Control-Allow-Origin', req.get('Origin'));
res.send('content');
});
Test coverage:
- 13 new unit tests in test/utils.js
- 10 new integration tests in test/res.send.cors.js
- All existing tests pass (1269 total)
Fixes #59861 parent f267d2c commit e4002f0
4 files changed
+433
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
190 | 190 | | |
191 | 191 | | |
192 | 192 | | |
193 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
194 | 196 | | |
195 | 197 | | |
196 | 198 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
53 | 83 | | |
54 | 84 | | |
55 | 85 | | |
| |||
144 | 174 | | |
145 | 175 | | |
146 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
147 | 183 | | |
148 | 184 | | |
149 | 185 | | |
| |||
155 | 191 | | |
156 | 192 | | |
157 | 193 | | |
| 194 | + | |
158 | 195 | | |
159 | 196 | | |
160 | 197 | | |
161 | 198 | | |
162 | | - | |
| 199 | + | |
163 | 200 | | |
164 | 201 | | |
165 | 202 | | |
| |||
174 | 211 | | |
175 | 212 | | |
176 | 213 | | |
177 | | - | |
| 214 | + | |
178 | 215 | | |
179 | 216 | | |
180 | 217 | | |
| |||
242 | 279 | | |
243 | 280 | | |
244 | 281 | | |
| 282 | + | |
| 283 | + | |
245 | 284 | | |
246 | 285 | | |
247 | 286 | | |
248 | 287 | | |
249 | 288 | | |
250 | | - | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
251 | 293 | | |
252 | 294 | | |
253 | | - | |
| 295 | + | |
254 | 296 | | |
255 | | - | |
256 | | - | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
257 | 321 | | |
258 | 322 | | |
259 | 323 | | |
260 | | - | |
| 324 | + | |
261 | 325 | | |
262 | | - | |
263 | | - | |
| 326 | + | |
| 327 | + | |
264 | 328 | | |
265 | 329 | | |
266 | 330 | | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
271 | 342 | | |
0 commit comments