|
| 1 | +# Development Environment |
| 2 | + |
| 3 | +## `rustup` |
| 4 | + |
| 5 | +`rustup` is the Rust toolchain installer. Among other things, it enables |
| 6 | +switching between different flavors of the toolchain (stable, beta, nightly), |
| 7 | +managing additional components installation and keeping them up to date. |
| 8 | + |
| 9 | +> ### Warning: |
| 10 | +> From a security perspective, `rustup` does perform all downloads over HTTPS, |
| 11 | +> but doesn’t validate signatures of downloads. Protection against downgrade |
| 12 | +> attacks, certificate pinning, validation of signatures are works that are |
| 13 | +> currently in progress. In some cases, it may be preferable to opt for |
| 14 | +> an alternative installation method listed in the *Install* section of the |
| 15 | +> official rust website. |
| 16 | +
|
| 17 | +### Rust Editions |
| 18 | + |
| 19 | +Several flavors, called *editions*, of the Rust language coexist. |
| 20 | +The concept of editions has been introduced to clarify new features |
| 21 | +implementation and to make them incremental. But as stated in the |
| 22 | +[Edition Guide](https://rust-lang-nursery.github.io/edition-guide/editions/index.html), |
| 23 | +this doesn’t mean that new features and improvements will be shipped on |
| 24 | +the last edition only. |
| 25 | + |
| 26 | +However, some editions could bring new keywords and language constructs. |
| 27 | +Recommendations for secure applications development then remain closely |
| 28 | +linked to features that are used in such applications rather than the actual |
| 29 | +edition that is declared in it. |
| 30 | +In the rest of this guide, best effort will be made to highlight constructions |
| 31 | +and language features that are specific to a particular Rust edition. |
| 32 | + |
| 33 | +> ### Note: |
| 34 | +> No specific edition is recommended, as long as users follow recommendations |
| 35 | +> that are expressed in relation to features offered by edition that has been |
| 36 | +> chosen. |
| 37 | +
|
| 38 | +### Stable, nightly and beta toolchains |
| 39 | + |
| 40 | +Orthogonally to editions that allows one to select a flavor (a set of features) |
| 41 | +of the Rust language, the Rust toolchain is provided in three different |
| 42 | +versions, called *release channels*: |
| 43 | + |
| 44 | +- *nightly* releases are created once a day, |
| 45 | +- *nightly* releases are promoted every six weeks to *beta* releases, |
| 46 | +- *beta* releases are promoted every six weeks to *stable* releases. |
| 47 | + |
| 48 | +When playing with different toolchains, it is important to check not only what |
| 49 | +the default toolchain is, but also if overrides are currently set for some |
| 50 | +directories. |
| 51 | + |
| 52 | +```shell |
| 53 | +$ pwd |
| 54 | +/tmp/foo |
| 55 | +$ rustup toolchain list |
| 56 | +stable-x86_64-unknown-linux-gnu (default) |
| 57 | +beta-x86_64-unknown-linux-gnu |
| 58 | +nightly-x86_64-unknown-linux-gnu |
| 59 | +$ rustup override list |
| 60 | +/tmp/foo nightly-x86_64-unknown-linux-gnu |
| 61 | +$ |
| 62 | +``` |
| 63 | + |
| 64 | +> ### Recommendation: |
| 65 | +> In general, development of a secure application should be done using a |
| 66 | +> fully stable toolchain, for limiting potential compiler, runtime or tool |
| 67 | +> bugs. |
| 68 | +
|
| 69 | +When using a specific cargo subcommand that requires a nightly component, |
| 70 | +it is preferable to run it by switching the toolchain only locally, instead |
| 71 | +of explicitly switching the complete toolchain. For example, to run the |
| 72 | +(nightly) latest `rustfmt`: |
| 73 | + |
| 74 | +```shell |
| 75 | +$ rustup toolchain list |
| 76 | +stable-x86_64-unknown-linux-gnu (default) |
| 77 | +beta-x86_64-unknown-linux-gnu |
| 78 | +nightly-x86_64-unknown-linux-gnu |
| 79 | +$ rustup run nightly cargo fmt |
| 80 | +$ # or |
| 81 | +$ cargo +nightly fmt |
| 82 | +$ |
| 83 | +``` |
| 84 | + |
| 85 | +## `cargo` |
| 86 | + |
| 87 | +Once `rustup` has been used to set up the appropriate Rust toolchain, the |
| 88 | +tool `cargo` has been made available. It’s the Rust package manager, that |
| 89 | +provides ways to structure and build projects, managing on its own dependencies |
| 90 | +download among other tasks. It’s also a front-end to run complementary tools such |
| 91 | +as those that are described below, in the form of sub-commands. |
| 92 | + |
| 93 | +<mark>TODO</mark>: identify unsafe features and risky environment variables. |
| 94 | + |
| 95 | +### clippy |
| 96 | + |
| 97 | +Clippy is a tool that provides and checks many lints (bugs, styling, performance |
| 98 | +issues, etc.). Since the stable toolchain has reached version 1.29, `clippy` can |
| 99 | +be used within the stable rustup environment. It is also recommended |
| 100 | +to install `clippy` as a component (`rustup component add clippy`) in the |
| 101 | +stable toolchain instead of installing it as a project dependency. |
| 102 | + |
| 103 | +The tool comes with some lint categories regarding the kind of issue it aims to |
| 104 | +detect. The warnings should be re-checked by the programmer before committing |
| 105 | +the fix that is suggested by `clippy`, especially in the case of lints of the |
| 106 | +category `clippy::nursery` since those hints are still under development. |
| 107 | + |
| 108 | +> ### Recommendation: |
| 109 | +> The tool `clippy` must be used at various times during a secure application |
| 110 | +> development process. |
| 111 | +
|
| 112 | +### rustfmt |
| 113 | + |
| 114 | +<mark>TODO</mark>: introduce the rustfmt tool and recommend its use. |
| 115 | + |
| 116 | +### Others |
| 117 | + |
| 118 | +There exist other useful tools or cargo subcommands for enforcing program |
| 119 | +security whether by searching for specific code patterns or by providing |
| 120 | +convenient commands for testing or fuzzing. They are discussed in the following |
| 121 | +chapters, according to their goals. |
0 commit comments