18
18
#include " mozilla/dom/TrustedScriptURL.h"
19
19
#include " mozilla/dom/TrustedTypePolicy.h"
20
20
#include " mozilla/dom/TrustedTypePolicyFactory.h"
21
+ #include " mozilla/dom/TrustedTypesConstants.h"
21
22
#include " nsGlobalWindowInner.h"
22
23
#include " nsLiteralString.h"
23
24
#include " nsTArray.h"
@@ -250,6 +251,10 @@ MOZ_CAN_RUN_SCRIPT inline const nsAString* GetTrustedTypesCompliantString(
250
251
TrustedScriptOrNullIsEmptyString>) {
251
252
return aInput.IsNullIsEmptyString ();
252
253
}
254
+ if constexpr (std::is_same_v<TrustedTypeOrStringArg, const nsAString*>) {
255
+ Unused << aInput;
256
+ return true ;
257
+ }
253
258
MOZ_ASSERT_UNREACHABLE ();
254
259
return false ;
255
260
};
@@ -267,6 +272,9 @@ MOZ_CAN_RUN_SCRIPT inline const nsAString* GetTrustedTypesCompliantString(
267
272
TrustedScriptOrNullIsEmptyString>) {
268
273
return &aInput.GetAsNullIsEmptyString ();
269
274
}
275
+ if constexpr (std::is_same_v<TrustedTypeOrStringArg, const nsAString*>) {
276
+ return aInput;
277
+ }
270
278
MOZ_ASSERT_UNREACHABLE ();
271
279
return static_cast <const nsAString*>(&EmptyString ());
272
280
};
@@ -286,6 +294,10 @@ MOZ_CAN_RUN_SCRIPT inline const nsAString* GetTrustedTypesCompliantString(
286
294
TrustedScriptURLOrString>) {
287
295
return aInput.IsTrustedScriptURL ();
288
296
}
297
+ if constexpr (std::is_same_v<TrustedTypeOrStringArg, const nsAString*>) {
298
+ Unused << aInput;
299
+ return false ;
300
+ }
289
301
MOZ_ASSERT_UNREACHABLE ();
290
302
return false ;
291
303
};
@@ -305,6 +317,7 @@ MOZ_CAN_RUN_SCRIPT inline const nsAString* GetTrustedTypesCompliantString(
305
317
TrustedScriptURLOrString>) {
306
318
return &aInput.GetAsTrustedScriptURL ().mData ;
307
319
}
320
+ Unused << aInput;
308
321
MOZ_ASSERT_UNREACHABLE ();
309
322
return &EmptyString ();
310
323
};
@@ -410,13 +423,15 @@ bool GetTrustedTypeDataForAttribute(const nsAtom* aElementName,
410
423
if (aAttributeNamespaceID == kNameSpaceID_None &&
411
424
aAttributeName == nsGkAtoms::srcdoc) {
412
425
aTrustedType = TrustedType::TrustedHTML;
426
+ aSink.AssignLiteral (u" HTMLIFrameElement srcdoc" );
413
427
return true ;
414
428
}
415
429
} else if (aElementName == nsGkAtoms::script) {
416
430
// HTMLScriptElement
417
431
if (aAttributeNamespaceID == kNameSpaceID_None &&
418
432
aAttributeName == nsGkAtoms::src) {
419
433
aTrustedType = TrustedType::TrustedScriptURL;
434
+ aSink.AssignLiteral (u" HTMLScriptElement src" );
420
435
return true ;
421
436
}
422
437
}
@@ -427,6 +442,7 @@ bool GetTrustedTypeDataForAttribute(const nsAtom* aElementName,
427
442
aAttributeNamespaceID == kNameSpaceID_XLink ) &&
428
443
aAttributeName == nsGkAtoms::href) {
429
444
aTrustedType = TrustedType::TrustedScriptURL;
445
+ aSink.AssignLiteral (u" SVGScriptElement href" );
430
446
return true ;
431
447
}
432
448
}
@@ -435,4 +451,75 @@ bool GetTrustedTypeDataForAttribute(const nsAtom* aElementName,
435
451
return false ;
436
452
}
437
453
454
+ MOZ_CAN_RUN_SCRIPT const nsAString* GetTrustedTypesCompliantAttributeValue (
455
+ const nsINode& aElement, nsAtom* aAttributeName,
456
+ int32_t aAttributeNamespaceID,
457
+ const TrustedHTMLOrTrustedScriptOrTrustedScriptURLOrString& aNewValue,
458
+ Maybe<nsAutoString>& aResultHolder, ErrorResult& aError) {
459
+ auto getAsTrustedType = [&aNewValue] {
460
+ if (aNewValue.IsTrustedHTML ()) {
461
+ return &aNewValue.GetAsTrustedHTML ().mData ;
462
+ }
463
+ if (aNewValue.IsTrustedScript ()) {
464
+ return &aNewValue.GetAsTrustedScript ().mData ;
465
+ }
466
+ MOZ_ASSERT (aNewValue.IsTrustedScriptURL ());
467
+ return &aNewValue.GetAsTrustedScriptURL ().mData ;
468
+ };
469
+ auto getContent = [&aNewValue, &getAsTrustedType] {
470
+ return aNewValue.IsString () ? &aNewValue.GetAsString () : getAsTrustedType ();
471
+ };
472
+
473
+ if (!StaticPrefs::dom_security_trusted_types_enabled ()) {
474
+ // A trusted type might've been created before the pref was set to `false`,
475
+ // so we cannot assume aNewValue.IsString().
476
+ return getContent ();
477
+ }
478
+
479
+ // In the common situation of non-data document without any
480
+ // require-trusted-types-for directive, we just return immediately.
481
+ const NodeInfo* nodeInfo = aElement.NodeInfo ();
482
+ Document* ownerDoc = nodeInfo->GetDocument ();
483
+ const bool ownerDocLoadedAsData = ownerDoc->IsLoadedAsData ();
484
+ if (!ownerDoc->HasPolicyWithRequireTrustedTypesForDirective () &&
485
+ !ownerDocLoadedAsData) {
486
+ return getContent ();
487
+ }
488
+
489
+ TrustedType expectedType;
490
+ nsAutoString sink;
491
+ if (!GetTrustedTypeDataForAttribute (
492
+ nodeInfo->NameAtom (), nodeInfo->NamespaceID (), aAttributeName,
493
+ aAttributeNamespaceID, expectedType, sink)) {
494
+ return getContent ();
495
+ }
496
+
497
+ if ((expectedType == TrustedType::TrustedHTML && aNewValue.IsTrustedHTML ()) ||
498
+ (expectedType == TrustedType::TrustedScript &&
499
+ aNewValue.IsTrustedScript ()) ||
500
+ (expectedType == TrustedType::TrustedScriptURL &&
501
+ aNewValue.IsTrustedScriptURL ())) {
502
+ return getAsTrustedType ();
503
+ }
504
+
505
+ const nsAString* input =
506
+ aNewValue.IsString () ? &aNewValue.GetAsString () : getAsTrustedType ();
507
+ switch (expectedType) {
508
+ case TrustedType::TrustedHTML:
509
+ return GetTrustedTypesCompliantString<TrustedHTML>(
510
+ input, sink, kTrustedTypesOnlySinkGroup , aElement, aResultHolder,
511
+ aError);
512
+ case TrustedType::TrustedScript:
513
+ return GetTrustedTypesCompliantString<TrustedScript>(
514
+ input, sink, kTrustedTypesOnlySinkGroup , aElement, aResultHolder,
515
+ aError);
516
+ case TrustedType::TrustedScriptURL:
517
+ return GetTrustedTypesCompliantString<TrustedScriptURL>(
518
+ input, sink, kTrustedTypesOnlySinkGroup , aElement, aResultHolder,
519
+ aError);
520
+ }
521
+ MOZ_ASSERT_UNREACHABLE ();
522
+ return nullptr ;
523
+ }
524
+
438
525
} // namespace mozilla::dom::TrustedTypeUtils
0 commit comments