PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/cordova-ios/tests/CordovaLibTests/CDVWhitelistTests.m

https://gitlab.com/blocknotary/IonicInterviews
Objective C | 332 lines | 231 code | 70 blank | 31 comment | 0 complexity | 2cfc11f743f8d9ab86ccd19bce087825 MD5 | raw file
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import <XCTest/XCTest.h>
  18. #import <Cordova/CDVWhitelist.h>
  19. #import "CDVIntentAndNavigationFilter.h"
  20. @interface CDVWhitelistTests : XCTestCase
  21. @end
  22. @implementation CDVWhitelistTests
  23. - (void)setUp
  24. {
  25. [super setUp];
  26. // setup code here
  27. }
  28. - (void)tearDown
  29. {
  30. // Tear-down code here.
  31. [super tearDown];
  32. }
  33. - (void)testAllowedSchemes
  34. {
  35. NSArray* allowedHosts = [NSArray arrayWithObjects:
  36. @"*.apache.org",
  37. nil];
  38. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  39. XCTAssertTrue([whitelist schemeIsAllowed:@"http"]);
  40. XCTAssertTrue([whitelist schemeIsAllowed:@"https"]);
  41. XCTAssertTrue([whitelist schemeIsAllowed:@"ftp"]);
  42. XCTAssertTrue([whitelist schemeIsAllowed:@"ftps"]);
  43. XCTAssertFalse([whitelist schemeIsAllowed:@"gopher"]);
  44. }
  45. - (void)testSubdomainWildcard
  46. {
  47. NSArray* allowedHosts = [NSArray arrayWithObjects:
  48. @"*.apache.org",
  49. nil];
  50. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  51. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://build.apache.org"]]);
  52. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://apache.org"]]);
  53. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://sub1.sub0.build.apache.org"]]);
  54. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://apache.org.ca"]]);
  55. }
  56. - (void)testCatchallWildcardOnly
  57. {
  58. NSArray* allowedHosts = [NSArray arrayWithObjects:
  59. @"*",
  60. nil];
  61. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  62. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://apache.org"]]);
  63. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"https://build.apache.prg"]]);
  64. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"ftp://MyDangerousSite.org"]]);
  65. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"ftps://apache.org.SuspiciousSite.com"]]);
  66. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"gopher://apache.org"]]);
  67. }
  68. - (void)testURISchemesNotFollowedByDoubleSlashes
  69. {
  70. NSArray* allowedHosts = [NSArray arrayWithObjects:
  71. @"tel:*",
  72. @"sms:*",
  73. nil];
  74. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  75. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"tel:1234567890"]]);
  76. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"sms:1234567890"]]);
  77. }
  78. - (void)testCatchallWildcardByProto
  79. {
  80. NSArray* allowedHosts = [NSArray arrayWithObjects:
  81. @"http://*",
  82. @"https://*",
  83. @"ftp://*",
  84. @"ftps://*",
  85. nil];
  86. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  87. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://apache.org"]]);
  88. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"https://build.apache.prg"]]);
  89. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"ftp://MyDangerousSite.org"]]);
  90. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"ftps://apache.org.SuspiciousSite.com"]]);
  91. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"gopher://apache.org"]]);
  92. }
  93. - (void)testExactMatch
  94. {
  95. NSArray* allowedHosts = [NSArray arrayWithObjects:
  96. @"www.apache.org",
  97. nil];
  98. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  99. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://www.apache.org"]]);
  100. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://build.apache.org"]]);
  101. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://apache.org"]]);
  102. }
  103. - (void)testNoMatchInQueryParam
  104. {
  105. NSArray* allowedHosts = [NSArray arrayWithObjects:
  106. @"www.apache.org",
  107. nil];
  108. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  109. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"www.malicious-site.org?url=http://www.apache.org"]]);
  110. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"www.malicious-site.org?url=www.apache.org"]]);
  111. }
  112. - (void)testIpExactMatch
  113. {
  114. NSArray* allowedHosts = [NSArray arrayWithObjects:
  115. @"192.168.1.1",
  116. @"192.168.2.1",
  117. nil];
  118. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  119. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://apache.org"]]);
  120. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://192.168.1.1"]]);
  121. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://192.168.2.1"]]);
  122. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://192.168.3.1"]]);
  123. }
  124. - (void)testIpWildcardMatch
  125. {
  126. NSArray* allowedHosts = [NSArray arrayWithObjects:
  127. @"192.168.1.*",
  128. @"192.168.2.*",
  129. nil];
  130. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  131. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://apache.org"]]);
  132. // Ever since Cordova 3.1, whitelist wildcards are simplified, only "*" and "*.apache.org" (subdomain example) are allowed. Therefore the next four tests should fail
  133. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://192.168.1.1"]]);
  134. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://192.168.1.2"]]);
  135. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://192.168.2.1"]]);
  136. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://192.168.2.2"]]);
  137. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://192.168.3.1"]]);
  138. }
  139. - (void)testHostnameExtraction
  140. {
  141. NSArray* allowedHosts = [NSArray arrayWithObjects:
  142. @"http://apache.org/",
  143. @"http://apache.org/foo/bar?x=y",
  144. @"ftp://apache.org/foo/bar?x=y",
  145. @"ftps://apache.org/foo/bar?x=y",
  146. @"http://apache.*/foo/bar?x=y",
  147. nil];
  148. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  149. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://apache.org/"]]);
  150. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://google.com/"]]);
  151. }
  152. - (void)testWhitelistRejectionString
  153. {
  154. NSArray* allowedHosts = [NSArray arrayWithObject:@"http://www.yahoo.com/"]; // Doesn't matter in this test.
  155. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  156. NSURL* testUrl = [NSURL URLWithString:@"http://www/google.com"];
  157. NSString* errorString = [whitelist errorStringForURL:testUrl];
  158. NSString* expectedErrorString = [NSString stringWithFormat:kCDVDefaultWhitelistRejectionString, [testUrl absoluteString]];
  159. XCTAssertTrue([expectedErrorString isEqualToString:errorString], @"Default error string has an unexpected value.");
  160. whitelist.whitelistRejectionFormatString = @"Hey, '%@' is, like, bogus man!";
  161. errorString = [whitelist errorStringForURL:testUrl];
  162. expectedErrorString = [NSString stringWithFormat:whitelist.whitelistRejectionFormatString, [testUrl absoluteString]];
  163. XCTAssertTrue([expectedErrorString isEqualToString:errorString], @"Customized whitelist rejection string has unexpected value.");
  164. }
  165. - (void)testSpecificProtocol
  166. {
  167. NSArray* allowedHosts = [NSArray arrayWithObjects:
  168. @"http://www.apache.org",
  169. @"cordova://www.google.com",
  170. nil];
  171. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  172. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://www.apache.org"]]);
  173. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"cordova://www.google.com"]]);
  174. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"cordova://www.apache.org"]]);
  175. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://www.google.com"]]);
  176. }
  177. - (void)testWildcardPlusOtherUrls
  178. {
  179. // test for https://issues.apache.org/jira/browse/CB-3394
  180. NSArray* allowedHosts = [NSArray arrayWithObjects:
  181. @"*",
  182. @"cordova.apache.org",
  183. nil];
  184. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  185. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://*.apache.org"]]);
  186. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"https://www.google.com"]]);
  187. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"ftp://cordova.apache.org"]]);
  188. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://cordova.apache.org"]]);
  189. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"https://cordova.apache.org"]]);
  190. }
  191. - (void)testWildcardScheme
  192. {
  193. NSArray* allowedHosts = [NSArray arrayWithObjects:
  194. @"*://*.test.com",
  195. nil];
  196. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  197. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"http://apache.org"]]);
  198. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"gopher://testtt.com"]]);
  199. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"gopher://test.com"]]);
  200. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://test.com"]]);
  201. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://my.test.com"]]);
  202. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"https://test.com"]]);
  203. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"https://my.test.com"]]);
  204. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://test.com/my/path"]]);
  205. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://my.test.com/my/path"]]);
  206. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"https://test.com/my/path"]]);
  207. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"https://my.test.com/my/path"]]);
  208. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"gopher://test.com#foo"]]);
  209. XCTAssertFalse([whitelist URLIsAllowed:[NSURL URLWithString:@"#foo"]]);
  210. }
  211. - (void)testCredentials
  212. {
  213. NSArray* allowedHosts = [NSArray arrayWithObjects:
  214. @"http://*.apache.org",
  215. @"http://www.google.com",
  216. nil];
  217. CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];
  218. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://user:pass@www.apache.org"]]);
  219. XCTAssertTrue([whitelist URLIsAllowed:[NSURL URLWithString:@"http://user:pass@www.google.com"]]);
  220. }
  221. - (void)testAllowIntentsAndNavigations
  222. {
  223. NSArray* allowIntents = @[ @"https://*" ];
  224. NSArray* allowNavigations = @[ @"https://*.apache.org" ];
  225. CDVWhitelist* intentsWhitelist = [[CDVWhitelist alloc] initWithArray:allowIntents];
  226. CDVWhitelist* navigationsWhitelist = [[CDVWhitelist alloc] initWithArray:allowNavigations];
  227. // Test allow-navigation superceding allow-intent
  228. XCTAssertEqual([CDVIntentAndNavigationFilter filterUrl:[NSURL URLWithString:@"https://apache.org/foo.html"] intentsWhitelist:intentsWhitelist navigationsWhitelist:navigationsWhitelist], CDVIntentAndNavigationFilterValueNavigationAllowed);
  229. // Test wildcard https as allow-intent
  230. XCTAssertEqual([CDVIntentAndNavigationFilter filterUrl:[NSURL URLWithString:@"https://google.com"] intentsWhitelist:intentsWhitelist navigationsWhitelist:navigationsWhitelist], CDVIntentAndNavigationFilterValueIntentAllowed);
  231. // Test http (not allowed in either)
  232. XCTAssertEqual([CDVIntentAndNavigationFilter filterUrl:[NSURL URLWithString:@"http://google.com"] intentsWhitelist:intentsWhitelist navigationsWhitelist:navigationsWhitelist], CDVIntentAndNavigationFilterValueNoneAllowed);
  233. NSURL* telUrl = [NSURL URLWithString:@"tel:5555555"];
  234. NSMutableURLRequest* telRequest = [NSMutableURLRequest requestWithURL:telUrl];
  235. telRequest.mainDocumentURL = telUrl;
  236. // mainDocumentURL and URL are the same in the NSURLRequest
  237. // Only UIWebViewNavigationTypeLinkClicked and UIWebViewNavigationTypeOther should return YES
  238. XCTAssertTrue([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeLinkClicked]);
  239. XCTAssertTrue([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeOther]);
  240. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeReload]);
  241. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeBackForward]);
  242. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeFormSubmitted]);
  243. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeFormResubmitted]);
  244. telRequest.mainDocumentURL = nil;
  245. // mainDocumentURL and URL are not the same in the NSURLRequest
  246. // Only UIWebViewNavigationTypeLinkClicked should return YES
  247. XCTAssertTrue([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeLinkClicked]);
  248. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeOther]);
  249. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeReload]);
  250. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeBackForward]);
  251. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeFormSubmitted]);
  252. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOpenURLRequest:telRequest navigationType:UIWebViewNavigationTypeFormResubmitted]);
  253. NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://apache.org"]];
  254. // Only CDVIntentAndNavigationFilterValueNavigationAllowed should return YES
  255. // navigationType doesn't matter
  256. XCTAssertTrue([CDVIntentAndNavigationFilter shouldOverrideLoadWithRequest:request navigationType:UIWebViewNavigationTypeOther filterValue:CDVIntentAndNavigationFilterValueNavigationAllowed]);
  257. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOverrideLoadWithRequest:request navigationType:UIWebViewNavigationTypeOther filterValue:CDVIntentAndNavigationFilterValueIntentAllowed]);
  258. XCTAssertFalse([CDVIntentAndNavigationFilter shouldOverrideLoadWithRequest:request navigationType:UIWebViewNavigationTypeOther filterValue:CDVIntentAndNavigationFilterValueNoneAllowed]);
  259. }
  260. @end