| 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 /** | 5 /** |
| 6 * @fileoverview Implements a check whether an app id lists an origin. | 6 * @fileoverview Implements a check whether an app id lists an origin. |
| 7 */ | 7 */ |
| 8 'use strict'; | 8 'use strict'; |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * Parses the text as JSON and returns it as an array of strings. | 11 * Parses the text as JSON and returns it as an array of strings. |
| 12 * @param {string} text Input JSON | 12 * @param {string} text Input JSON |
| 13 * @return {!Array<string>} Array of origins | 13 * @return {!Array<string>} Array of origins |
| 14 */ | 14 */ |
| 15 function getOriginsFromJson(text) { | 15 function getOriginsFromJson(text) { |
| 16 try { | 16 try { |
| 17 var urls, i; | 17 var urls, i; |
| 18 var appIdData = JSON.parse(text); | 18 var appIdData = JSON.parse(text); |
| 19 var trustedFacets = appIdData['trustedFacets']; | 19 var trustedFacets = appIdData['trustedFacets']; |
| 20 if (trustedFacets) { | 20 if (trustedFacets) { |
| 21 var versionBlock; | 21 var versionBlock; |
| 22 for (i = 0; versionBlock = trustedFacets[i]; i++) { | 22 for (i = 0; versionBlock = trustedFacets[i]; i++) { |
| 23 if (versionBlock['version'] && | 23 if (versionBlock['version'] && versionBlock['version']['major'] == 1 && |
| 24 versionBlock['version']['major'] == 1 && | |
| 25 versionBlock['version']['minor'] == 0) { | 24 versionBlock['version']['minor'] == 0) { |
| 26 urls = versionBlock['ids']; | 25 urls = versionBlock['ids']; |
| 27 break; | 26 break; |
| 28 } | 27 } |
| 29 } | 28 } |
| 30 } | 29 } |
| 31 if (typeof urls == 'undefined') { | 30 if (typeof urls == 'undefined') { |
| 32 throw Error('Could not find trustedFacets for version 1.0'); | 31 throw Error('Could not find trustedFacets for version 1.0'); |
| 33 } | 32 } |
| 34 var origins = {}; | 33 var origins = {}; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 72 |
| 74 /** | 73 /** |
| 75 * Checks whether the given origin is allowed by all of the given appIds. | 74 * Checks whether the given origin is allowed by all of the given appIds. |
| 76 * @param {!Countdown} timer A timer by which to resolve all provided app ids. | 75 * @param {!Countdown} timer A timer by which to resolve all provided app ids. |
| 77 * @param {string} origin The origin to check. | 76 * @param {string} origin The origin to check. |
| 78 * @param {!Array<string>} appIds The app ids to check. | 77 * @param {!Array<string>} appIds The app ids to check. |
| 79 * @param {boolean} allowHttp Whether to allow http:// URLs. | 78 * @param {boolean} allowHttp Whether to allow http:// URLs. |
| 80 * @param {string=} opt_logMsgUrl A log message URL. | 79 * @param {string=} opt_logMsgUrl A log message URL. |
| 81 * @return {Promise<boolean>} A promise for the result of the check | 80 * @return {Promise<boolean>} A promise for the result of the check |
| 82 */ | 81 */ |
| 83 AppIdChecker.prototype.checkAppIds = | 82 AppIdChecker.prototype.checkAppIds = function( |
| 84 function(timer, origin, appIds, allowHttp, opt_logMsgUrl) {}; | 83 timer, origin, appIds, allowHttp, opt_logMsgUrl) {}; |
| 85 | 84 |
| 86 /** | 85 /** |
| 87 * An interface to create an AppIdChecker. | 86 * An interface to create an AppIdChecker. |
| 88 * @interface | 87 * @interface |
| 89 */ | 88 */ |
| 90 function AppIdCheckerFactory() {} | 89 function AppIdCheckerFactory() {} |
| 91 | 90 |
| 92 /** | 91 /** |
| 93 * @return {!AppIdChecker} A new AppIdChecker. | 92 * @return {!AppIdChecker} A new AppIdChecker. |
| 94 */ | 93 */ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 107 | 106 |
| 108 /** | 107 /** |
| 109 * Checks whether all the app ids provided can be asserted by the given origin. | 108 * Checks whether all the app ids provided can be asserted by the given origin. |
| 110 * @param {!Countdown} timer A timer by which to resolve all provided app ids. | 109 * @param {!Countdown} timer A timer by which to resolve all provided app ids. |
| 111 * @param {string} origin The origin to check. | 110 * @param {string} origin The origin to check. |
| 112 * @param {!Array<string>} appIds The app ids to check. | 111 * @param {!Array<string>} appIds The app ids to check. |
| 113 * @param {boolean} allowHttp Whether to allow http:// URLs. | 112 * @param {boolean} allowHttp Whether to allow http:// URLs. |
| 114 * @param {string=} opt_logMsgUrl A log message URL. | 113 * @param {string=} opt_logMsgUrl A log message URL. |
| 115 * @return {Promise<boolean>} A promise for the result of the check | 114 * @return {Promise<boolean>} A promise for the result of the check |
| 116 */ | 115 */ |
| 117 XhrAppIdChecker.prototype.checkAppIds = | 116 XhrAppIdChecker.prototype.checkAppIds = function( |
| 118 function(timer, origin, appIds, allowHttp, opt_logMsgUrl) { | 117 timer, origin, appIds, allowHttp, opt_logMsgUrl) { |
| 119 if (this.timer_) { | 118 if (this.timer_) { |
| 120 // Can't use the same object to check appIds more than once. | 119 // Can't use the same object to check appIds more than once. |
| 121 return Promise.resolve(false); | 120 return Promise.resolve(false); |
| 122 } | 121 } |
| 123 | 122 |
| 124 /** @private {!Countdown} */ | 123 /** @private {!Countdown} */ |
| 125 this.timer_ = timer; | 124 this.timer_ = timer; |
| 126 /** @private {string} */ | 125 /** @private {string} */ |
| 127 this.origin_ = origin; | 126 this.origin_ = origin; |
| 128 var appIdsMap = {}; | 127 var appIdsMap = {}; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 */ | 162 */ |
| 164 XhrAppIdChecker.prototype.checkAppId_ = function(appId) { | 163 XhrAppIdChecker.prototype.checkAppId_ = function(appId) { |
| 165 if (appId == this.origin_) { | 164 if (appId == this.origin_) { |
| 166 // Trivially allowed | 165 // Trivially allowed |
| 167 return Promise.resolve(true); | 166 return Promise.resolve(true); |
| 168 } | 167 } |
| 169 var p = this.fetchAllowedOriginsForAppId_(appId); | 168 var p = this.fetchAllowedOriginsForAppId_(appId); |
| 170 var self = this; | 169 var self = this; |
| 171 return p.then(function(allowedOrigins) { | 170 return p.then(function(allowedOrigins) { |
| 172 if (allowedOrigins.indexOf(self.origin_) == -1) { | 171 if (allowedOrigins.indexOf(self.origin_) == -1) { |
| 173 console.warn(UTIL_fmt('Origin ' + self.origin_ + | 172 console.warn(UTIL_fmt( |
| 174 ' not allowed by app id ' + appId)); | 173 'Origin ' + self.origin_ + ' not allowed by app id ' + appId)); |
| 175 return false; | 174 return false; |
| 176 } | 175 } |
| 177 return true; | 176 return true; |
| 178 }); | 177 }); |
| 179 }; | 178 }; |
| 180 | 179 |
| 181 /** | 180 /** |
| 182 * @return {boolean} Whether all the app ids being checked are equal to the | 181 * @return {boolean} Whether all the app ids being checked are equal to the |
| 183 * calling origin. | 182 * calling origin. |
| 184 * @private | 183 * @private |
| (...skipping 23 matching lines...) Expand all Loading... |
| 208 } | 207 } |
| 209 | 208 |
| 210 var origin = getOriginFromUrl(appId); | 209 var origin = getOriginFromUrl(appId); |
| 211 if (!origin) { | 210 if (!origin) { |
| 212 return Promise.resolve([]); | 211 return Promise.resolve([]); |
| 213 } | 212 } |
| 214 | 213 |
| 215 var p = this.fetcher_.fetch(appId); | 214 var p = this.fetcher_.fetch(appId); |
| 216 var self = this; | 215 var self = this; |
| 217 return p.then(getOriginsFromJson, function(rc_) { | 216 return p.then(getOriginsFromJson, function(rc_) { |
| 218 var rc = /** @type {number} */(rc_); | 217 var rc = /** @type {number} */ (rc_); |
| 219 console.log(UTIL_fmt('fetching ' + appId + ' failed: ' + rc)); | 218 console.log(UTIL_fmt('fetching ' + appId + ' failed: ' + rc)); |
| 220 if (!(rc >= 400 && rc < 500) && !self.timer_.expired()) { | 219 if (!(rc >= 400 && rc < 500) && !self.timer_.expired()) { |
| 221 // Retry | 220 // Retry |
| 222 return self.fetchAllowedOriginsForAppId_(appId); | 221 return self.fetchAllowedOriginsForAppId_(appId); |
| 223 } | 222 } |
| 224 return []; | 223 return []; |
| 225 }); | 224 }); |
| 226 }; | 225 }; |
| 227 | 226 |
| 228 /** | 227 /** |
| 229 * A factory to create an XhrAppIdChecker. | 228 * A factory to create an XhrAppIdChecker. |
| 230 * @implements AppIdCheckerFactory | 229 * @implements AppIdCheckerFactory |
| 231 * @param {!TextFetcher} fetcher | 230 * @param {!TextFetcher} fetcher |
| 232 * @constructor | 231 * @constructor |
| 233 */ | 232 */ |
| 234 function XhrAppIdCheckerFactory(fetcher) { | 233 function XhrAppIdCheckerFactory(fetcher) { |
| 235 /** @private {!TextFetcher} */ | 234 /** @private {!TextFetcher} */ |
| 236 this.fetcher_ = fetcher; | 235 this.fetcher_ = fetcher; |
| 237 } | 236 } |
| 238 | 237 |
| 239 /** | 238 /** |
| 240 * @return {!AppIdChecker} A new AppIdChecker. | 239 * @return {!AppIdChecker} A new AppIdChecker. |
| 241 */ | 240 */ |
| 242 XhrAppIdCheckerFactory.prototype.create = function() { | 241 XhrAppIdCheckerFactory.prototype.create = function() { |
| 243 return new XhrAppIdChecker(this.fetcher_); | 242 return new XhrAppIdChecker(this.fetcher_); |
| 244 }; | 243 }; |
| OLD | NEW |