1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/net/URLPatternGlue.h"
#include "mozilla/dom/ScriptSettings.h" // for AutoJSAPI
#include "js/RegExp.h"
#include "js/RegExpFlags.h"
#include "js/RootingAPI.h"
#include "js/Array.h" // JS::GetArrayLength
#include "js/GlobalObject.h" // JS::CurrentGlobalOrNull
#include "nsJSUtils.h"
#include "mozilla/dom/BindingUtils.h"
mozilla::LazyLogModule gUrlPatternLog("urlpattern");
namespace mozilla::net {
UrlPatternInput CreateUrlPatternInput(const nsACString& url) {
UrlPatternInput input;
input.string_or_init_type = UrlPatternStringOrInitType::String;
input.str = url;
return input;
}
UrlPatternInput CreateUrlPatternInput(const UrlPatternInit& init) {
UrlPatternInput input;
input.string_or_init_type = UrlPatternStringOrInitType::Init;
input.init = init;
return input;
}
MaybeString CreateMaybeString(const nsACString& str, bool valid) {
return MaybeString{.string = nsCString(str), .valid = valid};
}
MaybeString CreateMaybeStringNone() {
return MaybeString{.string = ""_ns, .valid = false};
}
nsAutoCString UrlPatternGetProtocol(const UrlPatternGlue aPattern) {
nsAutoCString result;
urlpattern_component_get_pattern_string(
urlpattern_get_protocol_component(aPattern), &result);
return result;
}
nsAutoCString UrlPatternGetUsername(const UrlPatternGlue aPattern) {
nsAutoCString result;
urlpattern_component_get_pattern_string(
urlpattern_get_username_component(aPattern), &result);
return result;
}
nsAutoCString UrlPatternGetPassword(const UrlPatternGlue aPattern) {
nsAutoCString result;
urlpattern_component_get_pattern_string(
urlpattern_get_password_component(aPattern), &result);
return result;
}
nsAutoCString UrlPatternGetHostname(const UrlPatternGlue aPattern) {
nsAutoCString result;
urlpattern_component_get_pattern_string(
urlpattern_get_hostname_component(aPattern), &result);
return result;
}
nsAutoCString UrlPatternGetPort(const UrlPatternGlue aPattern) {
nsAutoCString result;
urlpattern_component_get_pattern_string(
urlpattern_get_port_component(aPattern), &result);
return result;
}
nsAutoCString UrlPatternGetPathname(const UrlPatternGlue aPattern) {
nsAutoCString result;
urlpattern_component_get_pattern_string(
urlpattern_get_pathname_component(aPattern), &result);
return result;
}
nsAutoCString UrlPatternGetSearch(const UrlPatternGlue aPattern) {
nsAutoCString result;
urlpattern_component_get_pattern_string(
urlpattern_get_search_component(aPattern), &result);
return result;
}
nsAutoCString UrlPatternGetHash(const UrlPatternGlue aPattern) {
nsAutoCString result;
urlpattern_component_get_pattern_string(
urlpattern_get_hash_component(aPattern), &result);
return result;
}
// https://urlpattern.spec.whatwg.org/#create-a-component-match-result
Maybe<UrlPatternComponentResult> ComponentMatches(
UrlPatternComponentPtr* aComponentPtr, nsACString& aInput,
bool aMatchOnly) {
UrlPatternComponentResult res;
// if a component's regexp is empty then we can skip prefix/suffix parsing,
// any capture or regexp logic and group list building
// and simply match on the empty string
if (urlpattern_component_is_regexp_string_empty(aComponentPtr)) {
if (aInput != "") {
return Nothing();
}
} else { // non-empty regexp requires deeper matching and group population
nsTArray<MaybeString> matches;
if (!urlpattern_component_matches(aComponentPtr, &aInput, aMatchOnly,
&matches)) {
return Nothing();
}
// if we are only doing a pattern.test(), then we don't need the
// ComponentResults (groups and input) to be fully populated,
// we just need to know they exist. so we can cut out early
if (aMatchOnly) {
return Some(res);
}
nsTArray<nsCString> groupNames;
urlpattern_component_get_group_name_list(aComponentPtr, &groupNames);
for (size_t i = 0; i < matches.Length(); i++) {
// Insert all capture groups, both matched and unmatched
// The valid flag will be used later to set undefined vs string value
res.mGroups.InsertOrUpdate(groupNames[i], std::move(matches[i]));
}
}
res.mInput = aInput;
return Some(res);
}
Maybe<UrlPatternResult> AllComponentMatches(const UrlPatternGlue aPattern,
UrlPatternMatchInput& aMatchInput,
bool aMatchOnly) {
UrlPatternResult res;
res.mProtocol = ComponentMatches(urlpattern_get_protocol_component(aPattern),
aMatchInput.protocol, aMatchOnly);
if (res.mProtocol.isNothing()) {
return Nothing();
}
res.mUsername = ComponentMatches(urlpattern_get_username_component(aPattern),
aMatchInput.username, aMatchOnly);
if (res.mUsername.isNothing()) {
return Nothing();
}
res.mPassword = ComponentMatches(urlpattern_get_password_component(aPattern),
aMatchInput.password, aMatchOnly);
if (res.mPassword.isNothing()) {
return Nothing();
}
res.mHostname = ComponentMatches(urlpattern_get_hostname_component(aPattern),
aMatchInput.hostname, aMatchOnly);
if (res.mHostname.isNothing()) {
return Nothing();
}
res.mPort = ComponentMatches(urlpattern_get_port_component(aPattern),
aMatchInput.port, aMatchOnly);
if (res.mPort.isNothing()) {
return Nothing();
}
res.mPathname = ComponentMatches(urlpattern_get_pathname_component(aPattern),
aMatchInput.pathname, aMatchOnly);
if (res.mPathname.isNothing()) {
return Nothing();
}
res.mSearch = ComponentMatches(urlpattern_get_search_component(aPattern),
aMatchInput.search, aMatchOnly);
if (res.mSearch.isNothing()) {
return Nothing();
}
res.mHash = ComponentMatches(urlpattern_get_hash_component(aPattern),
aMatchInput.hash, aMatchOnly);
if (res.mHash.isNothing()) {
return Nothing();
}
return Some(res);
}
Maybe<UrlPatternResult> UrlPatternExec(UrlPatternGlue aPattern,
const UrlPatternInput& aInput,
Maybe<nsAutoCString> aMaybeBaseUrl,
bool aIgnoreCase) {
MOZ_LOG(gUrlPatternLog, LogLevel::Debug, ("UrlPatternExec()...\n"));
UrlPatternMatchInputAndInputs matchInputAndInputs;
if (aInput.string_or_init_type == UrlPatternStringOrInitType::Init) {
MOZ_ASSERT(aMaybeBaseUrl.isNothing());
if (!urlpattern_process_match_input_from_init(&aInput.init, nullptr,
&matchInputAndInputs)) {
return Nothing();
}
} else {
nsAutoCString* baseUrl = nullptr;
if (aMaybeBaseUrl.isSome()) {
baseUrl = &aMaybeBaseUrl.ref();
}
if (!urlpattern_process_match_input_from_string(&aInput.str, baseUrl,
&matchInputAndInputs)) {
return Nothing();
}
}
// shouldn't be any need to convert the urlpatternwrapper to quirks wrapper
// the all_component_matches signature should be able to receive as a wrapper
Maybe<UrlPatternResult> res =
AllComponentMatches(aPattern, matchInputAndInputs.input, false);
if (res.isNothing()) {
return Nothing();
}
if (matchInputAndInputs.inputs.string_or_init_type ==
UrlPatternStringOrInitType::Init) {
res->mInputs.AppendElement(
CreateUrlPatternInput(matchInputAndInputs.inputs.init));
} else {
res->mInputs.AppendElement(
CreateUrlPatternInput(matchInputAndInputs.inputs.str));
if (matchInputAndInputs.inputs.base.valid) {
res->mInputs.AppendElement(
CreateUrlPatternInput(matchInputAndInputs.inputs.base.string));
}
}
return res;
}
bool UrlPatternTest(UrlPatternGlue aPattern, const UrlPatternInput& aInput,
Maybe<nsAutoCString> aMaybeBaseUrl, bool aIgnoreCase) {
MOZ_LOG(gUrlPatternLog, LogLevel::Debug, ("UrlPatternTest()...\n"));
UrlPatternMatchInputAndInputs matchInputAndInputs;
if (aInput.string_or_init_type == UrlPatternStringOrInitType::Init) {
MOZ_ASSERT(aMaybeBaseUrl.isNothing());
if (!urlpattern_process_match_input_from_init(&aInput.init, nullptr,
&matchInputAndInputs)) {
return false;
}
} else {
nsAutoCString* baseUrl = nullptr;
if (aMaybeBaseUrl.isSome()) {
baseUrl = &aMaybeBaseUrl.ref();
}
if (!urlpattern_process_match_input_from_string(&aInput.str, baseUrl,
&matchInputAndInputs)) {
return false;
}
}
// shouldn't be any need to convert the urlpatternwrapper to quirks wrapper
// the all_component_matches signature should be able to receive as a wrapper
Maybe<UrlPatternResult> res =
AllComponentMatches(aPattern, matchInputAndInputs.input, true);
return !res.isNothing();
}
// Implementation for the same object represented in rust as RegExpObjWrapper
// we are using this class to root the spidermonkey regexp object returned
// from parsing so that we can hold onto it longer without it getting cleaned up
// by garbage collection.
// As noted elsewhere, this object gets held by SpiderMonkeyRegexp,
// which is ultimately held by the dom::URLPattern.
class RegExpObjImpl {
public:
// should be okay but doesn't participate in slicing of incremental GC
// alternative: implement trace method, called from dom::URLPattern
JS::PersistentRooted<JSObject*> mRegexp; // advertising directly to GC
explicit RegExpObjImpl(JSContext* cx, JSObject* aJsObj)
: mRegexp(cx, aJsObj) {}
};
extern "C" bool parse_regexp_ffi(const uint16_t* pattern, uintptr_t pattern_len,
const uint16_t* flags, uintptr_t flags_len,
RegExpObjWrapper** res) {
dom::AutoJSAPI jsapi;
jsapi.Init();
JSContext* cx = jsapi.cx();
AutoDisableJSInterruptCallback disabler(cx);
JSAutoRealm ar(
cx, dom::binding_detail::UnprivilegedJunkScopeOrWorkerGlobal(fallible));
const char16_t* flagsStr = reinterpret_cast<const char16_t*>(flags);
JS::RegExpFlags regexpFlags = JS::RegExpFlag::UnicodeSets;
for (uintptr_t i = 0; i < flags_len; i++) {
switch (flagsStr[i]) {
case u'i':
regexpFlags = regexpFlags | JS::RegExpFlag::IgnoreCase;
break;
default:
break;
}
}
const char16_t* patternStr = reinterpret_cast<const char16_t*>(pattern);
JS::Rooted<JSObject*> regexp(
cx, JS::NewUCRegExpObject(cx, patternStr, pattern_len, regexpFlags));
if (!regexp) {
if (JS_IsExceptionPending(cx)) {
JS_ClearPendingException(cx);
}
return false;
}
RegExpObjImpl* w = new RegExpObjImpl(cx, regexp);
*res =
reinterpret_cast<RegExpObjWrapper*>(w); // can't static_cast to void **
return true;
}
bool matches_regexp(RegExpObjWrapper* const* regexpWrapper,
const uint8_t* aText, uintptr_t aTextLen, bool aMatchOnly,
bool* aMatchResult, nsTArray<MaybeString>* aRegexResults) {
dom::AutoJSAPI jsapi;
jsapi.Init();
JSContext* cx = jsapi.cx();
AutoDisableJSInterruptCallback disabler(cx);
RegExpObjImpl* regExpObjImpl =
reinterpret_cast<RegExpObjImpl*>(*regexpWrapper);
JSAutoRealm ar(cx, regExpObjImpl->mRegexp);
// SM expects utf-16 strings and the matches_regexp API has been simplified to
// only deal with utf-8, so we convert as necessary here
nsDependentCSubstring utf8Text(reinterpret_cast<const char*>(aText),
aTextLen);
NS_ConvertUTF8toUTF16 utf16Text(utf8Text);
const char16_t* text = utf16Text.get();
JS::Rooted<JS::Value> regexResult(cx, JS::NullValue());
size_t textLen = utf16Text.Length();
size_t index = 0;
if (!JS::ExecuteRegExpNoStatics(cx, regExpObjImpl->mRegexp, text, textLen,
&index, aMatchOnly, ®exResult)) {
return false;
}
// On no match, ExecuteRegExpNoStatics returns Null
if (regexResult.isNull()) {
*aMatchResult = false;
return true;
}
// We have a match
*aMatchResult = true;
// early exit if we requested aMatchOnly because we don't need the results
if (aMatchOnly) {
MOZ_ASSERT(regexResult.isBoolean() && regexResult.toBoolean());
return true;
}
if (aRegexResults == nullptr) {
return false;
}
// Now we know we have a result, and we need to extract it so we can read it.
uint32_t length;
JS::Rooted<JSObject*> regexResultObj(cx, ®exResult.toObject());
if (!JS::GetArrayLength(cx, regexResultObj, &length)) {
return false;
}
// Skip index 0 (the full match) and only return captured groups
for (uint32_t i = 1; i < length; i++) {
JS::Rooted<JS::Value> element(cx);
if (!JS_GetElement(cx, regexResultObj, i, &element)) {
return false;
}
// If capture group didn't match (is undefined), add invalid MaybeString
if (element.isUndefined()) {
aRegexResults->AppendElement(CreateMaybeStringNone());
continue;
}
nsAutoJSString value;
if (!value.init(cx, element)) {
return false;
}
aRegexResults->AppendElement(
CreateMaybeString(NS_ConvertUTF16toUTF8(value), true));
}
return true;
}
extern "C" bool matches_regexp_ffi(RegExpObjWrapper* const* regexp_wrapper,
const uint8_t* string, uintptr_t string_len,
bool match_only, bool* match_result,
nsTArray<MaybeString>* res) {
if (!matches_regexp(regexp_wrapper, string, string_len, match_only,
match_result, res)) {
return false;
}
return true;
}
extern "C" void free_regexp_ffi(RegExpObjWrapper* regexp_wrapper) {
if (regexp_wrapper) {
RegExpObjImpl* impl = reinterpret_cast<RegExpObjImpl*>(regexp_wrapper);
delete impl;
}
}
} // namespace mozilla::net
|