Skip to content

Commit 9bfa988

Browse files
committed
fix(policy): validate raw OPA settings and redact startup errors
Validate raw network fields through the authored schema before normalization. Preserve custom Rego data and supported runtime forms. Apply the shared filesystem path checks and non-root identity predicate to raw static settings. Discard authored Rego source and nested errors from static configuration evaluation. Cover malformed inputs, valid controls, file loading, and rejected reloads retaining active decisions and generation. Refs NVIDIA#3092. Signed-off-by: Shiju <[email protected]>
1 parent f395a8c commit 9bfa988

5 files changed

Lines changed: 685 additions & 53 deletions

File tree

‎architecture/security-policy.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ high-severity detection finding at startup naming the inactive controls.
213213

214214
## Policy Load Diagnostics
215215

216-
`OpaEngine` bounds the error messages returned when loading or reloading policy from files, strings, or protobuf. Each message contains at most eight error items and 512 UTF-8 bytes, including its heading, separators, and any `additional violations omitted` marker. The loader reports complete, fixed categories and discards authored names, values, paths, source snippets, and nested error chains.
216+
`OpaEngine` bounds the error messages returned when loading or reloading policy from files, strings, or protobuf and when evaluating static sandbox settings at startup. Each message contains at most eight error items and 512 UTF-8 bytes, including its heading, separators, and any `additional violations omitted` marker. The loader reports complete, fixed categories and discards authored names, values, paths, source snippets, and nested error chains. Static evaluation failures report that sandbox settings could not be evaluated without including Rego source.
217217

218218
Typed validation categories distinguish process identity, filesystem paths and limits, Landlock compatibility, endpoint hosts and ports, credential signing and rewriting, MCP configuration, and middleware configuration. Opaque L7 errors identify the protocol-configuration or policy-validation stage; endpoint conflicts report ambiguous selectors. YAML errors retain a fixed parser category and numeric line and column when available. File I/O, Rego loading, and internal policy-data errors use fixed messages.
219219

220-
A candidate rejected during validation does not replace the active engine or advance its generation. The supervisor separately applies `policy_validation_failure_mode` and may publish a quarantine generation as described below. The diagnostic bounds cover returned OPA load errors; accepted-policy warnings, runtime request diagnostics, and gateway-authored policy parser messages have separate reporting contracts.
220+
A candidate rejected during validation does not replace the active engine or advance its generation. The supervisor separately applies `policy_validation_failure_mode` and may publish a quarantine generation as described below. Accepted-policy warnings, runtime request diagnostics, and gateway-authored policy parser messages have separate reporting contracts.
221221

222222
## Live Updates
223223

@@ -230,7 +230,9 @@ same effective configuration path.
230230

231231
The OPA loader checks the object and list shapes of raw policy data before injecting runtime fields, normalizing values, or expanding access presets. It rejects the first malformed container with a fixed structural error that excludes authored keys and values. This check preserves valid versionless OPA data and runtime-only fields. A rejected OPA engine reload leaves that engine's installed policy, generation, and decisions unchanged; the supervisor separately applies its configured runtime rejection mode.
232232

233-
Raw file and string loading validate the nested filesystem, Landlock, and process settings with the authored schema types. Invalid field types, unknown static fields, and unknown Landlock modes reject the candidate. An omitted filesystem stanza keeps `include_workdir: true`; a present stanza defaults it to `false`. Accepted Landlock enum representations are serialized to the canonical string consumed by the runtime. Explicit `null` JSON-RPC or MCP options also reject the candidate before protocol defaults are applied. Validation retains the raw document's runtime fields and Rego-specific network forms.
233+
Raw file and string loading validate the nested filesystem, Landlock, and process settings with the authored schema types. Invalid field types, unknown static fields, and unknown Landlock modes reject the candidate. The same filesystem path validator and non-root identity predicate apply to typed and raw policies. An omitted filesystem stanza keeps `include_workdir: true`; a present stanza defaults it to `false`. Accepted Landlock enum representations are serialized to the canonical string consumed by the runtime.
234+
235+
Network field validation uses the authored endpoint, binary, and rule types before normalization can erase malformed values. For example, a quoted boolean or an array-valued deny-rule method rejects the candidate. Explicit `null` JSON-RPC or MCP options also reject it before protocol defaults are applied. Validation operates on copies of known fields, preserving custom Rego data and supported runtime forms, including flattened protocol options and lowered matchers.
234236

235237
After validating L7 rules, the OPA loader converts nonempty string query and MCP parameter matchers into explicit `glob` objects, including MCP `tool` aliases and deny rules. Matchers representable in both YAML and protobuf therefore expose the same representation to endpoint configuration consumers. Already lowered `glob` and `any` matchers retain their values across reloads; normalization preserves runtime endpoint provenance. Empty scalar query matchers remain an OPA-only form because Rego gives them different behavior from empty `glob` objects.
236238

‎crates/openshell-policy/src/lib.rs‎

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,61 @@ pub fn validate_sandbox_policy(
13621362
validate_sandbox_policy_with_mcp_presence(policy, McpVersionPresence::RequireMaterialized)
13631363
}
13641364

1365+
/// Validate filesystem paths shared by typed policies and raw OPA data.
1366+
///
1367+
/// Paths must be absolute, contain no parent traversal, and fit the path count
1368+
/// and byte limits. Read-write access to the filesystem root is forbidden.
1369+
/// Callers that expose errors outside trusted authoring tools must redact the
1370+
/// path values carried by the returned violations.
1371+
pub fn validate_filesystem_paths(
1372+
read_only: &[String],
1373+
read_write: &[String],
1374+
) -> std::result::Result<(), Vec<PolicyViolation>> {
1375+
let mut violations = Vec::new();
1376+
let total_paths = read_only.len() + read_write.len();
1377+
if total_paths > MAX_FILESYSTEM_PATHS {
1378+
violations.push(PolicyViolation::TooManyPaths { count: total_paths });
1379+
}
1380+
1381+
for path_str in read_only.iter().chain(read_write.iter()) {
1382+
if path_str.len() > MAX_PATH_LENGTH {
1383+
violations.push(PolicyViolation::FieldTooLong {
1384+
path: truncate_for_display(path_str),
1385+
length: path_str.len(),
1386+
});
1387+
continue;
1388+
}
1389+
let path = Path::new(path_str);
1390+
if !path.has_root() {
1391+
violations.push(PolicyViolation::RelativePath {
1392+
path: path_str.clone(),
1393+
});
1394+
}
1395+
if path
1396+
.components()
1397+
.any(|c| matches!(c, std::path::Component::ParentDir))
1398+
{
1399+
violations.push(PolicyViolation::PathTraversal {
1400+
path: path_str.clone(),
1401+
});
1402+
}
1403+
}
1404+
1405+
for path_str in read_write {
1406+
// Repeated separators still designate the filesystem root.
1407+
if path_str.trim_end_matches('/').is_empty() {
1408+
violations.push(PolicyViolation::OverlyBroadPath {
1409+
path: path_str.clone(),
1410+
});
1411+
}
1412+
}
1413+
if violations.is_empty() {
1414+
Ok(())
1415+
} else {
1416+
Err(violations)
1417+
}
1418+
}
1419+
13651420
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13661421
enum McpVersionPresence {
13671422
RequireMaterialized,
@@ -1403,50 +1458,10 @@ fn validate_sandbox_policy_with_mcp_presence(
14031458
});
14041459
}
14051460

1406-
// Check filesystem paths
1407-
if let Some(ref fs) = policy.filesystem {
1408-
let total_paths = fs.read_only.len() + fs.read_write.len();
1409-
if total_paths > MAX_FILESYSTEM_PATHS {
1410-
violations.push(PolicyViolation::TooManyPaths { count: total_paths });
1411-
}
1412-
1413-
for path_str in fs.read_only.iter().chain(fs.read_write.iter()) {
1414-
if path_str.len() > MAX_PATH_LENGTH {
1415-
violations.push(PolicyViolation::FieldTooLong {
1416-
path: truncate_for_display(path_str),
1417-
length: path_str.len(),
1418-
});
1419-
continue;
1420-
}
1421-
1422-
let path = Path::new(path_str);
1423-
1424-
if !path.has_root() {
1425-
violations.push(PolicyViolation::RelativePath {
1426-
path: path_str.clone(),
1427-
});
1428-
}
1429-
1430-
if path
1431-
.components()
1432-
.any(|c| matches!(c, std::path::Component::ParentDir))
1433-
{
1434-
violations.push(PolicyViolation::PathTraversal {
1435-
path: path_str.clone(),
1436-
});
1437-
}
1438-
}
1439-
1440-
// Only reject "/" as read-write (overly broad)
1441-
for path_str in &fs.read_write {
1442-
let normalized = path_str.trim_end_matches('/');
1443-
if normalized.is_empty() {
1444-
// Path is "/" or "///" etc.
1445-
violations.push(PolicyViolation::OverlyBroadPath {
1446-
path: path_str.clone(),
1447-
});
1448-
}
1449-
}
1461+
if let Some(ref fs) = policy.filesystem
1462+
&& let Err(errors) = validate_filesystem_paths(&fs.read_only, &fs.read_write)
1463+
{
1464+
violations.extend(errors);
14501465
}
14511466

14521467
// Protobuf maps do not preserve iteration order. Sort rule keys so callers

0 commit comments

Comments
 (0)