Skip to content

Commit

Permalink
fix: remove process, filter env variables (#174)
Browse files Browse the repository at this point in the history
Co-authored-by: Pratik Mishra <[email protected]>
  • Loading branch information
pratikmishra356 and Pratik Mishra authored Jul 18, 2024
1 parent cd8b659 commit 710c69d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 17 additions & 6 deletions crates/context_aware_config/src/validation_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::str;
use superposition_macros::{unexpected_error, validation_error};
use superposition_types::result as superposition;

static FUNCTION_ENV_VARIABLES: &str =
"HTTP_PROXY,HTTPS_PROXY,HTTP_PROXY_HOST,HTTP_PROXY_PORT,NO_PROXY";

fn type_check_validate(code_str: &str) -> String {
format!(
r#"const vm = require("node:vm")
Expand All @@ -23,7 +26,7 @@ fn type_check_validate(code_str: &str) -> String {
)
}

fn execute_validate_fun(code_str: &str, value: Value, key: String) -> String {
fn execute_validate_fun(code_str: &str, key: String, value: Value) -> String {
format!(
r#"
const vm = require("node:vm")
Expand All @@ -41,18 +44,26 @@ fn execute_validate_fun(code_str: &str, value: Value, key: String) -> String {
throw new Error(err)
}});\`);
script.runInNewContext({{axios,console,process}}, {{ timeout: 1500}});
script.runInNewContext({{axios,console}}, {{ timeout: 1500}});
"#,
code_str, value, key
code_str, key, value
)
}

fn generate_code(code_str: &str) -> String {
format!(
r#"
const {{ Worker, isMainThread, threadId }} = require("node:worker_threads");
if (isMainThread) {{
let function_env_variables = "{}"
let variablesToKeep = []
variablesToKeep = function_env_variables.split(',').map(variable => variable.trim());
for (const key in process.env) {{
if (!variablesToKeep.includes(key)) {{
delete process.env[key];
}}
}}
// starting worker thread , making separated from the main thread
function runService() {{
Expand Down Expand Up @@ -97,7 +108,7 @@ fn generate_code(code_str: &str) -> String {
}}
"#,
code_str
FUNCTION_ENV_VARIABLES, code_str
)
}

Expand All @@ -106,7 +117,7 @@ pub fn execute_fn(
key: &str,
value: Value,
) -> Result<String, (String, Option<String>)> {
let exec_code = execute_validate_fun(code_str, value, format!(r#""{key}""#));
let exec_code = execute_validate_fun(code_str, format!(r#""{key}""#), value);
let output = Command::new("node")
.arg("-e")
.arg(generate_code(&exec_code))
Expand Down
2 changes: 1 addition & 1 deletion crates/frontend/src/pages/function/function_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn create_function_view() -> impl IntoView {
window.editor = monaco.editor.create(document.querySelector('.monaco'), {
value: [
'async function validate(value, key) {',
'async function validate(key, value) {',
' return true;',
'}'
].join('\n'),
Expand Down

0 comments on commit 710c69d

Please sign in to comment.