Skip to content

fix: Move off atty to resolve soundness issue #11420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ name = "cargo"
path = "src/cargo/lib.rs"

[dependencies]
atty = "0.2"
bytesize = "1.0"
cargo-platform = { path = "crates/cargo-platform", version = "0.1.2" }
cargo-util = { path = "crates/cargo-util", version = "0.2.3" }
Expand All @@ -37,6 +36,7 @@ http-auth = { version = "0.1.6", default-features = false }
humantime = "2.0.0"
indexmap = "1"
ignore = "0.4.7"
is-terminal = "0.4.0"
lazy_static = "1.2.0"
jobserver = "0.1.24"
lazycell = "1.2.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/resolver-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ cargo-util = { path = "../cargo-util" }
proptest = "0.9.1"
lazy_static = "1.3.0"
varisat = "0.2.1"
atty = "0.2.11"
is-terminal = "0.4.0"
2 changes: 1 addition & 1 deletion crates/resolver-tests/tests/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use proptest::prelude::*;
proptest! {
#![proptest_config(ProptestConfig {
max_shrink_iters:
if is_ci() || !atty::is(atty::Stream::Stderr) {
if is_ci() || !is_terminal::IsTerminal::is_terminal(&std::io::stderr()){
// This attempts to make sure that CI will fail fast,
0
} else {
Expand Down
33 changes: 22 additions & 11 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;
use std::io::prelude::*;

use is_terminal::IsTerminal;
use termcolor::Color::{Cyan, Green, Red, Yellow};
use termcolor::{self, Color, ColorSpec, StandardStream, WriteColor};

Expand Down Expand Up @@ -99,14 +100,10 @@ impl Shell {
let auto_clr = ColorChoice::CargoAuto;
Shell {
output: ShellOut::Stream {
stdout: StandardStream::stdout(
auto_clr.to_termcolor_color_choice(atty::Stream::Stdout),
),
stderr: StandardStream::stderr(
auto_clr.to_termcolor_color_choice(atty::Stream::Stderr),
),
stdout: StandardStream::stdout(auto_clr.to_termcolor_color_choice(Stream::Stdout)),
stderr: StandardStream::stderr(auto_clr.to_termcolor_color_choice(Stream::Stderr)),
color_choice: ColorChoice::CargoAuto,
stderr_tty: atty::is(atty::Stream::Stderr),
stderr_tty: std::io::stderr().is_terminal(),
},
verbosity: Verbosity::Verbose,
needs_clear: false,
Expand Down Expand Up @@ -301,8 +298,8 @@ impl Shell {
),
};
*color_choice = cfg;
*stdout = StandardStream::stdout(cfg.to_termcolor_color_choice(atty::Stream::Stdout));
*stderr = StandardStream::stderr(cfg.to_termcolor_color_choice(atty::Stream::Stderr));
*stdout = StandardStream::stdout(cfg.to_termcolor_color_choice(Stream::Stdout));
*stderr = StandardStream::stderr(cfg.to_termcolor_color_choice(Stream::Stderr));
}
Ok(())
}
Expand Down Expand Up @@ -496,12 +493,12 @@ impl ShellOut {

impl ColorChoice {
/// Converts our color choice to termcolor's version.
fn to_termcolor_color_choice(self, stream: atty::Stream) -> termcolor::ColorChoice {
fn to_termcolor_color_choice(self, stream: Stream) -> termcolor::ColorChoice {
match self {
ColorChoice::Always => termcolor::ColorChoice::Always,
ColorChoice::Never => termcolor::ColorChoice::Never,
ColorChoice::CargoAuto => {
if atty::is(stream) {
if stream.is_terminal() {
termcolor::ColorChoice::Auto
} else {
termcolor::ColorChoice::Never
Expand All @@ -511,6 +508,20 @@ impl ColorChoice {
}
}

enum Stream {
Stdout,
Stderr,
}

impl Stream {
fn is_terminal(self) -> bool {
match self {
Self::Stdout => std::io::stdout().is_terminal(),
Self::Stderr => std::io::stderr().is_terminal(),
}
}
}

#[cfg(unix)]
mod imp {
use super::{Shell, TtyWidth};
Expand Down