mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
Fix deno.open permission check (#1336)
This commit is contained in:
parent
40d6daf824
commit
2cb52cc4d1
4 changed files with 54 additions and 7 deletions
12
js/deno.ts
12
js/deno.ts
|
@ -4,7 +4,17 @@
|
|||
/// <amd-module name="deno"/>
|
||||
export { env, exit } from "./os";
|
||||
export { chdir, cwd } from "./dir";
|
||||
export { File, open, stdin, stdout, stderr, read, write, close } from "./files";
|
||||
export {
|
||||
File,
|
||||
open,
|
||||
stdin,
|
||||
stdout,
|
||||
stderr,
|
||||
read,
|
||||
write,
|
||||
close,
|
||||
OpenMode
|
||||
} from "./files";
|
||||
export {
|
||||
copy,
|
||||
toAsyncIterator,
|
||||
|
|
|
@ -29,7 +29,7 @@ export const stdout = new File(1);
|
|||
/** An instance of `File` for stderr. */
|
||||
export const stderr = new File(2);
|
||||
|
||||
type OpenMode =
|
||||
export type OpenMode =
|
||||
/** Read-only. Default. Starts at beginning of file. */
|
||||
| "r"
|
||||
/** Read-write. Start at beginning of file. */
|
||||
|
|
|
@ -30,10 +30,26 @@ test(async function filesToAsyncIterator() {
|
|||
assertEqual(totalSize, 12);
|
||||
});
|
||||
|
||||
testPerm({ write: false }, async function writePermFailure() {
|
||||
const filename = "tests/hello.txt";
|
||||
const writeModes: deno.OpenMode[] = ["r+", "w", "w+", "a", "a+", "x", "x+"];
|
||||
for (const mode of writeModes) {
|
||||
let err;
|
||||
try {
|
||||
await deno.open(filename, mode);
|
||||
} catch (e) {
|
||||
err = e;
|
||||
}
|
||||
assert(!!err);
|
||||
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
|
||||
assertEqual(err.name, "PermissionDenied");
|
||||
}
|
||||
});
|
||||
|
||||
testPerm({ write: true }, async function createFile() {
|
||||
const tempDir = await deno.makeTempDir();
|
||||
const filename = tempDir + "/test.txt";
|
||||
let f = await deno.open(filename, "w");
|
||||
const f = await deno.open(filename, "w");
|
||||
let fileInfo = deno.statSync(filename);
|
||||
assert(fileInfo.isFile());
|
||||
assert(fileInfo.len === 0);
|
||||
|
@ -88,7 +104,7 @@ testPerm({ write: true }, async function openModeWriteRead() {
|
|||
const filename = tempDir + "hello.txt";
|
||||
const data = encoder.encode("Hello world!\n");
|
||||
|
||||
let file = await deno.open(filename, "w+");
|
||||
const file = await deno.open(filename, "w+");
|
||||
// assert file was created
|
||||
let fileInfo = deno.statSync(filename);
|
||||
assert(fileInfo.isFile());
|
||||
|
|
27
src/ops.rs
27
src/ops.rs
|
@ -576,15 +576,15 @@ fn op_chmod(
|
|||
}
|
||||
|
||||
fn op_open(
|
||||
_state: &IsolateState,
|
||||
state: &IsolateState,
|
||||
base: &msg::Base,
|
||||
data: libdeno::deno_buf,
|
||||
) -> Box<Op> {
|
||||
assert_eq!(data.len(), 0);
|
||||
let cmd_id = base.cmd_id();
|
||||
let inner = base.inner_as_open().unwrap();
|
||||
let filename = PathBuf::from(inner.filename().unwrap());
|
||||
// let perm = inner.perm();
|
||||
let filename_str = inner.filename().unwrap();
|
||||
let filename = PathBuf::from(&filename_str);
|
||||
let mode = inner.mode().unwrap();
|
||||
|
||||
let mut open_options = tokio::fs::OpenOptions::new();
|
||||
|
@ -594,12 +594,21 @@ fn op_open(
|
|||
open_options.read(true);
|
||||
}
|
||||
"r+" => {
|
||||
if let Err(e) = state.check_write(&filename_str) {
|
||||
return odd_future(e);
|
||||
}
|
||||
open_options.read(true).write(true);
|
||||
}
|
||||
"w" => {
|
||||
if let Err(e) = state.check_write(&filename_str) {
|
||||
return odd_future(e);
|
||||
}
|
||||
open_options.create(true).write(true).truncate(true);
|
||||
}
|
||||
"w+" => {
|
||||
if let Err(e) = state.check_write(&filename_str) {
|
||||
return odd_future(e);
|
||||
}
|
||||
open_options
|
||||
.read(true)
|
||||
.create(true)
|
||||
|
@ -607,15 +616,27 @@ fn op_open(
|
|||
.truncate(true);
|
||||
}
|
||||
"a" => {
|
||||
if let Err(e) = state.check_write(&filename_str) {
|
||||
return odd_future(e);
|
||||
}
|
||||
open_options.create(true).append(true);
|
||||
}
|
||||
"a+" => {
|
||||
if let Err(e) = state.check_write(&filename_str) {
|
||||
return odd_future(e);
|
||||
}
|
||||
open_options.read(true).create(true).append(true);
|
||||
}
|
||||
"x" => {
|
||||
if let Err(e) = state.check_write(&filename_str) {
|
||||
return odd_future(e);
|
||||
}
|
||||
open_options.create_new(true).write(true);
|
||||
}
|
||||
"x+" => {
|
||||
if let Err(e) = state.check_write(&filename_str) {
|
||||
return odd_future(e);
|
||||
}
|
||||
open_options.create_new(true).read(true).write(true);
|
||||
}
|
||||
&_ => {
|
||||
|
|
Loading…
Reference in a new issue