LibWeb: Implement TrustedHTML class

The TrustedHTML interface represents a string that a developer can
confidently insert into an injection sink that will render it as HTML.

These objects are immutable wrappers around a string, constructed via a
TrustedTypePolicy’s createHTML method.
This commit is contained in:
Tete17 2025-07-31 00:21:13 +02:00 committed by Luke Wilde
commit 0a147aa9a1
Notes: github-actions[bot] 2025-08-11 11:23:51 +00:00
8 changed files with 86 additions and 0 deletions

View file

@ -896,6 +896,7 @@ set(SOURCES
SVG/SVGViewElement.cpp
SVG/TagNames.cpp
SVG/ViewBox.cpp
TrustedTypes/TrustedHTML.cpp
TrustedTypes/TrustedTypePolicyFactory.cpp
UIEvents/CompositionEvent.cpp
UIEvents/EventNames.cpp

View file

@ -1220,6 +1220,7 @@ ErrorOr<Web::UniqueNodeID> decode(Decoder&);
namespace Web::TrustedTypes {
class TrustedHTML;
class TrustedTypePolicyFactory;
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/TrustedTypes/TrustedHTML.h>
#include <LibGC/Ptr.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/Intrinsics.h>
namespace Web::TrustedTypes {
GC_DEFINE_ALLOCATOR(TrustedHTML);
TrustedHTML::TrustedHTML(JS::Realm& realm, Utf16String data)
: PlatformObject(realm)
, m_data(move(data))
{
}
void TrustedHTML::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(TrustedHTML);
Base::initialize(realm);
}
// https://w3c.github.io/trusted-types/dist/spec/#trustedhtml-stringification-behavior
Utf16String const& TrustedHTML::to_string() const
{
// 1. return the associated data value.
return m_data;
}
// https://w3c.github.io/trusted-types/dist/spec/#dom-trustedhtml-tojson
Utf16String const& TrustedHTML::to_json() const
{
// 1. return the associated data value.
return to_string();
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Bindings/TrustedHTMLPrototype.h>
namespace Web::TrustedTypes {
class TrustedHTML final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(TrustedHTML, Bindings::PlatformObject);
GC_DECLARE_ALLOCATOR(TrustedHTML);
public:
virtual ~TrustedHTML() override = default;
Utf16String const& to_string() const;
Utf16String const& to_json() const;
private:
explicit TrustedHTML(JS::Realm&, Utf16String);
virtual void initialize(JS::Realm&) override;
Utf16String const m_data;
};
}

View file

@ -0,0 +1,6 @@
// https://w3c.github.io/trusted-types/dist/spec/#trusted-html
[Exposed=(Window,Worker)]
interface TrustedHTML {
stringifier;
Utf16DOMString toJSON();
};

View file

@ -328,6 +328,7 @@ libweb_js_bindings(Streams/TransformStreamDefaultController)
libweb_js_bindings(Streams/WritableStream)
libweb_js_bindings(Streams/WritableStreamDefaultController)
libweb_js_bindings(Streams/WritableStreamDefaultWriter)
libweb_js_bindings(TrustedTypes/TrustedHTML)
libweb_js_bindings(TrustedTypes/TrustedTypePolicyFactory)
libweb_js_bindings(SVG/SVGAElement)
libweb_js_bindings(SVG/SVGAnimatedEnumeration)

View file

@ -116,6 +116,7 @@ static bool is_platform_object(Type const& type)
"TextMetrics"sv,
"TextTrack"sv,
"TimeRanges"sv,
"TrustedHTML"sv,
"TrustedTypePolicyFactory"sv,
"URLSearchParams"sv,
"VTTRegion"sv,

View file

@ -432,6 +432,7 @@ TransformStream
TransformStreamDefaultController
TransitionEvent
TreeWalker
TrustedHTML
TrustedTypePolicyFactory
TypeError
UIEvent