OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "core/frame/SubresourceIntegrity.h" | 6 #include "core/frame/SubresourceIntegrity.h" |
7 | 7 |
8 #include "core/HTMLNames.h" | 8 #include "core/HTMLNames.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/dom/Element.h" | 10 #include "core/dom/Element.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 String attribute = element.fastGetAttribute(HTMLNames::integrityAttr); | 107 String attribute = element.fastGetAttribute(HTMLNames::integrityAttr); |
108 if (attribute.isEmpty()) | 108 if (attribute.isEmpty()) |
109 return true; | 109 return true; |
110 | 110 |
111 if (!resource.isEligibleForIntegrityCheck(document.securityOrigin())) { | 111 if (!resource.isEligibleForIntegrityCheck(document.securityOrigin())) { |
112 UseCounter::count(document, UseCounter::SRIElementIntegrityAttributeButI neligible); | 112 UseCounter::count(document, UseCounter::SRIElementIntegrityAttributeButI neligible); |
113 logErrorToConsole("Subresource Integrity: The resource '" + resourceUrl. elidedString() + "' has an integrity attribute, but the resource requires the re quest to be CORS enabled to check the integrity, and it is not. The resource has not been blocked, but no integrity check occurred.", document); | 113 logErrorToConsole("Subresource Integrity: The resource '" + resourceUrl. elidedString() + "' has an integrity attribute, but the resource requires the re quest to be CORS enabled to check the integrity, and it is not. The resource has not been blocked, but no integrity check occurred.", document); |
114 return false; | 114 return false; |
115 } | 115 } |
116 | 116 |
117 String errorMessage; | |
118 bool result = CheckSubresourceIntegrity(attribute, source, resourceUrl, docu ment, errorMessage); | |
119 logErrorToConsole(errorMessage, document); | |
sof
2015/08/20 20:29:29
Don't you need to consider 'result' before logging
jww
2015/08/20 23:16:44
Yes, fantastic catch. Thanks, and fixed.
| |
120 return result; | |
121 } | |
122 | |
123 bool SubresourceIntegrity::CheckSubresourceIntegrity(const String& integrityMeta data, const WTF::String& source, const KURL& resourceUrl, Document& document, St ring& errorMessage) | |
124 { | |
117 WTF::Vector<IntegrityMetadata> metadataList; | 125 WTF::Vector<IntegrityMetadata> metadataList; |
118 IntegrityParseResult integrityParseResult = parseIntegrityAttribute(attribut e, metadataList, document); | 126 IntegrityParseResult integrityParseResult = parseIntegrityAttribute(integrit yMetadata, metadataList, document); |
119 // On failed parsing, there's no need to log an error here, as | 127 // On failed parsing, there's no need to log an error here, as |
120 // parseIntegrityAttribute() will output an appropriate console message. | 128 // parseIntegrityAttribute() will output an appropriate console message. |
121 if (integrityParseResult != IntegrityParseValidResult) | 129 if (integrityParseResult != IntegrityParseValidResult) |
122 return true; | 130 return true; |
123 | 131 |
124 StringUTF8Adaptor normalizedSource(source, StringUTF8Adaptor::Normalize, WTF ::EntitiesForUnencodables); | 132 StringUTF8Adaptor normalizedSource(source, StringUTF8Adaptor::Normalize, WTF ::EntitiesForUnencodables); |
125 | 133 |
126 if (!metadataList.size()) | 134 if (!metadataList.size()) |
127 return true; | 135 return true; |
128 | 136 |
(...skipping 22 matching lines...) Expand all Loading... | |
151 } | 159 } |
152 } | 160 } |
153 | 161 |
154 digest.clear(); | 162 digest.clear(); |
155 if (computeDigest(HashAlgorithmSha256, normalizedSource.data(), normalizedSo urce.length(), digest)) { | 163 if (computeDigest(HashAlgorithmSha256, normalizedSource.data(), normalizedSo urce.length(), digest)) { |
156 // This message exposes the digest of the resource to the console. | 164 // This message exposes the digest of the resource to the console. |
157 // Because this is only to the console, that's okay for now, but we | 165 // Because this is only to the console, that's okay for now, but we |
158 // need to be very careful not to expose this in exceptions or | 166 // need to be very careful not to expose this in exceptions or |
159 // JavaScript, otherwise it risks exposing information about the | 167 // JavaScript, otherwise it risks exposing information about the |
160 // resource cross-origin. | 168 // resource cross-origin. |
161 logErrorToConsole("Failed to find a valid digest in the 'integrity' attr ibute for resource '" + resourceUrl.elidedString() + "' with computed SHA-256 in tegrity '" + digestToString(digest) + "'. The resource has been blocked.", docum ent); | 169 errorMessage = "Failed to find a valid digest in the 'integrity' attribu te for resource '" + resourceUrl.elidedString() + "' with computed SHA-256 integ rity '" + digestToString(digest) + "'. The resource has been blocked."; |
162 } else { | 170 } else { |
163 logErrorToConsole("There was an error computing an integrity value for r esource '" + resourceUrl.elidedString() + "'. The resource has been blocked.", d ocument); | 171 errorMessage = "There was an error computing an integrity value for reso urce '" + resourceUrl.elidedString() + "'. The resource has been blocked."; |
164 } | 172 } |
165 UseCounter::count(document, UseCounter::SRIElementWithNonMatchingIntegrityAt tribute); | 173 UseCounter::count(document, UseCounter::SRIElementWithNonMatchingIntegrityAt tribute); |
166 return false; | 174 return false; |
167 } | 175 } |
168 | 176 |
169 // Before: | 177 // Before: |
170 // | 178 // |
171 // [algorithm]-[hash] | 179 // [algorithm]-[hash] |
172 // ^ ^ | 180 // ^ ^ |
173 // position end | 181 // position end |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 metadataList.append(integrityMetadata); | 325 metadataList.append(integrityMetadata); |
318 } | 326 } |
319 | 327 |
320 if (metadataList.size() == 0 && error) | 328 if (metadataList.size() == 0 && error) |
321 return IntegrityParseNoValidResult; | 329 return IntegrityParseNoValidResult; |
322 | 330 |
323 return IntegrityParseValidResult; | 331 return IntegrityParseValidResult; |
324 } | 332 } |
325 | 333 |
326 } // namespace blink | 334 } // namespace blink |
OLD | NEW |