Skip to content

Commit b35c2cb

Browse files
committed
Bug 1935434 Implement forgiving parsing for trusted-types CSP directive. r=smaug
Currently, we just discard the whole directive if an invalid token is found. With this patch, we instead ignore such a token. Also improves tests in should-trusted-type-policy-creation-be-blocked-by-csp-002.html so that we really check that the original trusted-types directive is preserved after serialization. See w3c/webappsec-csp#363 (comment) Differential Revision: https://phabricator.services.mozilla.com/D243358
1 parent 6ce9e17 commit b35c2cb

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

dom/security/nsCSPParser.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -975,14 +975,10 @@ void nsCSPParser::handleTrustedTypesDirective(nsCSPDirective* aDir) {
975975
new nsCSPTrustedTypesDirectivePolicyName(mCurToken));
976976
} else {
977977
AutoTArray<nsString, 1> token = {mCurToken};
978-
logWarningErrorToConsole(nsIScriptError::errorFlag,
978+
logWarningErrorToConsole(nsIScriptError::warningFlag,
979979
"invalidTrustedTypesExpression", token);
980-
981-
for (auto* trustedTypeExpression : trustedTypesExpressions) {
982-
delete trustedTypeExpression;
983-
}
984-
985-
return;
980+
trustedTypesExpressions.AppendElement(
981+
new nsCSPTrustedTypesDirectiveInvalidToken(mCurToken));
986982
}
987983
}
988984

dom/security/nsCSPUtils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,24 @@ void nsCSPTrustedTypesDirectivePolicyName::toString(nsAString& aOutStr) const {
10851085
aOutStr.Append(mName);
10861086
}
10871087

1088+
/* =============== nsCSPTrustedTypesDirectiveInvalidToken =============== */
1089+
1090+
nsCSPTrustedTypesDirectiveInvalidToken::nsCSPTrustedTypesDirectiveInvalidToken(
1091+
const nsAString& aInvalidToken)
1092+
: mInvalidToken{aInvalidToken} {}
1093+
1094+
bool nsCSPTrustedTypesDirectiveInvalidToken::visit(
1095+
nsCSPSrcVisitor* aVisitor) const {
1096+
MOZ_ASSERT_UNREACHABLE(
1097+
"Should only be called for other overloads of this method.");
1098+
return false;
1099+
}
1100+
1101+
void nsCSPTrustedTypesDirectiveInvalidToken::toString(
1102+
nsAString& aOutStr) const {
1103+
aOutStr.Append(mInvalidToken);
1104+
}
1105+
10881106
/* ===== nsCSPDirective ====================== */
10891107

10901108
nsCSPDirective::nsCSPDirective(CSPDirective aDirective) {

dom/security/nsCSPUtils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,19 @@ class nsCSPTrustedTypesDirectivePolicyName : public nsCSPBaseSrc {
437437
const nsString mName;
438438
};
439439

440+
class nsCSPTrustedTypesDirectiveInvalidToken : public nsCSPBaseSrc {
441+
public:
442+
explicit nsCSPTrustedTypesDirectiveInvalidToken(
443+
const nsAString& aInvalidToken);
444+
virtual ~nsCSPTrustedTypesDirectiveInvalidToken() = default;
445+
446+
bool visit(nsCSPSrcVisitor* aVisitor) const override;
447+
void toString(nsAString& aOutStr) const override;
448+
449+
private:
450+
const nsString mInvalidToken;
451+
};
452+
440453
/* =============== nsCSPSrcVisitor ================== */
441454

442455
class nsCSPSrcVisitor {
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
[should-trusted-type-policy-creation-be-blocked-by-csp-002.html]
2-
[invalid tt-policy-name name "policy*name"]
3-
expected: FAIL
4-
5-
[invalid tt-policy-name name "policy$name"]
6-
expected: FAIL
7-
8-
[invalid tt-policy-name name "policy?name"]
9-
expected: FAIL
10-
11-
[invalid tt-policy-name name "policy!name"]
12-
expected: FAIL
13-
142
[invalid tt-policy-name name "política"]
153
expected: FAIL
164

175
[directive "trusted-type _TTP1_%09_TTP2_%0A_TTP3_%0C_TTP4_%0D_TTP5_%20_TTP6_" (required-ascii-whitespace)]
186
expected: FAIL
19-
20-
[invalid directive "trusted-type _TTP" (no ascii whitespace)]
21-
expected: FAIL

testing/web-platform/tests/trusted-types/should-trusted-type-policy-creation-be-blocked-by-csp-002.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
// https://w3c.github.io/trusted-types/dist/spec/#should-block-create-policy
6565
assert_true(results[0].exception instanceof TypeError, "createPolicy() should throw a TypeError.");
6666
assert_equals(results[0].violatedPolicies.length, 1, "createPolicy() should trigger a violation report.");
67+
assert_equals(results[0].violatedPolicies[0].disposition, "enforce");
68+
assert_equals(results[0].violatedPolicies[0].policy, `trusted-types ${trustedTypePolicyName}`);
6769
}, `invalid tt-policy-name name "${trustedTypePolicyName}"`);
6870
});
6971

@@ -91,5 +93,7 @@
9193
assert_equals(results.length, 1);
9294
assert_true(results[0].exception instanceof TypeError);
9395
assert_equals(results[0].violatedPolicies.length, 1);
96+
assert_equals(results[0].violatedPolicies[0].disposition, "enforce");
97+
assert_equals(results[0].violatedPolicies[0].policy, `trusted-types _TTP_*`);
9498
}, `invalid directive "trusted-type _TTP" (no ascii whitespace)`);
9599
</script>

0 commit comments

Comments
 (0)