Skip to content
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: 2 additions & 0 deletions fix_file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
# A script to patch file.rs
98 changes: 72 additions & 26 deletions stdlib/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::fs;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue};
use techscript_runtime::{
context::Capability,
error::{RuntimeError, RuntimeErrorKind},
value::RuntimeValue,
};

impl StdlibRegistry {
pub fn register_file(&mut self) {
Expand All @@ -14,12 +18,22 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "read".to_string(),
arity: 1,
callback: |_ctx, args| {
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: FileSystem capability is denied"
.to_string(),
),
None,
None,
));
}
let path = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::TypeMismatch {
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
Expand All @@ -30,9 +44,7 @@ impl StdlibRegistry {
};
let content = fs::read_to_string(&path).map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
e.to_string(),
),
RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
Expand All @@ -47,12 +59,22 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "write".to_string(),
arity: 2,
callback: |_ctx, args| {
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: FileSystem capability is denied"
.to_string(),
),
None,
None,
));
}
let path = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::TypeMismatch {
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
Expand All @@ -65,7 +87,7 @@ impl StdlibRegistry {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::TypeMismatch {
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
Expand All @@ -76,9 +98,7 @@ impl StdlibRegistry {
};
fs::write(&path, &content).map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
e.to_string(),
),
RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
Expand All @@ -93,12 +113,22 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "copy".to_string(),
arity: 2,
callback: |_ctx, args| {
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: FileSystem capability is denied"
.to_string(),
),
None,
None,
));
}
let src = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::TypeMismatch {
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
Expand All @@ -111,7 +141,7 @@ impl StdlibRegistry {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::TypeMismatch {
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
Expand All @@ -122,9 +152,7 @@ impl StdlibRegistry {
};
fs::copy(&src, &dest).map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
e.to_string(),
),
RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
Expand All @@ -139,12 +167,22 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "remove".to_string(),
arity: 1,
callback: |_ctx, args| {
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: FileSystem capability is denied"
.to_string(),
),
None,
None,
));
}
let path = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::TypeMismatch {
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
Expand All @@ -155,9 +193,7 @@ impl StdlibRegistry {
};
fs::remove_file(&path).map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
e.to_string(),
),
RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
Expand All @@ -172,12 +208,22 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "exists".to_string(),
arity: 1,
callback: |_ctx, args| {
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: FileSystem capability is denied"
.to_string(),
),
None,
None,
));
}
let path = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::TypeMismatch {
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
Expand All @@ -197,7 +243,7 @@ impl StdlibRegistry {
name: "std.file".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
required_capabilities: vec![Capability::FileSystem],
},
);
}
Expand Down
Loading