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
|
/*
* Copyright (C) 2022 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 "ImportMap.h"
#include "SourceCode.h"
#include <algorithm>
#include <wtf/JSONValues.h>
#include <wtf/text/MakeString.h>
namespace JSC {
namespace ImportMapInternal {
static constexpr bool verbose = false;
}
Expected<URL, String> ImportMap::resolveImportMatch(const String& normalizedSpecifier, const URL& asURL, const SpecifierMap& specifierMap)
{
// https://html.spec.whatwg.org/C#resolving-an-imports-match
auto result = specifierMap.find(normalizedSpecifier);
// 1.1.1. If resolutionResult is null, then throw a TypeError indicating that resolution of specifierKey was blocked by a null entry.
if (result != specifierMap.end()) {
if (result->value.isNull())
return makeUnexpected("specifier is blocked"_s);
return result->value;
}
if (!asURL.isValid() || asURL.hasSpecialScheme()) {
int64_t length = -1;
std::optional<URL> matched;
for (auto& [key, value] : specifierMap) {
if (key.endsWith('/')) {
auto position = normalizedSpecifier.find(key);
if (position == 0) {
if (key.length() > length) {
matched = value;
length = key.length();
}
}
}
}
if (matched) {
if (matched->isNull())
return makeUnexpected("specifier is blocked"_s);
auto afterPrefix = normalizedSpecifier.substring(length);
ASSERT(matched->string().endsWith('/'));
URL url { matched.value(), afterPrefix };
if (!url.isValid())
return makeUnexpected("specifier is blocked"_s);
if (!url.string().startsWith(matched->string()))
return makeUnexpected("specifier is blocked"_s);
return url;
}
}
return { };
}
static URL parseURLLikeModuleSpecifier(const String& specifier, const URL& baseURL)
{
// https://html.spec.whatwg.org/C#resolving-a-url-like-module-specifier
if (specifier.startsWith('/') || specifier.startsWith("./"_s) || specifier.startsWith("../"_s))
return URL(baseURL, specifier);
return URL { specifier };
}
URL ImportMap::resolve(const String& specifier, const URL& baseURL) const
{
// https://html.spec.whatwg.org/C#resolve-a-module-specifier
URL asURL = parseURLLikeModuleSpecifier(specifier, baseURL);
String normalizedSpecifier = asURL.isValid() ? asURL.string() : specifier;
dataLogLnIf(ImportMapInternal::verbose, "Resolve ", specifier, " with ", baseURL);
for (auto& entry : m_scopes) {
dataLogLnIf(ImportMapInternal::verbose, " Scope ", entry.m_scope);
if (entry.m_scope == baseURL || (entry.m_scope.string().endsWith('/') && baseURL.string().startsWith(entry.m_scope.string()))) {
dataLogLnIf(ImportMapInternal::verbose, " Matching");
auto result = resolveImportMatch(normalizedSpecifier, asURL, entry.m_map);
if (!result)
return { };
URL scopeImportsMatch = WTFMove(result.value());
if (!scopeImportsMatch.isNull())
return scopeImportsMatch;
}
}
dataLogLnIf(ImportMapInternal::verbose, " Matching with imports");
auto result = resolveImportMatch(normalizedSpecifier, asURL, m_imports);
if (!result)
return { };
URL topLevelImportsMatch = WTFMove(result.value());
if (!topLevelImportsMatch.isNull())
return topLevelImportsMatch;
if (asURL.isValid())
return asURL;
return { };
}
static String normalizeSpecifierKey(const String& specifierKey, const URL& baseURL, ImportMap::Reporter* reporter)
{
// https://html.spec.whatwg.org/C#normalizing-a-specifier-key
if (UNLIKELY(specifierKey.isEmpty())) {
if (reporter)
reporter->reportWarning("specifier key is empty"_s);
return nullString();
}
URL url = parseURLLikeModuleSpecifier(specifierKey, baseURL);
if (url.isValid())
return url.string();
return specifierKey;
}
static ImportMap::SpecifierMap sortAndNormalizeSpecifierMap(Ref<JSON::Object> importsMap, const URL& baseURL, ImportMap::Reporter* reporter)
{
// https://html.spec.whatwg.org/C#sorting-and-normalizing-a-module-specifier-map
ImportMap::SpecifierMap normalized;
for (auto& [key, value] : importsMap.get()) {
String normalizedSpecifierKey = normalizeSpecifierKey(key, baseURL, reporter);
if (normalizedSpecifierKey.isNull())
continue;
if (auto valueAsString = value->asString(); LIKELY(!valueAsString.isNull())) {
URL addressURL = parseURLLikeModuleSpecifier(valueAsString, baseURL);
if (UNLIKELY(!addressURL.isValid())) {
if (reporter)
reporter->reportWarning(makeString("value in specifier map cannot be parsed as URL "_s, valueAsString));
normalized.add(normalizedSpecifierKey, URL { });
continue;
}
if (UNLIKELY(key.endsWith('/') && !addressURL.string().endsWith('/'))) {
if (reporter)
reporter->reportWarning(makeString("address "_s, addressURL.string(), " does not end with '/' while key "_s, key, " ends with '/'"_s));
normalized.add(normalizedSpecifierKey, URL { });
continue;
}
normalized.add(normalizedSpecifierKey, WTFMove(addressURL));
} else {
if (reporter)
reporter->reportWarning("value in specifier map needs to be a string"_s);
normalized.add(normalizedSpecifierKey, URL { });
continue;
}
}
return normalized;
}
Expected<void, String> ImportMap::registerImportMap(const SourceCode& sourceCode, const URL& baseURL, ImportMap::Reporter* reporter)
{
// https://html.spec.whatwg.org/C#register-an-import-map
// https://html.spec.whatwg.org/C#parse-an-import-map-string
auto result = JSON::Value::parseJSON(sourceCode.view());
if (!result)
return makeUnexpected("ImportMap has invalid JSON"_s);
auto rootMap = result->asObject();
if (!rootMap)
return makeUnexpected("ImportMap is not a map"_s);
SpecifierMap normalizedImports;
if (auto importsMapValue = rootMap->getValue("imports"_s)) {
auto importsMap = importsMapValue->asObject();
if (!importsMap)
return makeUnexpected("imports is not a map"_s);
normalizedImports = sortAndNormalizeSpecifierMap(importsMap.releaseNonNull(), baseURL, reporter);
}
Scopes scopes;
if (auto scopesMapValue = rootMap->getValue("scopes"_s)) {
auto scopesMap = scopesMapValue->asObject();
if (!scopesMap)
return makeUnexpected("scopes is not a map"_s);
// https://html.spec.whatwg.org/C#sorting-and-normalizing-scopes
for (auto& [key, value] : *scopesMap) {
auto potentialSpecifierMap = value->asObject();
if (!potentialSpecifierMap)
return makeUnexpected("scopes' value is not a map"_s);
URL scopePrefixURL { baseURL, key }; // Do not use parseURLLikeModuleSpecifier since we should accept non relative path.
dataLogLnIf(ImportMapInternal::verbose, "scope key ", key, " and URL ", scopePrefixURL);
if (UNLIKELY(!scopePrefixURL.isValid())) {
if (reporter)
reporter->reportWarning(makeString("scope key"_s, key, " was not parsable"_s));
continue;
}
scopes.append({ scopePrefixURL, sortAndNormalizeSpecifierMap(potentialSpecifierMap.releaseNonNull(), baseURL, reporter) });
}
}
// Sort to accending order. So, more specific scope will come first.
std::sort(scopes.begin(), scopes.end(), [&](const auto& lhs, const auto& rhs) -> bool {
return codePointCompareLessThan(rhs.m_scope.string(), lhs.m_scope.string());
});
IntegrityMap integrity;
StringBuilder errorMessage;
if (auto integrityValue = rootMap->getValue("integrity"_s)) {
auto integrityMap = integrityValue->asObject();
if (!integrityMap)
return makeUnexpected("integrity is not a map"_s);
// https://html.spec.whatwg.org/C#normalizing-a-module-integrity-map
for (auto& [key, value] : *integrityMap) {
URL integrityURL = resolve(key, baseURL);
if (UNLIKELY(integrityURL.isNull())) {
errorMessage.append("Integrity URL "_s);
errorMessage.append(key);
errorMessage.append(" is not a valid absolute URL nor a relative URL starting with '/', './' or '../'\n"_s);
continue;
}
auto valueAsString = value->asString();
if (UNLIKELY(valueAsString.isNull())) {
errorMessage.append("Integrity value of "_s);
errorMessage.append(key);
errorMessage.append(" is not a string\n"_s);
continue;
}
integrity.set(integrityURL, valueAsString);
}
}
m_imports = WTFMove(normalizedImports);
m_scopes = WTFMove(scopes);
m_integrity = WTFMove(integrity);
if (!errorMessage.isEmpty())
return makeUnexpected(errorMessage.toString());
return { };
}
String ImportMap::integrityForURL(const URL& url) const
{
return m_integrity.get(url);
}
} // namespace JSC
|