Skip to content

Commit d211df8

Browse files
Lubrsitrflynn89
authored andcommitted
LibWeb: Implement Trusted Types for HTMLIFrameElement#srcdoc
1 parent 0f3a9a6 commit d211df8

8 files changed

+49
-19
lines changed

Libraries/LibWeb/HTML/HTMLIFrameElement.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <LibWeb/HTML/Parser/HTMLParser.h>
2020
#include <LibWeb/HTML/TraversableNavigable.h>
2121
#include <LibWeb/Layout/NavigableContainerViewport.h>
22+
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
23+
#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
2224

2325
namespace Web::HTML {
2426

@@ -305,4 +307,31 @@ void HTMLIFrameElement::set_current_navigation_was_lazy_loaded(bool value)
305307
set_potentially_delays_the_load_event(!value);
306308
}
307309

310+
// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#dom-iframe-srcdoc
311+
TrustedTypes::TrustedHTMLOrString HTMLIFrameElement::srcdoc()
312+
{
313+
// 1. Let attribute be the result of running get an attribute by namespace and local name given null, srcdoc's
314+
// local name, and this.
315+
// 2. If attribute is null, then return the empty string.
316+
// 3. Return attribute's value.
317+
return Utf16String::from_utf8(get_attribute_value(AttributeNames::srcdoc));
318+
}
319+
320+
// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#dom-iframe-srcdoc
321+
WebIDL::ExceptionOr<void> HTMLIFrameElement::set_srcdoc(TrustedTypes::TrustedHTMLOrString const& value)
322+
{
323+
// 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with
324+
// TrustedHTML, this's relevant global object, the given value, "HTMLIFrameElement srcdoc", and "script".
325+
auto const compliant_string = TRY(TrustedTypes::get_trusted_type_compliant_string(
326+
TrustedTypes::TrustedTypeName::TrustedHTML,
327+
HTML::relevant_global_object(*this),
328+
value,
329+
TrustedTypes::InjectionSink::HTMLIFrameElementsrcdoc,
330+
TrustedTypes::Script.to_string()));
331+
332+
// 2. Set an attribute value given this, srcdoc's local name, and compliantString.
333+
set_attribute_value(AttributeNames::srcdoc, compliant_string.to_utf8_but_should_be_ported_to_utf16());
334+
return {};
335+
}
336+
308337
}

Libraries/LibWeb/HTML/HTMLIFrameElement.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class HTMLIFrameElement final
3838

3939
SandboxingFlagSet iframe_sandboxing_flag_set() const { return m_iframe_sandboxing_flag_set; }
4040

41+
TrustedTypes::TrustedHTMLOrString srcdoc();
42+
WebIDL::ExceptionOr<void> set_srcdoc(TrustedTypes::TrustedHTMLOrString const& value);
43+
4144
virtual void visit_edges(Cell::Visitor&) override;
4245

4346
private:

Libraries/LibWeb/HTML/HTMLIFrameElement.idl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <HTML/HTMLElement.idl>
22
#import <HTML/Scripting/Fetching.idl>
33
#import <DOM/Document.idl>
4+
#import <TrustedTypes/TrustedHTML.idl>
45

56
// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#htmliframeelement
67
[Exposed=Window]
@@ -9,7 +10,7 @@ interface HTMLIFrameElement : HTMLElement {
910
[HTMLConstructor] constructor();
1011

1112
[CEReactions, Reflect, URL] attribute USVString src;
12-
[CEReactions, Reflect] attribute DOMString srcdoc;
13+
[CEReactions] attribute (TrustedHTML or Utf16DOMString) srcdoc;
1314
[CEReactions, Reflect] attribute DOMString name;
1415
[SameObject, PutForwards=value] readonly attribute DOMTokenList sandbox;
1516
[CEReactions, Reflect] attribute DOMString allow;

Tests/LibWeb/Text/expected/wpt-import/trusted-types/HTMLElement-generic.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ Harness status: OK
22

33
Found 72 tests
44

5-
68 Pass
6-
4 Fail
5+
72 Pass
76
Pass TT disabled: script.src
87
= TrustedScript on a
98
connected element
@@ -132,11 +131,11 @@ Pass TT enabled: div.innerHTML
132131
= String on a
133132
non-connected element
134133

135-
Fail TT enabled: iframe.srcdoc
134+
Pass TT enabled: iframe.srcdoc
136135
= TrustedHTML on a
137136
connected element
138137

139-
Fail TT enabled: iframe.srcdoc
138+
Pass TT enabled: iframe.srcdoc
140139
= TrustedHTML on a
141140
non-connected element
142141

@@ -228,11 +227,11 @@ Pass TT enabled: div.innerHTML
228227
= String on a
229228
non-connected element
230229
after removing the "require-trusted-types-for 'script' directive
231-
Fail TT enabled: iframe.srcdoc
230+
Pass TT enabled: iframe.srcdoc
232231
= TrustedHTML on a
233232
connected element
234233
after removing the "require-trusted-types-for 'script' directive
235-
Fail TT enabled: iframe.srcdoc
234+
Pass TT enabled: iframe.srcdoc
236235
= TrustedHTML on a
237236
non-connected element
238237
after removing the "require-trusted-types-for 'script' directive

Tests/LibWeb/Text/expected/wpt-import/trusted-types/block-string-assignment-to-HTMLElement-generic.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ Harness status: OK
22

33
Found 9 tests
44

5-
7 Pass
6-
2 Fail
5+
8 Pass
6+
1 Fail
77
Pass script.src accepts only TrustedScriptURL
88
Pass div.innerHTML accepts only TrustedHTML
9-
Fail iframe.srcdoc accepts only TrustedHTML
9+
Pass iframe.srcdoc accepts only TrustedHTML
1010
Pass script.src accepts string and null after default policy was created
1111
Fail div.innerHTML accepts string and null after default policy was created
1212
Pass iframe.srcdoc accepts string and null after default policy was created

Tests/LibWeb/Text/expected/wpt-import/trusted-types/block-string-assignment-to-HTMLIFrameElement-srcdoc.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ Harness status: OK
22

33
Found 5 tests
44

5-
4 Pass
6-
1 Fail
7-
Fail iframe.srcdoc assigned via policy (successful HTML transformation).
5+
5 Pass
6+
Pass iframe.srcdoc assigned via policy (successful HTML transformation).
87
Pass `iframe.srcdoc = string` throws.
98
Pass `iframe.srcdoc = null` throws.
109
Pass `iframe.srcdoc = string` assigned via default policy (successful HTML transformation).

Tests/LibWeb/Text/expected/wpt-import/trusted-types/policy-without-return-value.sub.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ Harness status: OK
22

33
Found 4 tests
44

5-
2 Pass
6-
2 Fail
5+
3 Pass
6+
1 Fail
77
Fail createHTML with a policy that returns undefined DOMParser
8-
Fail createHTML with a policy that returns undefined iframe.srcdoc
8+
Pass createHTML with a policy that returns undefined iframe.srcdoc
99
Pass createScript with a policy that returns undefined <div onload>
1010
Pass createScriptURL with a policy that returns undefined script.src

Tests/LibWeb/Text/expected/wpt-import/trusted-types/require-trusted-types-for-TypeError-belongs-to-the-global-object-realm.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ Harness status: OK
22

33
Found 2 tests
44

5-
1 Pass
6-
1 Fail
5+
2 Pass
76
Pass Setting innerHTML on a node inserted by the parser.
8-
Fail Setting innerHTML on a node adopted from a subframe.
7+
Pass Setting innerHTML on a node adopted from a subframe.

0 commit comments

Comments
 (0)