Bug 1737295 - Improve exemption matching and handling of off-main-thread reports r=ckerschb

Differential Revision: https://phabricator.services.mozilla.com/D129276
This commit is contained in:
Tom Ritter 2021-10-22 18:38:13 +00:00
Родитель 77f4e76e2c
Коммит a847bad0ce
2 изменённых файлов: 57 добавлений и 41 удалений

Просмотреть файл

@ -352,46 +352,44 @@ FilenameTypeAndDetails nsContentSecurityUtils::FilenameToFilenameType(
return FilenameTypeAndDetails(kDataUri, Nothing());
}
if (!NS_IsMainThread()) {
// We can't do Regex matching off the main thread; so just report.
return FilenameTypeAndDetails(kOtherWorker, Nothing());
}
// Can't do regex matching off-main-thread
if (NS_IsMainThread()) {
// Extension as loaded via a file://
bool regexMatch;
nsTArray<nsString> regexResults;
nsresult rv = RegexEval(kExtensionRegex, fileName, /* aOnlyMatch = */ false,
regexMatch, &regexResults);
if (NS_FAILED(rv)) {
return FilenameTypeAndDetails(kRegexFailure, Nothing());
}
if (regexMatch) {
nsCString type = StringEndsWith(regexResults[2], u"mozilla.org.xpi"_ns)
? kMozillaExtensionFile
: kOtherExtensionFile;
const auto& extensionNameAndPath =
Substring(regexResults[0], ArrayLength("extensions/") - 1);
return FilenameTypeAndDetails(
type, Some(OptimizeFileName(extensionNameAndPath)));
}
// Extension as loaded via a file://
bool regexMatch;
nsTArray<nsString> regexResults;
nsresult rv = RegexEval(kExtensionRegex, fileName, /* aOnlyMatch = */ false,
regexMatch, &regexResults);
if (NS_FAILED(rv)) {
return FilenameTypeAndDetails(kRegexFailure, Nothing());
}
if (regexMatch) {
nsCString type = StringEndsWith(regexResults[2], u"mozilla.org.xpi"_ns)
? kMozillaExtensionFile
: kOtherExtensionFile;
auto& extensionNameAndPath =
Substring(regexResults[0], ArrayLength("extensions/") - 1);
return FilenameTypeAndDetails(type,
Some(OptimizeFileName(extensionNameAndPath)));
}
// Single File
rv = RegexEval(kSingleFileRegex, fileName, /* aOnlyMatch = */ true,
regexMatch);
if (NS_FAILED(rv)) {
return FilenameTypeAndDetails(kRegexFailure, Nothing());
}
if (regexMatch) {
return FilenameTypeAndDetails(kSingleString, Some(fileName));
}
// Single File
rv = RegexEval(kSingleFileRegex, fileName, /* aOnlyMatch = */ true,
regexMatch);
if (NS_FAILED(rv)) {
return FilenameTypeAndDetails(kRegexFailure, Nothing());
}
if (regexMatch) {
return FilenameTypeAndDetails(kSingleString, Some(fileName));
}
// Suspected userChromeJS script
rv = RegexEval(kUCJSRegex, fileName, /* aOnlyMatch = */ true, regexMatch);
if (NS_FAILED(rv)) {
return FilenameTypeAndDetails(kRegexFailure, Nothing());
}
if (regexMatch) {
return FilenameTypeAndDetails(kSuspectedUserChromeJS, Nothing());
// Suspected userChromeJS script
rv = RegexEval(kUCJSRegex, fileName, /* aOnlyMatch = */ true, regexMatch);
if (NS_FAILED(rv)) {
return FilenameTypeAndDetails(kRegexFailure, Nothing());
}
if (regexMatch) {
return FilenameTypeAndDetails(kSuspectedUserChromeJS, Nothing());
}
}
// Something loaded via an about:// URI.
@ -488,6 +486,9 @@ FilenameTypeAndDetails nsContentSecurityUtils::FilenameToFilenameType(
}
#endif
if (!NS_IsMainThread()) {
return FilenameTypeAndDetails(kOtherWorker, Nothing());
}
return FilenameTypeAndDetails(kOther, Nothing());
}
@ -1296,16 +1297,26 @@ bool nsContentSecurityUtils::ValidateScriptFilename(const char* aFilename,
}
}
auto kAllowedFilenames = {
auto kAllowedFilenamesExact = {
// Allow through the injection provided by about:sync addon
u"data:,new function() {\n Components.utils.import(\"chrome://aboutsync/content/AboutSyncRedirector.js\");\n AboutSyncRedirector.register();\n}"_ns,
};
for (auto allowedFilename : kAllowedFilenamesExact) {
if (filenameU == allowedFilename) {
return true;
}
}
auto kAllowedFilenamesPrefix = {
// Until 371900 is fixed, we need to do something about about:downloads
// and this is the most reasonable. See 1727770
u"about:downloads"_ns,
// We think this is the same problem as about:downloads
u"about:preferences"_ns};
for (auto allowedFilename : kAllowedFilenames) {
if (filenameU == allowedFilename) {
for (auto allowedFilenamePrefix : kAllowedFilenamesPrefix) {
if (StringBeginsWith(filenameU, allowedFilenamePrefix)) {
return true;
}
}

Просмотреть файл

@ -38,6 +38,11 @@ static constexpr auto kOther = "other"_ns;
ASSERT_TRUE((condition));
// Usage: ASSERT_AND_PRINT(ret.first, ret.second.value(), ...
#define ASSERT_AND_PRINT_FIRST(first, condition) \
fprintf(stderr, "First: %s\n", (first).get()); \
ASSERT_TRUE((condition));
// Usage: ASSERT_AND_PRINT_FIRST(ret.first, ...
TEST(FilenameEvalParser, ResourceChrome)
{
{