Skip to content
Closed
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
32 changes: 28 additions & 4 deletions stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::net::TcpStream;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue};
use techscript_runtime::{context::Capability, error::RuntimeError, value::RuntimeValue};

impl StdlibRegistry {
pub fn register_socket(&mut self) {
Expand All @@ -14,7 +14,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "connect".to_string(),
arity: 2,
callback: |_ctx, args| {
callback: |ctx, args| {
let host = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
Expand Down Expand Up @@ -42,6 +42,18 @@ impl StdlibRegistry {
}
};
let addr = format!("{}:{}", host, port);

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,
));
}

TcpStream::connect(&addr).map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
Expand All @@ -61,7 +73,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "listen".to_string(),
arity: 1,
callback: |_ctx, args| {
callback: |ctx, args| {
let port = match &args[0] {
RuntimeValue::Int(n) => *n as u16,
_ => {
Expand All @@ -76,6 +88,18 @@ impl StdlibRegistry {
}
};
let addr = format!("0.0.0.0:{}", port);

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 _listener = std::net::TcpListener::bind(&addr).map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
Expand All @@ -96,7 +120,7 @@ impl StdlibRegistry {
name: "std.socket".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
required_capabilities: vec![Capability::Network],
},
);
}
Expand Down
4 changes: 1 addition & 3 deletions stdlib/tests/stdlib_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,5 @@ fn test_ai_generate_text() {
let val = res.unwrap();
assert!(val.as_string().unwrap().contains("Prompt: What is 2+2?"));
}

#[test]
<<
}
<
Loading