mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 16:03:10 +00:00
fix(trusted-types): reject transformed source without extra reports
This commit is contained in:
@@ -2464,6 +2464,46 @@ fn empty_default_policy_report_only_allows_and_reports_each_element_sink() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn report_only_function_constructors_preserve_brands_and_apply_default_conversion() {
|
||||
let mut vm = new_storage_test_vm("https://trusted-types-function-report-only.test/");
|
||||
vm.set_response_content_security_policies(&["script-src 'unsafe-eval'".to_owned()]);
|
||||
vm.set_response_content_security_report_only_policies(&[
|
||||
"require-trusted-types-for 'script'".to_owned()
|
||||
]);
|
||||
let source = format!(
|
||||
"globalThis.__functionReportOnly = {}; JSON.stringify(__functionReportOnly);",
|
||||
include_str!("../../../../tests/fixtures/trusted-types-function-report-only.js")
|
||||
);
|
||||
let result: serde_json::Value = serde_json::from_str(
|
||||
&vm.eval(&source)
|
||||
.expect("report-only Function conversions should evaluate"),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result["defaults"], 8);
|
||||
assert_eq!(result["conversions"], 8);
|
||||
assert_eq!(result["rejected"], 4);
|
||||
assert_eq!(result["violations"], serde_json::json!([]));
|
||||
assert_eq!(
|
||||
drain_pre_domcontentloaded_non_script_page_tasks_for_test(&mut vm),
|
||||
1
|
||||
);
|
||||
let reports: serde_json::Value = serde_json::from_str(
|
||||
&vm.eval("JSON.stringify(__functionReportOnly.violations)")
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(reports.as_array().unwrap().len(), 1, "{reports}");
|
||||
assert_eq!(reports[0]["disposition"], "report");
|
||||
assert!(
|
||||
reports[0]["sample"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.starts_with("Function|"),
|
||||
"{reports}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn report_only_trusted_types_eval_runs_reports_and_applies_the_default_policy() {
|
||||
let mut vm = new_storage_test_vm("https://trusted-types-eval-report-only.test/");
|
||||
|
||||
@@ -1282,6 +1282,37 @@ async fn worker_trusted_types_eval_keyword_allows_eval_when_trusted_types_are_en
|
||||
assert_eq!(expect_post_json(msg), r#"{"value":42,"violations":0}"#);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn worker_report_only_function_constructors_preserve_brands_and_default_conversion() {
|
||||
ensure_v8();
|
||||
let source = format!(
|
||||
"const result = {}; setTimeout(() => {{ postMessage(result); close(); }}, 0);",
|
||||
include_str!("../../../../tests/fixtures/trusted-types-function-report-only.js")
|
||||
);
|
||||
let mut handle = spawn_test_worker_with_options(
|
||||
WorkerSpawnOptions::new(source, "https://app.test/worker/main.js".to_owned())
|
||||
.with_content_security_policies(vec!["script-src 'unsafe-eval'".to_owned()])
|
||||
.with_content_security_report_only_policies(vec![
|
||||
"require-trusted-types-for 'script'".to_owned(),
|
||||
]),
|
||||
);
|
||||
let message = timeout(TIMEOUT, handle.recv()).await.unwrap().unwrap();
|
||||
let result: serde_json::Value = serde_json::from_str(&expect_post_json(message)).unwrap();
|
||||
assert_eq!(result["defaults"], 8);
|
||||
assert_eq!(result["conversions"], 8);
|
||||
assert_eq!(result["rejected"], 4);
|
||||
let reports = result["violations"].as_array().unwrap();
|
||||
assert_eq!(reports.len(), 1, "{result}");
|
||||
assert_eq!(reports[0]["disposition"], "report");
|
||||
assert!(
|
||||
reports[0]["sample"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.starts_with("Function|"),
|
||||
"{result}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn worker_trusted_script_code_like_brand_drives_function_constructor() {
|
||||
ensure_v8();
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
(() => {
|
||||
const assert = (condition, message) => {
|
||||
if (!condition) throw Error(message);
|
||||
};
|
||||
const target = typeof document === 'undefined' ? globalThis : document;
|
||||
const violations = [];
|
||||
target.addEventListener('securitypolicyviolation', event => {
|
||||
if (event.blockedURI === 'trusted-types-sink') {
|
||||
violations.push({disposition: event.disposition, sample: event.sample});
|
||||
}
|
||||
});
|
||||
const policy = trustedTypes.createPolicy('function-report-only', {
|
||||
createScript: value => value
|
||||
});
|
||||
const parameter = policy.createScript('value');
|
||||
const body = policy.createScript('return value + 1;');
|
||||
const constructors = [
|
||||
values => Function(...values),
|
||||
values => new Function(...values),
|
||||
values => Reflect.apply(Function, undefined, values),
|
||||
values => Reflect.construct(Function, values)
|
||||
];
|
||||
for (const construct of constructors) {
|
||||
assert(construct([parameter, body])(41) === 42, 'trusted source remains usable');
|
||||
}
|
||||
assert(Function('value', 'return value + 10;')(1) === 11,
|
||||
'report-only policy permits and reports plain source');
|
||||
|
||||
const defaults = [];
|
||||
let transform = false;
|
||||
trustedTypes.createPolicy('default', {
|
||||
createScript(value, type, sink) {
|
||||
defaults.push({type, sink});
|
||||
return transform ? value.replace('value + 10', 'value + 100') : value;
|
||||
}
|
||||
});
|
||||
let conversions = 0;
|
||||
for (const construct of constructors) {
|
||||
const before = defaults.length;
|
||||
assert(construct([parameter, body])(41) === 42, 'native brand retains trusted source');
|
||||
assert(defaults.length === before, 'trusted source skips the default policy');
|
||||
|
||||
const changed = policy.createScript('return value + 1;');
|
||||
changed.toString = () => {
|
||||
conversions++;
|
||||
return 'return value + 10;';
|
||||
};
|
||||
assert(construct([parameter, changed])(1) === 11,
|
||||
'changed conversion goes through the transparent report-only default policy');
|
||||
assert(defaults.length === before + 1, 'default conversion occurs once');
|
||||
}
|
||||
transform = true;
|
||||
let rejected = 0;
|
||||
for (const construct of constructors) {
|
||||
const before = defaults.length;
|
||||
assert(construct([parameter, body])(41) === 42, 'native brand skips a transforming policy');
|
||||
assert(defaults.length === before, 'transforming policy is not called for trusted source');
|
||||
const changed = policy.createScript('return value + 1;');
|
||||
changed.toString = () => {
|
||||
conversions++;
|
||||
return 'return value + 10;';
|
||||
};
|
||||
try {
|
||||
construct([parameter, changed]);
|
||||
} catch (error) {
|
||||
assert(error instanceof EvalError, 'Function rejects transformed source with EvalError');
|
||||
rejected++;
|
||||
}
|
||||
assert(defaults.length === before + 1, 'transforming default policy is called once');
|
||||
}
|
||||
assert(rejected === constructors.length, 'all Function entry points reject transformed source');
|
||||
assert(conversions === constructors.length * 2, 'each author conversion runs once');
|
||||
assert(defaults.every(value => value.type === 'TrustedScript' && value.sink === 'Function'),
|
||||
'default policy receives the Function sink metadata');
|
||||
return {violations, defaults: defaults.length, conversions, rejected};
|
||||
})()
|
||||
Reference in New Issue
Block a user