Skip to content

Document and test the use of unsafe #412

Open
@sbosnick

Description

@sbosnick

A recent Medium article criticized 3 of the 7 rust http clients it reviewed because of their dependency on this crate and because of this crate's use of unsafe. The criticism does not appear to be founded (i.e. it claims that HeaderMap is a reimplementation of HashMap without explaining how to use HashMap as a multimap) and the author's tests of these 3 http clients did not show any exploitable problems related to this crate. The article appears to an example of withoutboats' post about allegations of vulnerabilities in Rust libraries.

Despite the limitations of the Medium article, http is (and is designed to be) a high-level dependency of many network exposed crates so the sound use of unsafe is important. The use of unsafe has already been audited (see for example rust-secure-code/safety-dance#37). In my preliminary review of the use of unsafe I found that they were sound (with one possible exception I am still investigating). The soundness, though, relies on implicit invariants that, in some instance, are not local to the use of unsafe.

I propose to try to increase the robustness (in the sense described here) of the use of unsafe in http by:

  • Adding unit tests for edge condition involving unsafe code paths
  • Refactoring the code involving unsafe into smaller leaf modules that present a safe wrapper around the unsafe code (without introducing performance regressions)
  • Documenting the invariants that make the use of unsafe sound (making explicit what is implicit)
  • Adding additional performance tests for uses-no-unsafe alternatives to HeaderMap (such as multimap) so that the performance trade-offs of the current implantation are more easily apparent

The goal isn't to reduce or eliminate the use of unsafe (though a reduction in in the number of uses of unsafe may be a by-product). It is rather to make the soundness of the use of unsafe easier for someone else to audit.

I plan to review the use of unsafe file by file as follows (the number in parenthesis is the number of occurrences of unsafe in the file):

  • byte_str.rs (2)
  • method.rs (2)
  • uri/path.rs (1)
  • uri/authority.rs (3)
  • uri/scheme.rs (1)
  • uri/mod.rs (3)
  • header/value.rs (4)
  • header/name.rs (11)
  • header/map.rs (19)

I have already completed the work for the first two files in #408 and #410. I am planning to continue with the three uri/* files and then the header/* files as time allows. If the maintainers of the project don't think this is worthwhile please let me know so I can move on to something else.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions