Skip to content
Merged
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
42 changes: 37 additions & 5 deletions stdlib/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ impl Resolver for SafeResolver {
use std::sync::Mutex;
use std::thread;
use techscript_runtime::{
context::RuntimeContext, error::RuntimeError, function::Callable, value::RuntimeValue,
context::{Capability, RuntimeContext},
error::RuntimeError,
function::Callable,
value::RuntimeValue,
};

static SERVER_RUNNING: AtomicBool = AtomicBool::new(false);
Expand Down Expand Up @@ -382,7 +385,17 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "start".to_string(),
arity: 2,
callback: |_ctx, args| {
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied"
.to_string(),
),
None,
None,
));
}
let port = args[0].try_into_int().map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
Expand Down Expand Up @@ -442,7 +455,17 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "serve".to_string(),
arity: 1,
callback: |_ctx, args| {
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied"
.to_string(),
),
None,
None,
));
}
let port = args[0].try_into_int().map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
Expand Down Expand Up @@ -511,7 +534,16 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "fetch".to_string(),
arity: 1,
callback: |_ctx, args| {
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied".to_string(),
),
None,
None,
));
}
let url = args[0].to_string();

if !is_safe_url(&url) {
Expand Down Expand Up @@ -592,7 +624,7 @@ impl StdlibRegistry {
name: "std.web".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
required_capabilities: vec![Capability::Network],
},
);
}
Expand Down
Loading