Skip to content

NamedFile: no panic on pre-epoch modification times or on an empty or degenerate Range - #37

Open
AlexeyMatskevich wants to merge 1 commit into
ntex-rs:mainfrom
AlexeyMatskevich:files-fix-panics
Open

AlexeyMatskevich wants to merge 1 commit into
ntex-rs:mainfrom
AlexeyMatskevich:files-fix-panics

Conversation

@AlexeyMatskevich

Copy link
Copy Markdown

With ntex-files, one request can panic the handler: an empty Range header, or a request for a file whose modification time is before 1970 or in year 10000 or later. A suffix range on an empty file overflows an integer subtraction, and Range: bytes=-0 on a non-empty file gets 206, although a zero-length range cannot be satisfied (RFC 9110 section 14.1.2).

What changes

  • A file modified before 1970 is served with 200, without ETag and without Last-Modified (test: test_modified_before_epoch).
  • With use_etag(false), such a file is served with 200 and without Last-Modified (test: test_modified_before_epoch_without_etag).
  • A file modified in year 10000 or later is served with 200 and an ETag, without Last-Modified (test: test_modified_after_year_9999).
  • A Range header with no range in it ("", bytes=, bytes=,, bytes= , ,) gets 416 with Content-Range: bytes */<len> (test: test_named_file_empty_range_header).
  • bytes=-0 gets 416 with Content-Range: bytes */<len>, also on an empty file (tests: test_named_file_zero_length_suffix_range, test_named_file_range_responses_match_the_selected_bytes).
  • HttpRange::parse skips a zero-length suffix range: bytes=-0 yields no range, bytes=-0,2-3 yields 2-3 (test: test_parse).
  • A non-zero suffix range such as bytes=-5 on an empty file gets 200 with an empty body and no Content-Range (test: test_named_file_suffix_range_of_empty_file).

Reference

actix-files fixed the same for pre-1970 files in actix/actix-web#3922.

This PR and "NamedFile: support If-Range" touch the same lines of ntex-files/src/lib.rs; the one merged second needs a rebase.

An empty Range header, or an mtime before 1970 or from year 10000 on,
panicked; bytes=-0 got 206. Now an empty Range and bytes=-0 get 416,
and files with such mtimes are served without the affected validator.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant