Skip to content

Commit

Permalink
fix(permissions): lock stdio streams when prompt is shown (#17392)
Browse files Browse the repository at this point in the history
This commit changes permission prompt to lock stdio streams when prompt
is shown.
  • Loading branch information
bartlomieju committed Jan 13, 2023
1 parent 9644220 commit 1b17985
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3723,3 +3723,23 @@ fn file_fetcher_preserves_permissions() {
assert_contains!(output, "true");
});
}

#[test]
fn stdio_streams_are_locked_in_permission_prompt() {
let _guard = util::http_server();
util::with_pty(&[
"repl",
"--allow-read=run/stdio_streams_are_locked_in_permission_prompt/worker.js,run/stdio_streams_are_locked_in_permission_prompt/text.txt"
], |mut console| {
console.write_line(
r#"new Worker(`${Deno.cwd()}/run/stdio_streams_are_locked_in_permissions_prompt/worker.js`, { type: "module" });
await Deno.writeTextFile("./run/stdio_streams_are_locked_in_permissions_prompt/text.txt", "some code");"#,
);
console.write_line("y");
console.write_line("close();");
let output = console.read_all_output();

let expected_output = r#"\x1b[1;1H\x1b[0JAre you sure you want to continue?"#;
assert_eq!(output, expected_output);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\x1B[2J\x1B[1;1H
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.clear();
console.log("Are you sure you want to continue?");
8 changes: 8 additions & 0 deletions runtime/permissions/prompter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ impl PermissionPrompter for TtyPrompter {
return PromptResponse::Deny; // don't grant permission if this fails
}

// Lock stdio streams, so no other output is written while the prompt is
// displayed.
let _stdout_guard = std::io::stdout().lock();
let _stderr_guard = std::io::stderr().lock();

// print to stderr so that if stdout is piped this is still displayed.
const OPTS: &str = "[y/n] (y = yes, allow; n = no, deny)";
eprint!("{} ┌ ", PERMISSION_EMOJI);
Expand Down Expand Up @@ -238,6 +243,9 @@ impl PermissionPrompter for TtyPrompter {
};
};

drop(_stdout_guard);
drop(_stderr_guard);

value
}
}
Expand Down

0 comments on commit 1b17985

Please sign in to comment.