mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 04:39:10 +00:00
BREAKING: execPath should require allow-read (#5109)
This commit is contained in:
parent
76c77bb32c
commit
221221cc97
5 changed files with 29 additions and 22 deletions
2
cli/js/lib.deno.ns.d.ts
vendored
2
cli/js/lib.deno.ns.d.ts
vendored
|
@ -147,7 +147,7 @@ declare namespace Deno {
|
||||||
*
|
*
|
||||||
* console.log(Deno.execPath()); // e.g. "/home/alice/.local/bin/deno"
|
* console.log(Deno.execPath()); // e.g. "/home/alice/.local/bin/deno"
|
||||||
*
|
*
|
||||||
* Requires `allow-env` permission.
|
* Requires `allow-read` permission.
|
||||||
*/
|
*/
|
||||||
export function execPath(): string;
|
export function execPath(): string;
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,10 @@ unitTest(function envPermissionDenied2(): void {
|
||||||
// case-insensitive. Case normalization needs be done using the collation
|
// case-insensitive. Case normalization needs be done using the collation
|
||||||
// that Windows uses, rather than naively using String.toLowerCase().
|
// that Windows uses, rather than naively using String.toLowerCase().
|
||||||
unitTest(
|
unitTest(
|
||||||
{ ignore: Deno.build.os !== "windows", perms: { env: true, run: true } },
|
{
|
||||||
|
ignore: Deno.build.os !== "windows",
|
||||||
|
perms: { read: true, env: true, run: true },
|
||||||
|
},
|
||||||
async function envCaseInsensitive() {
|
async function envCaseInsensitive() {
|
||||||
// Utility function that runs a Deno subprocess with the environment
|
// Utility function that runs a Deno subprocess with the environment
|
||||||
// specified in `inputEnv`. The subprocess reads the environment variables
|
// specified in `inputEnv`. The subprocess reads the environment variables
|
||||||
|
@ -269,11 +272,11 @@ unitTest(function getDirWithoutPermission(): void {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest({ perms: { env: true } }, function execPath(): void {
|
unitTest({ perms: { read: true } }, function execPath(): void {
|
||||||
assertNotEquals(Deno.execPath(), "");
|
assertNotEquals(Deno.execPath(), "");
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest({ perms: { env: false } }, function execPathPerm(): void {
|
unitTest({ perms: { read: false } }, function execPathPerm(): void {
|
||||||
let caughtError = false;
|
let caughtError = false;
|
||||||
try {
|
try {
|
||||||
Deno.execPath();
|
Deno.execPath();
|
||||||
|
|
|
@ -82,8 +82,8 @@ fn op_exec_path(
|
||||||
_args: Value,
|
_args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
state.check_env()?;
|
|
||||||
let current_exe = env::current_exe().unwrap();
|
let current_exe = env::current_exe().unwrap();
|
||||||
|
state.check_read(¤t_exe)?;
|
||||||
// Now apply URL parser to current exe to get fully resolved path, otherwise
|
// Now apply URL parser to current exe to get fully resolved path, otherwise
|
||||||
// we might get `./` and `../` bits in `exec_path`
|
// we might get `./` and `../` bits in `exec_path`
|
||||||
let exe_url = Url::from_file_path(current_exe).unwrap();
|
let exe_url = Url::from_file_path(current_exe).unwrap();
|
||||||
|
|
|
@ -2421,6 +2421,27 @@ async fn inspector_does_not_hang() {
|
||||||
assert!(child.wait().unwrap().success());
|
assert!(child.wait().unwrap().success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn exec_path() {
|
||||||
|
let output = util::deno_cmd()
|
||||||
|
.current_dir(util::root_path())
|
||||||
|
.arg("run")
|
||||||
|
.arg("--allow-read")
|
||||||
|
.arg("cli/tests/exec_path.ts")
|
||||||
|
.stdout(std::process::Stdio::piped())
|
||||||
|
.spawn()
|
||||||
|
.unwrap()
|
||||||
|
.wait_with_output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(output.status.success());
|
||||||
|
let stdout_str = std::str::from_utf8(&output.stdout).unwrap().trim();
|
||||||
|
let actual =
|
||||||
|
std::fs::canonicalize(&std::path::Path::new(stdout_str)).unwrap();
|
||||||
|
let expected =
|
||||||
|
std::fs::canonicalize(deno::test_util::deno_exe_path()).unwrap();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
mod util {
|
mod util {
|
||||||
use deno::colors::strip_ansi_codes;
|
use deno::colors::strip_ansi_codes;
|
||||||
pub use deno::test_util::*;
|
pub use deno::test_util::*;
|
||||||
|
|
|
@ -31,23 +31,6 @@ class TestTarget(DenoTestCase):
|
||||||
result = run_output([self.deno_exe, "run", t], quiet=True)
|
result = run_output([self.deno_exe, "run", t], quiet=True)
|
||||||
assert result.out.strip() == "noColor false"
|
assert result.out.strip() == "noColor false"
|
||||||
|
|
||||||
def test_exec_path(self):
|
|
||||||
cmd = [
|
|
||||||
self.deno_exe, "run", "--allow-run", "--allow-env",
|
|
||||||
"cli/tests/exec_path.ts"
|
|
||||||
]
|
|
||||||
result = run_output(cmd, quiet=True)
|
|
||||||
print "exec_path", result
|
|
||||||
self.assertEqual(result.code, 0)
|
|
||||||
if os.name == "nt":
|
|
||||||
# When running in github actions, the windows drive letter of the
|
|
||||||
# executable path reported by deno has a different case than the one
|
|
||||||
# reported by python.
|
|
||||||
assert self.deno_exe.upper() in result.out.strip().upper()
|
|
||||||
assert self.deno_exe[1:] in result.out.strip()
|
|
||||||
else:
|
|
||||||
assert self.deno_exe in result.out.strip()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
run_tests()
|
run_tests()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue