Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: Source/core/frame/SubresourceIntegrityTest.cpp

Issue 566083003: Implementation of subresource integrity attribute for secure origins. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fixed broken build Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/frame/SubresourceIntegrity.cpp ('k') | Source/core/frame/UseCounter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/frame/SubresourceIntegrity.h"
7
8 #include "core/HTMLNames.h"
9 #include "core/dom/Document.h"
10 #include "core/html/HTMLScriptElement.h"
11 #include "platform/Crypto.h"
12 #include "platform/weborigin/KURL.h"
13 #include "platform/weborigin/SecurityOrigin.h"
14 #include "wtf/RefPtr.h"
15 #include "wtf/text/WTFString.h"
16 #include <gtest/gtest.h>
17
18 namespace blink {
19
20 static const char kBasicScript[] = "alert('test');";
21 static const char kSha256Integrity[] = "ni://sha256;GAF48QOoxRvu0gZAmQivUdJPyBac qznBAXwnkfpmQX4=";
22 static const char kSha384Intgrity[] = "ni://sha384;nep3XpvhUxpCMOVXIFPecThAqdY/u VeiD4kXSqXpx0YJUWU4fTTaFgciTuZk7fmE";
23 static const char kSha512Integrity[] = "ni://sha512;TXkJw18PqlVlEUXXjeXbGetop1TK B3wYQIp1/ihxCOFGUfG9TYOaA1MlkpTAqSV6yaevLO8Tj5pgH1JmZ++ItA==";
24 static const char kSha384IntegrityLabeledAs256[] = "ni://sha256;nep3XpvhUxpCMOVX IFPecThAqdY/uVeiD4kXSqXpx0YJUWU4fTTaFgciTuZk7fmE";
25 static const char kUnsupportedHashFunctionIntegrity[] = "ni://sha1;JfLW308qMPKfb 4DaHpUBEESwuPc=";
26
27 TEST(SubresourceIntegrityTest, CheckSubresourceIntegrity)
28 {
29 KURL secureKURL(KURL(), "https://foobar.com:443");
30 KURL insecureKURL(KURL(), "http://foobar.com:80");
31 RefPtr<SecurityOrigin> secureOrigin = SecurityOrigin::create(secureKURL);
32 RefPtr<SecurityOrigin> insecureOrigin = SecurityOrigin::create(insecureKURL) ;
33 RefPtrWillBeRawPtr<Document> document = Document::create();
34 RefPtrWillBeRawPtr<HTMLScriptElement> scriptElement = HTMLScriptElement::cre ate(*document, true);
35
36 // Verify basic sha256, sha384, and sha512 integrity checks.
37 document->updateSecurityOrigin(secureOrigin->isolatedCopy());
38 scriptElement->setAttribute(HTMLNames::integrityAttr, kSha256Integrity);
39 EXPECT_TRUE(SubresourceIntegrity::CheckSubresourceIntegrity(*scriptElement, kBasicScript, secureKURL));
40 scriptElement->setAttribute(HTMLNames::integrityAttr, kSha384Intgrity);
41 EXPECT_TRUE(SubresourceIntegrity::CheckSubresourceIntegrity(*scriptElement, kBasicScript, secureKURL));
42 scriptElement->setAttribute(HTMLNames::integrityAttr, kSha512Integrity);
43 EXPECT_TRUE(SubresourceIntegrity::CheckSubresourceIntegrity(*scriptElement, kBasicScript, secureKURL));
44
45 // The hash label must match the hash value.
46 scriptElement->setAttribute(HTMLNames::integrityAttr, kSha384IntegrityLabele dAs256);
47 EXPECT_FALSE(SubresourceIntegrity::CheckSubresourceIntegrity(*scriptElement, kBasicScript, secureKURL));
48
49 scriptElement->setAttribute(HTMLNames::integrityAttr, kSha256Integrity);
50 // Check should fail if the document is not on an authenticated origin or
51 // if the resource is not on an authenticated origin.
52 document->updateSecurityOrigin(insecureOrigin->isolatedCopy());
53 EXPECT_FALSE(SubresourceIntegrity::CheckSubresourceIntegrity(*scriptElement, kBasicScript, secureKURL));
54 document->updateSecurityOrigin(secureOrigin->isolatedCopy());
55 EXPECT_FALSE(SubresourceIntegrity::CheckSubresourceIntegrity(*scriptElement, kBasicScript, insecureKURL));
56
57 scriptElement->setAttribute(HTMLNames::integrityAttr, kUnsupportedHashFuncti onIntegrity);
58 EXPECT_FALSE(SubresourceIntegrity::CheckSubresourceIntegrity(*scriptElement, kBasicScript, insecureKURL));
59 }
60
61 TEST(SubresourceIntegrityTest, Parsing)
62 {
63 String attribute;
64 String integrity;
65 HashAlgorithm algorithm;
66
67 // Verify that empty attribute is not valid.
68 EXPECT_FALSE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integr ity, algorithm));
69
70 // Valid sha256 attribute
71 attribute = "ni://sha256;BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=";
72 EXPECT_TRUE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integri ty, algorithm));
73 EXPECT_EQ(integrity, "BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=");
74 EXPECT_EQ(algorithm, HashAlgorithmSha256);
75
76 // Another valid sha256 attribute, but this time with whitespace
77 attribute = " ni://sha256;BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE= ";
78 EXPECT_TRUE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integri ty, algorithm));
79 EXPECT_EQ(integrity, "BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=");
80 EXPECT_EQ(algorithm, HashAlgorithmSha256);
81
82 // Valid sha384 attribute
83 attribute = "ni://sha384;XVVXBGoYw6AJOh9J/Z8pBDMVVPfkBpngexkA7JqZu8d5GENND6T EIup/tA1v5GPr";
84 EXPECT_TRUE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integri ty, algorithm));
85 EXPECT_EQ(integrity, "XVVXBGoYw6AJOh9J/Z8pBDMVVPfkBpngexkA7JqZu8d5GENND6TEIu p/tA1v5GPr");
86 EXPECT_EQ(algorithm, HashAlgorithmSha384);
87
88 // Valid sha512 attribute
89 attribute = "ni://sha512;tbUPioKbVBplr0b1ucnWB57SJWt4x9dOE0Vy2mzCXvH3FepqDZ+ 07yMK81ytlg0MPaIrPAjcHqba5csorDWtKg==";
90 EXPECT_TRUE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integri ty, algorithm));
91 EXPECT_EQ(integrity, "tbUPioKbVBplr0b1ucnWB57SJWt4x9dOE0Vy2mzCXvH3FepqDZ+07y MK81ytlg0MPaIrPAjcHqba5csorDWtKg==");
92 EXPECT_EQ(algorithm, HashAlgorithmSha512);
93
94 // Invalid prefix
95 attribute = "foobar://sha256;BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=";
96 EXPECT_FALSE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integr ity, algorithm));
97
98 // Invalid hash function
99 attribute = "ni://not_a_hash_function;BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h 2nFSE=";
100 EXPECT_FALSE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integr ity, algorithm));
101
102 // Invalid integrity (not base64)
103 attribute = "ni://sha256;&&&foobar&&&";
104 EXPECT_FALSE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integr ity, algorithm));
105 attribute = "ni://sha256;\x01\x02\x03\x04";
106 EXPECT_FALSE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integr ity, algorithm));
107
108 // Just integrity
109 attribute = "BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=";
110 EXPECT_FALSE(SubresourceIntegrity::parseIntegrityAttribute(attribute, integr ity, algorithm));
111 }
112
113 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/frame/SubresourceIntegrity.cpp ('k') | Source/core/frame/UseCounter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698