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
|
/*
* Copyright (C) 2014-2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ContentExtensionParser.h"
#if ENABLE(CONTENT_EXTENSIONS)
#include "CSSParser.h"
#include "CSSParserMode.h"
#include "CSSSelectorList.h"
#include "ContentExtensionError.h"
#include "ContentExtensionRule.h"
#include "ContentExtensionsBackend.h"
#include "ContentExtensionsDebugging.h"
#include <JavaScriptCore/JSCInlines.h>
#include <JavaScriptCore/JSGlobalObject.h>
#include <JavaScriptCore/JSONObject.h>
#include <JavaScriptCore/VM.h>
#include <wtf/CurrentTime.h>
#include <wtf/Expected.h>
#include <wtf/text/WTFString.h>
using namespace JSC;
namespace WebCore {
namespace ContentExtensions {
static bool containsOnlyASCIIWithNoUppercase(const String& domain)
{
for (unsigned i = 0; i < domain.length(); ++i) {
UChar c = domain.at(i);
if (!isASCII(c) || isASCIIUpper(c))
return false;
}
return true;
}
static Expected<Vector<String>, std::error_code> getStringList(ExecState& exec, const JSObject* arrayObject)
{
static const ContentExtensionError error = ContentExtensionError::JSONInvalidConditionList;
VM& vm = exec.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!arrayObject || !isJSArray(arrayObject))
return makeUnexpected(error);
const JSArray* array = jsCast<const JSArray*>(arrayObject);
Vector<String> strings;
unsigned length = array->length();
for (unsigned i = 0; i < length; ++i) {
const JSValue value = array->getIndex(&exec, i);
if (scope.exception() || !value.isString())
return makeUnexpected(error);
const String& string = asString(value)->value(&exec);
if (string.isEmpty())
return makeUnexpected(error);
strings.append(string);
}
return WTFMove(strings);
}
static Expected<Vector<String>, std::error_code> getDomainList(ExecState& exec, const JSObject* arrayObject)
{
auto strings = getStringList(exec, arrayObject);
if (!strings.hasValue())
return strings;
for (auto& domain : strings.value()) {
// Domains should be punycode encoded lower case.
if (!containsOnlyASCIIWithNoUppercase(domain))
return makeUnexpected(ContentExtensionError::JSONDomainNotLowerCaseASCII);
}
return strings;
}
static std::error_code getTypeFlags(ExecState& exec, const JSValue& typeValue, ResourceFlags& flags, uint16_t (*stringToType)(const String&))
{
VM& vm = exec.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!typeValue.isObject())
return { };
const JSObject* object = typeValue.toObject(&exec);
scope.assertNoException();
if (!isJSArray(object))
return ContentExtensionError::JSONInvalidTriggerFlagsArray;
const JSArray* array = jsCast<const JSArray*>(object);
unsigned length = array->length();
for (unsigned i = 0; i < length; ++i) {
const JSValue value = array->getIndex(&exec, i);
if (scope.exception() || !value)
return ContentExtensionError::JSONInvalidObjectInTriggerFlagsArray;
String name = value.toWTFString(&exec);
uint16_t type = stringToType(name);
if (!type)
return ContentExtensionError::JSONInvalidStringInTriggerFlagsArray;
flags |= type;
}
return { };
}
static Expected<Trigger, std::error_code> loadTrigger(ExecState& exec, const JSObject& ruleObject)
{
VM& vm = exec.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
const JSValue triggerObject = ruleObject.get(&exec, Identifier::fromString(&exec, "trigger"));
if (!triggerObject || scope.exception() || !triggerObject.isObject())
return makeUnexpected(ContentExtensionError::JSONInvalidTrigger);
const JSValue urlFilterObject = triggerObject.get(&exec, Identifier::fromString(&exec, "url-filter"));
if (!urlFilterObject || scope.exception() || !urlFilterObject.isString())
return makeUnexpected(ContentExtensionError::JSONInvalidURLFilterInTrigger);
String urlFilter = asString(urlFilterObject)->value(&exec);
if (urlFilter.isEmpty())
return makeUnexpected(ContentExtensionError::JSONInvalidURLFilterInTrigger);
Trigger trigger;
trigger.urlFilter = urlFilter;
const JSValue urlFilterCaseValue = triggerObject.get(&exec, Identifier::fromString(&exec, "url-filter-is-case-sensitive"));
if (urlFilterCaseValue && !scope.exception() && urlFilterCaseValue.isBoolean())
trigger.urlFilterIsCaseSensitive = urlFilterCaseValue.toBoolean(&exec);
const JSValue topURLFilterCaseValue = triggerObject.get(&exec, Identifier::fromString(&exec, "top-url-filter-is-case-sensitive"));
if (topURLFilterCaseValue && !scope.exception() && topURLFilterCaseValue.isBoolean())
trigger.topURLConditionIsCaseSensitive = topURLFilterCaseValue.toBoolean(&exec);
const JSValue resourceTypeValue = triggerObject.get(&exec, Identifier::fromString(&exec, "resource-type"));
if (!scope.exception() && resourceTypeValue.isObject()) {
auto typeFlagsError = getTypeFlags(exec, resourceTypeValue, trigger.flags, readResourceType);
if (typeFlagsError)
return makeUnexpected(typeFlagsError);
} else if (!resourceTypeValue.isUndefined())
return makeUnexpected(ContentExtensionError::JSONInvalidTriggerFlagsArray);
const JSValue loadTypeValue = triggerObject.get(&exec, Identifier::fromString(&exec, "load-type"));
if (!scope.exception() && loadTypeValue.isObject()) {
auto typeFlagsError = getTypeFlags(exec, loadTypeValue, trigger.flags, readLoadType);
if (typeFlagsError)
return makeUnexpected(typeFlagsError);
} else if (!loadTypeValue.isUndefined())
return makeUnexpected(ContentExtensionError::JSONInvalidTriggerFlagsArray);
const JSValue ifDomainValue = triggerObject.get(&exec, Identifier::fromString(&exec, "if-domain"));
if (!scope.exception() && ifDomainValue.isObject()) {
auto ifDomain = getDomainList(exec, asObject(ifDomainValue));
if (!ifDomain.hasValue())
return makeUnexpected(ifDomain.error());
trigger.conditions = WTFMove(ifDomain.value());
if (trigger.conditions.isEmpty())
return makeUnexpected(ContentExtensionError::JSONInvalidConditionList);
ASSERT(trigger.conditionType == Trigger::ConditionType::None);
trigger.conditionType = Trigger::ConditionType::IfDomain;
} else if (!ifDomainValue.isUndefined())
return makeUnexpected(ContentExtensionError::JSONInvalidConditionList);
const JSValue unlessDomainValue = triggerObject.get(&exec, Identifier::fromString(&exec, "unless-domain"));
if (!scope.exception() && unlessDomainValue.isObject()) {
if (trigger.conditionType != Trigger::ConditionType::None)
return makeUnexpected(ContentExtensionError::JSONMultipleConditions);
auto unlessDomain = getDomainList(exec, asObject(unlessDomainValue));
if (!unlessDomain.hasValue())
return makeUnexpected(unlessDomain.error());
trigger.conditions = WTFMove(unlessDomain.value());
if (trigger.conditions.isEmpty())
return makeUnexpected(ContentExtensionError::JSONInvalidConditionList);
trigger.conditionType = Trigger::ConditionType::UnlessDomain;
} else if (!unlessDomainValue.isUndefined())
return makeUnexpected(ContentExtensionError::JSONInvalidConditionList);
const JSValue ifTopURLValue = triggerObject.get(&exec, Identifier::fromString(&exec, "if-top-url"));
if (!scope.exception() && ifTopURLValue.isObject()) {
if (trigger.conditionType != Trigger::ConditionType::None)
return makeUnexpected(ContentExtensionError::JSONMultipleConditions);
auto ifTopURL = getStringList(exec, asObject(ifTopURLValue));
if (!ifTopURL.hasValue())
return makeUnexpected(ifTopURL.error());
trigger.conditions = WTFMove(ifTopURL.value());
if (trigger.conditions.isEmpty())
return makeUnexpected(ContentExtensionError::JSONInvalidConditionList);
trigger.conditionType = Trigger::ConditionType::IfTopURL;
} else if (!ifTopURLValue.isUndefined())
return makeUnexpected(ContentExtensionError::JSONInvalidConditionList);
const JSValue unlessTopURLValue = triggerObject.get(&exec, Identifier::fromString(&exec, "unless-top-url"));
if (!scope.exception() && unlessTopURLValue.isObject()) {
if (trigger.conditionType != Trigger::ConditionType::None)
return makeUnexpected(ContentExtensionError::JSONMultipleConditions);
auto unlessTopURL = getStringList(exec, asObject(unlessTopURLValue));
if (!unlessTopURL.hasValue())
return makeUnexpected(unlessTopURL.error());
trigger.conditions = WTFMove(unlessTopURL.value());
if (trigger.conditions.isEmpty())
return makeUnexpected(ContentExtensionError::JSONInvalidConditionList);
trigger.conditionType = Trigger::ConditionType::UnlessTopURL;
} else if (!unlessTopURLValue.isUndefined())
return makeUnexpected(ContentExtensionError::JSONInvalidConditionList);
return WTFMove(trigger);
}
bool isValidCSSSelector(const String& selector)
{
AtomicString::init();
QualifiedName::init();
CSSParserContext context(HTMLQuirksMode);
CSSParser parser(context);
CSSSelectorList selectorList;
parser.parseSelector(selector, selectorList);
return selectorList.isValid();
}
static Expected<std::optional<Action>, std::error_code> loadAction(ExecState& exec, const JSObject& ruleObject)
{
VM& vm = exec.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
const JSValue actionObject = ruleObject.get(&exec, Identifier::fromString(&exec, "action"));
if (!actionObject || scope.exception() || !actionObject.isObject())
return makeUnexpected(ContentExtensionError::JSONInvalidAction);
const JSValue typeObject = actionObject.get(&exec, Identifier::fromString(&exec, "type"));
if (!typeObject || scope.exception() || !typeObject.isString())
return makeUnexpected(ContentExtensionError::JSONInvalidActionType);
String actionType = asString(typeObject)->value(&exec);
if (actionType == "block")
return {{ActionType::BlockLoad}};
if (actionType == "ignore-previous-rules")
return {{ActionType::IgnorePreviousRules}};
if (actionType == "block-cookies")
return {{ActionType::BlockCookies}};
if (actionType == "css-display-none") {
JSValue selector = actionObject.get(&exec, Identifier::fromString(&exec, "selector"));
if (!selector || scope.exception() || !selector.isString())
return makeUnexpected(ContentExtensionError::JSONInvalidCSSDisplayNoneActionType);
String selectorString = asString(selector)->value(&exec);
if (!isValidCSSSelector(selectorString)) {
// Skip rules with invalid selectors to be backwards-compatible.
return {std::nullopt};
}
return {Action(ActionType::CSSDisplayNoneSelector, selectorString)};
}
if (actionType == "make-https")
return {{ActionType::MakeHTTPS}};
return makeUnexpected(ContentExtensionError::JSONInvalidActionType);
}
static Expected<std::optional<ContentExtensionRule>, std::error_code> loadRule(ExecState& exec, const JSObject& ruleObject)
{
auto trigger = loadTrigger(exec, ruleObject);
if (!trigger.hasValue())
return makeUnexpected(trigger.error());
auto action = loadAction(exec, ruleObject);
if (!action.hasValue())
return makeUnexpected(action.error());
if (action.value())
return {{{WTFMove(trigger.value()), WTFMove(action.value().value())}}};
return {std::nullopt};
}
static Expected<Vector<ContentExtensionRule>, std::error_code> loadEncodedRules(ExecState& exec, String&& ruleJSON)
{
VM& vm = exec.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
// FIXME: JSONParse should require callbacks instead of an ExecState.
const JSValue decodedRules = JSONParse(&exec, ruleJSON);
if (scope.exception() || !decodedRules)
return makeUnexpected(ContentExtensionError::JSONInvalid);
if (!decodedRules.isObject())
return makeUnexpected(ContentExtensionError::JSONTopLevelStructureNotAnObject);
const JSObject* topLevelObject = decodedRules.toObject(&exec);
if (!topLevelObject || scope.exception())
return makeUnexpected(ContentExtensionError::JSONTopLevelStructureNotAnObject);
if (!isJSArray(topLevelObject))
return makeUnexpected(ContentExtensionError::JSONTopLevelStructureNotAnArray);
const JSArray* topLevelArray = jsCast<const JSArray*>(topLevelObject);
Vector<ContentExtensionRule> ruleList;
unsigned length = topLevelArray->length();
const unsigned maxRuleCount = 50000;
if (length > maxRuleCount)
return makeUnexpected(ContentExtensionError::JSONTooManyRules);
for (unsigned i = 0; i < length; ++i) {
const JSValue value = topLevelArray->getIndex(&exec, i);
if (scope.exception() || !value)
return makeUnexpected(ContentExtensionError::JSONInvalidObjectInTopLevelArray);
const JSObject* ruleObject = value.toObject(&exec);
if (!ruleObject || scope.exception())
return makeUnexpected(ContentExtensionError::JSONInvalidRule);
auto rule = loadRule(exec, *ruleObject);
if (!rule.hasValue())
return makeUnexpected(rule.error());
if (rule.value())
ruleList.append(*rule.value());
}
return WTFMove(ruleList);
}
Expected<Vector<ContentExtensionRule>, std::error_code> parseRuleList(String&& ruleJSON)
{
#if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
double loadExtensionStartTime = monotonicallyIncreasingTime();
#endif
RefPtr<VM> vm = VM::create();
JSLockHolder locker(vm.get());
JSGlobalObject* globalObject = JSGlobalObject::create(*vm, JSGlobalObject::createStructure(*vm, jsNull()));
ExecState* exec = globalObject->globalExec();
auto ruleList = loadEncodedRules(*exec, WTFMove(ruleJSON));
vm = nullptr;
if (!ruleList.hasValue())
return makeUnexpected(ruleList.error());
if (ruleList->isEmpty())
return makeUnexpected(ContentExtensionError::JSONContainsNoRules);
#if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
double loadExtensionEndTime = monotonicallyIncreasingTime();
dataLogF("Time spent loading extension %f\n", (loadExtensionEndTime - loadExtensionStartTime));
#endif
return WTFMove(*ruleList);
}
} // namespace ContentExtensions
} // namespace WebCore
#endif // ENABLE(CONTENT_EXTENSIONS)
|