docs
Loading...
Searching...
No Matches
OriginRules.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
7
8#include <cstddef>
9#include <initializer_list>
10#include <span>
11#include <type_traits>
12
13namespace ultralight {
14
15///
16/// A list of origin patterns that controls which pages get an attached API.
17///
18/// When attaching a js::API, dom::Triggers, or dom::data::Context, you pass OriginRules to
19/// AttachTo() to choose which web origins get access.
20///
21/// Pass origin rules when attaching an API to allow a remote domain, its subdomains, and local
22/// files:
23///
24/// ```
25/// if (app.AttachTo(view.get(),
26/// { .origin_rules = { "https://*.mygame.com", "file://*" } }))
27/// view->LoadURL("https://play.mygame.com/");
28/// ```
29///
30/// ## Rule Syntax
31///
32/// Each rule follows the `scheme://host[:port]` format. If a rule fails to parse, the attach call
33/// fails and returns `false`.
34///
35/// | Part | Accepts |
36/// |--------|--------------------------------------------------------------------------|
37/// | Scheme | An exact scheme with no wildcards |
38/// | Host | An exact host, `*`, or `*.example.com` for the domain and all subdomains |
39/// | Port | A specific port number, `:*` for any port, or default port if omitted |
40///
41/// Common rules illustrate these patterns:
42///
43/// ```
44/// "https://store.mygame.com" // store.mygame.com on the default port (443)
45/// "https://*.mygame.com" // mygame.com and all of its subdomains
46/// "http://localhost:*" // localhost on any port
47/// "file://*" // local files
48/// ```
49///
50/// ## Default Rules
51///
52/// Attach calls restrict page access when you omit origin rules:
53///
54/// - **An empty list allows local content.** Content loaded from `file://` URLs or View::LoadHTML()
55/// gets the API.
56/// - **Passing any rule replaces the default list.** Include `"file://*"` in your list if you want
57/// to keep local page access alongside remote domains.
58///
59/// ## Origin Matching
60///
61/// Origin rules match only the scheme, host, and port of a page's web origin. Path segments, query
62/// strings, and fragment identifiers are never checked.
63///
64/// Origin matching depends on how a document is loaded:
65///
66/// - **Frames with inherited origins match by the origin they inherit.** Documents loaded in
67/// `about:blank` or `srcdoc` frames evaluate against the origin of the document that created
68/// them.
69/// - **HTML loaded with an explicit URL matches that URL.** Passing a URL to View::LoadHTML()
70/// assigns that URL's origin to the document, matching rules like any page.
71/// - **Opaque origins never match any rule.** Sandboxed frames, `data:` URLs, and View::LoadHTML()
72/// called without a URL produce opaque origins that fail all rule checks.
73///
74/// To allow an API on pages that cannot match an origin rule, set a filter on the View using
75/// js::SetInjectionFilter() or dom::SetInjectionFilter(). A dom::data::Context has no filter
76/// mechanism.
77///
78/// ## String Lifetime
79///
80/// OriginRules refers to rule strings without copying them.
81///
82/// @warning An inline list's array is destroyed when that statement ends, leaving any variable
83/// that stored it (such as an OriginRules or an AttachOptions) pointing at freed memory.
84/// The attach call copies the strings, so pass the list directly into that call or keep
85/// them in a std::vector until the call completes.
86///
87/// @see js::API::AttachTo(), dom::Triggers::AttachTo(), dom::data::Context::AttachTo(),
88/// js::SetInjectionFilter(), dom::SetInjectionFilter()
89///
91 public:
92 ///
93 /// Create an empty list (the default policy).
94 ///
95 constexpr OriginRules() = default;
96
97// Storing the list's array without copying it is intended (see String Lifetime above).
98#if defined(__GNUC__) && !defined(__clang__)
99#pragma GCC diagnostic push
100#pragma GCC diagnostic ignored "-Winit-list-lifetime"
101#endif
102
103 ///
104 /// Create a list from an inline list of rules.
105 ///
106 /// @param rules The rules, as null-terminated UTF-8 strings.
107 ///
108 constexpr OriginRules(std::initializer_list<const char*> rules)
109 : rules_(rules.begin()), size_(rules.size()) {}
110
111#if defined(__GNUC__) && !defined(__clang__)
112#pragma GCC diagnostic pop
113#endif
114
115 ///
116 /// Create a list from an array of rules.
117 ///
118 /// @param rules An array of `size` rules, as null-terminated UTF-8 strings (can be nullptr
119 /// when `size` is 0).
120 ///
121 /// @param size The number of entries in `rules`.
122 ///
123 constexpr OriginRules(const char* const* rules, size_t size) : rules_(rules), size_(size) {}
124
125 ///
126 /// Create a list from a contiguous container of rules (eg, a std::vector, std::array, or
127 /// std::span of `const char*`).
128 ///
129 /// @param rules The rules, as null-terminated UTF-8 strings.
130 ///
131 template <typename Rules>
132 requires std::is_convertible_v<const Rules&, std::span<const char* const>>
133 constexpr OriginRules(const Rules& rules) {
134 std::span<const char* const> view = rules;
135 rules_ = view.data();
136 size_ = view.size();
137 }
138
139 ///
140 /// Get the rules.
141 ///
142 /// @return Returns a pointer to the first rule (nullptr for an empty list).
143 ///
144 constexpr const char* const* data() const { return size_ ? rules_ : nullptr; }
145
146 ///
147 /// Get the number of rules.
148 ///
149 constexpr size_t size() const { return size_; }
150
151 ///
152 /// Whether or not the list is empty.
153 ///
154 constexpr bool empty() const { return size_ == 0; }
155
156 private:
157 const char* const* rules_ = nullptr;
158 size_t size_ = 0;
159};
160
161} // namespace ultralight
constexpr OriginRules(const Rules &rules)
Create a list from a contiguous container of rules (eg, a std::vector, std::array,...
Definition OriginRules.h:133
constexpr OriginRules()=default
Create an empty list (the default policy).
constexpr OriginRules(std::initializer_list< const char * > rules)
Create a list from an inline list of rules.
Definition OriginRules.h:108
constexpr size_t size() const
Get the number of rules.
Definition OriginRules.h:149
constexpr const char *const * data() const
Get the rules.
Definition OriginRules.h:144
constexpr OriginRules(const char *const *rules, size_t size)
Create a list from an array of rules.
Definition OriginRules.h:123
constexpr bool empty() const
Whether or not the list is empty.
Definition OriginRules.h:154
Root namespace for every public Ultralight type, function, and enumeration.