Commit fdb87ba
fix(serve-static): correct Range header parsing edge cases (#372)
* fix(serve-static): correct Range header parsing edge cases
- `bytes=-N` (suffix range) now returns the last N bytes instead of
the first N+1 bytes.
- `bytes=0-0` now returns 1 byte instead of the whole file (the old
`parseInt(...) || size - 1` treated a valid 0 as falsy).
- An unsatisfiable range (start beyond EOF, or start > end) now
returns 416 instead of crashing createReadStream with a RangeError.
* fix(serve-static): reject non-digit range values and fix empty-file crash
- Range segments with trailing garbage (e.g. `bytes=0-1x`) were silently
truncated by parseInt into `0-1` instead of being treated as malformed.
first-pos/last-pos/suffix-length are now validated as `1*DIGIT`.
- A Range request on an empty file fell through to createReadStream with
end=-1 and crashed with 500 instead of returning 416.
Addresses review feedback from @usualoma on #372.
* refactor(serve-static): separate range parsing and resolution
---------
Co-authored-by: Taku Amano <taku@taaas.jp>1 parent 912e3fd commit fdb87ba
2 files changed
Lines changed: 135 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 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 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
38 | 87 | | |
39 | 88 | | |
40 | 89 | | |
| |||
151 | 200 | | |
152 | 201 | | |
153 | 202 | | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
159 | 208 | | |
| 209 | + | |
160 | 210 | | |
161 | | - | |
162 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
163 | 218 | | |
164 | | - | |
165 | | - | |
| 219 | + | |
| 220 | + | |
166 | 221 | | |
167 | | - | |
| 222 | + | |
| 223 | + | |
168 | 224 | | |
169 | 225 | | |
170 | 226 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
188 | 257 | | |
189 | 258 | | |
190 | 259 | | |
| |||
0 commit comments