Skip to content

feat: harden function code by validating return values#1070

Open
Datron wants to merge 1 commit into
mainfrom
rustyscript-hardening
Open

feat: harden function code by validating return values#1070
Datron wants to merge 1 commit into
mainfrom
rustyscript-hardening

Conversation

@Datron

@Datron Datron commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Problem

Superposition does not validate function return types

Solution

During compilation and checks, simplify and strengthen checks by running the execute function. This does the following:

  • ensure execute exists
  • ensure execute is a function
  • ensure execute returns the correct type based on the function type

Post-deployment activity

Maybe update blog post

Review these two files, the other changes were generated when I ran make check

  • crates/context_aware_config/src/api/functions/handlers.rs
  • crates/context_aware_config/src/validation_functions.rs

Summary by CodeRabbit

  • Bug Fixes
    • Improved function validation so checks now account for the function’s type, reducing incorrect validation results.
    • Updated function editing checks to validate against the current saved function state as well as the draft version.
    • Background compilation now uses the correct function context, which should make validation more reliable during create and update flows.

@Datron Datron requested a review from a team as a code owner July 1, 2026 06:56
@semanticdiff-com

semanticdiff-com Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review changes with  SemanticDiff

Changed Files
File Status
  crates/frontend/src/components/experiment_ramp_form.rs  100% smaller
  crates/context_aware_config/src/validation_functions.rs  30% smaller
  crates/superposition_core/benches/resolve.rs  18% smaller
  crates/context_aware_config/src/api/functions/handlers.rs  0% smaller

@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e433e130-f20f-4c5b-9d81-8aa1c3b33927

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

Function compilation now requires a FunctionType parameter. Handlers (create/update) fetch and pass function type to compile_fn. compile_fn's internal logic was reworked to use generate_wrapped_code and validate JS execute() output shape (array vs. boolean) based on function type, replacing a prior generic typeCheck approach.

Changes

Function type-aware compile validation

Layer / File(s) Summary
Handlers pass function_type to compile_fn
crates/context_aware_config/src/api/functions/handlers.rs
create_handler derives function_type from the request and passes it to compile_fn; update_handler fetches the existing function earlier to obtain its function_type and passes it alongside the draft code.
compile_fn validates execute output by FunctionType
crates/context_aware_config/src/validation_functions.rs
compile_fn signature now takes fn_type: &FunctionType, uses generate_wrapped_code instead of a custom typeCheck JS wrapper, builds a FunctionExecutionRequest with a dummy FunctionEnvironment, and validates that execute() returns an array (ValueCompute) or boolean (other variants), erroring on unhandled types; run_js_function and imports updated accordingly.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Handler as create_handler/update_handler
  participant CompileFn as compile_fn
  participant Runtime as rustyscript runtime

  Client->>Handler: submit function code
  Handler->>Handler: determine function_type
  Handler->>CompileFn: compile_fn(code, function_type)
  CompileFn->>CompileFn: generate_wrapped_code(code)
  CompileFn->>Runtime: execute() with typed payload
  Runtime-->>CompileFn: JSON result
  CompileFn->>CompileFn: validate array/boolean by function_type
  CompileFn-->>Handler: Ok or Err
  Handler-->>Client: response
Loading

Poem

A rabbit hops through code so neat, 🐇
Now types and functions finally meet,
No more typeCheck tricks of old,
Arrays and bools, correctly told,
Compile clean, and validate bright—
Hop hop hop, the tests feel right!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: strengthening function code validation by checking execute return values.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch rustyscript-hardening

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/context_aware_config/src/validation_functions.rs`:
- Around line 187-242: The compile_fn validation currently calls validation
functions through validation_functions::compile_fn using an empty
FunctionEnvironment and blank/default request fields, which can falsely fail
functions that require real key/value/context data. Update the execute smoke
test payloads for FunctionType::ValueCompute, FunctionType::ValueValidation, and
FunctionType::ContextValidation to use contract-appropriate fixture inputs, or
relax this path so runtime exceptions from missing data do not hard-fail
create/update validation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 53826c1e-16b9-468b-a64f-0979a723afc4

📥 Commits

Reviewing files that changed from the base of the PR and between 680f07d and e584879.

📒 Files selected for processing (2)
  • crates/context_aware_config/src/api/functions/handlers.rs
  • crates/context_aware_config/src/validation_functions.rs

Comment on lines +187 to +242
let environment = FunctionEnvironment {
context: serde_json::Map::new(),
overrides: serde_json::Map::new(),
};
match fn_type {
FunctionType::ValueCompute => {
let payload = FunctionExecutionRequest::ValueComputeFunctionRequest {
name: String::new(),
prefix: String::new(),
r#type: superposition_types::api::functions::KeyType::ConfigKey,
environment,
};
let serde_json::Value::Array(_) = runtime
.call_function_async::<serde_json::Value>(
Some(&module_handle),
"execute",
json_args!(payload),
)
.await? else {
return Err(rustyscript::Error::Runtime("The value compute function did not return an array".to_string()));
};
Ok(())
}
other => {
let payload = match other {
FunctionType::ValueValidation => FunctionExecutionRequest::ValueValidationFunctionRequest {
key: String::new(),
value: serde_json::Value::String(String::new()),
r#type:
KeyType::ConfigKey,
environment,
},
FunctionType::ContextValidation => FunctionExecutionRequest::ContextValidationFunctionRequest {
environment,
trigger_reason: ContextValidationTrigger::Context,
},
FunctionType::ChangeReasonValidation => FunctionExecutionRequest::ChangeReasonValidationFunctionRequest {
change_reason: ChangeReason::default(),
},
_ => {
return Err(rustyscript::Error::Runtime("An invalid situation occurred in the runtime".to_string()))
}
};
let serde_json::Value::Bool(_) = runtime
.call_function_async::<serde_json::Value>(
Some(&module_handle),
"execute",
json_args!(payload),
)
.await? else {
return Err(rustyscript::Error::Runtime("The function did not return a boolean".to_string()))
};
Ok(())
}

}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify rustyscript Error::Runtime variant and call_function_async signature
rg -nP 'enum Error|Runtime\s*\(|fn call_function_async' $(fd -HI -t d 'rustyscript*' ~/.cargo 2>/dev/null) 2>/dev/null
cargo doc -p rustyscript --no-deps 2>/dev/null | head -n 5 || echo "inspect rustyscript version in Cargo.lock"
rg -nP 'name = "rustyscript"' -A2 Cargo.lock

Repository: juspay/superposition

Length of output: 158


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the validation code and nearby context
sed -n '1,320p' crates/context_aware_config/src/validation_functions.rs | cat -n

# Find related usages/tests/docs in the repo
rg -n "validation_functions|FunctionType::ValueCompute|ContextValidation|ChangeReasonValidation|validate.*function|execute" crates/context_aware_config -S

# Locate rustyscript version and any cached source/docs in the local cargo registry
rg -n 'name = "rustyscript"' Cargo.lock
fd -HI -t d 'rustyscript-*' ~/.cargo/registry/src ~/.cargo/registry/cache 2>/dev/null | head -n 20

Repository: juspay/superposition

Length of output: 20912


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect call sites and validation semantics
sed -n '1,260p' crates/context_aware_config/src/api/functions/handlers.rs | cat -n
sed -n '1,260p' crates/context_aware_config/src/api/dimension/validations.rs | cat -n
sed -n '1,260p' crates/context_aware_config/src/api/default_config/handlers.rs | cat -n

# Search for tests or docs that describe what compile_fn accepts
rg -n "compile_fn|validation_error!|ValueCompute|ContextValidation|ChangeReasonValidation|dummy|empty/default|return type" crates/context_aware_config -S

# Locate rustyscript source in the local cargo registry and inspect the API
fd -HI -t d 'rustyscript-*' ~/.cargo/registry/src ~/.cargo/registry/cache 2>/dev/null | head -n 20

Repository: juspay/superposition

Length of output: 39607


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Get rustyscript version from Cargo.lock and locate the corresponding source
version=$(awk '
  $0 == '\"'\"'name = \"rustyscript\"'\"'\"' {found=1}
  found && $0 ~ /^version = / {gsub(/"/,""); print $3; exit}
' Cargo.lock)

echo "rustyscript version: ${version:-unknown}"

root=$(fd -HI -t d "rustyscript-${version}" ~/.cargo/registry/src ~/.cargo/registry/cache 2>/dev/null | head -n 1)
echo "rustyscript root: $root"

if [ -n "${root:-}" ]; then
  rg -n "enum Error|Runtime\\(|call_function_async" "$root" -S
fi

Repository: juspay/superposition

Length of output: 242


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect runtime validation semantics for context validation functions
sed -n '280,360p' crates/context_aware_config/src/api/context/helpers.rs | cat -n

# Safely extract the rustyscript version from Cargo.lock
python3 - <<'PY'
import re, pathlib
text = pathlib.Path("Cargo.lock").read_text()
m = re.search(r'name = "rustyscript"\nversion = "([^"]+)"', text)
print(m.group(1) if m else "not found")
PY

Repository: juspay/superposition

Length of output: 3623


Avoid validating with empty/default inputs
compile_fn executes execute with an empty FunctionEnvironment and blank fields, so any function that legitimately needs a key, value, or context entry can be rejected at create/update time even if it works during normal execution. Use contract-appropriate fixture data or skip hard-failing on runtime exceptions here.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/context_aware_config/src/validation_functions.rs` around lines 187 -
242, The compile_fn validation currently calls validation functions through
validation_functions::compile_fn using an empty FunctionEnvironment and
blank/default request fields, which can falsely fail functions that require real
key/value/context data. Update the execute smoke test payloads for
FunctionType::ValueCompute, FunctionType::ValueValidation, and
FunctionType::ContextValidation to use contract-appropriate fixture inputs, or
relax this path so runtime exceptions from missing data do not hard-fail
create/update validation.

@Datron Datron force-pushed the rustyscript-hardening branch from e584879 to 46ce270 Compare July 1, 2026 07:06
Comment on lines 105 to 111

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not required.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file changes also looks only formatting change, Can we revert ?

@Datron Datron force-pushed the rustyscript-hardening branch from 46ce270 to bc9e7c1 Compare July 6, 2026 11:55
@Datron Datron requested a review from sauraww July 10, 2026 06:48
@Datron Datron force-pushed the rustyscript-hardening branch from bc9e7c1 to 2c9d6d4 Compare July 10, 2026 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants