1
0

RequestTest.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. <?php
  2. /**
  3. * @copyright 2013 Thomas Tanghus (thomas@tanghus.net)
  4. * @copyright 2016 Lukas Reschke lukas@owncloud.com
  5. * @copyright 2022 Stanimir Bozhilov (stanimir@audriga.com)
  6. *
  7. * This file is licensed under the Affero General Public License version 3 or
  8. * later.
  9. * See the COPYING-README file.
  10. */
  11. namespace Test\AppFramework\Http;
  12. use OC\AppFramework\Http\Request;
  13. use OC\Security\CSRF\CsrfToken;
  14. use OC\Security\CSRF\CsrfTokenManager;
  15. use OCP\IConfig;
  16. use OCP\IRequestId;
  17. /**
  18. * Class RequestTest
  19. *
  20. * @package OC\AppFramework\Http
  21. */
  22. class RequestTest extends \Test\TestCase {
  23. /** @var string */
  24. protected $stream = 'fakeinput://data';
  25. /** @var IRequestId */
  26. protected $requestId;
  27. /** @var IConfig */
  28. protected $config;
  29. /** @var CsrfTokenManager */
  30. protected $csrfTokenManager;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. if (in_array('fakeinput', stream_get_wrappers())) {
  34. stream_wrapper_unregister('fakeinput');
  35. }
  36. stream_wrapper_register('fakeinput', 'Test\AppFramework\Http\RequestStream');
  37. $this->requestId = $this->createMock(IRequestId::class);
  38. $this->config = $this->createMock(IConfig::class);
  39. $this->csrfTokenManager = $this->getMockBuilder(CsrfTokenManager::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. }
  43. protected function tearDown(): void {
  44. stream_wrapper_unregister('fakeinput');
  45. parent::tearDown();
  46. }
  47. public function testRequestAccessors() {
  48. $vars = [
  49. 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
  50. 'method' => 'GET',
  51. ];
  52. $request = new Request(
  53. $vars,
  54. $this->requestId,
  55. $this->config,
  56. $this->csrfTokenManager,
  57. $this->stream
  58. );
  59. // Countable
  60. $this->assertSame(2, count($request));
  61. // Array access
  62. $this->assertSame('Joey', $request['nickname']);
  63. // "Magic" accessors
  64. $this->assertSame('Joey', $request->{'nickname'});
  65. $this->assertTrue(isset($request['nickname']));
  66. $this->assertTrue(isset($request->{'nickname'}));
  67. $this->assertFalse(isset($request->{'flickname'}));
  68. // Only testing 'get', but same approach for post, files etc.
  69. $this->assertSame('Joey', $request->get['nickname']);
  70. // Always returns null if variable not set.
  71. $this->assertSame(null, $request->{'flickname'});
  72. }
  73. // urlParams has precedence over POST which has precedence over GET
  74. public function testPrecedence() {
  75. $vars = [
  76. 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
  77. 'post' => ['name' => 'Jane Doe', 'nickname' => 'Janey'],
  78. 'urlParams' => ['user' => 'jw', 'name' => 'Johnny Weissmüller'],
  79. 'method' => 'GET'
  80. ];
  81. $request = new Request(
  82. $vars,
  83. $this->requestId,
  84. $this->config,
  85. $this->csrfTokenManager,
  86. $this->stream
  87. );
  88. $this->assertSame(3, count($request));
  89. $this->assertSame('Janey', $request->{'nickname'});
  90. $this->assertSame('Johnny Weissmüller', $request->{'name'});
  91. }
  92. public function testImmutableArrayAccess() {
  93. $this->expectException(\RuntimeException::class);
  94. $vars = [
  95. 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
  96. 'method' => 'GET'
  97. ];
  98. $request = new Request(
  99. $vars,
  100. $this->requestId,
  101. $this->config,
  102. $this->csrfTokenManager,
  103. $this->stream
  104. );
  105. $request['nickname'] = 'Janey';
  106. }
  107. public function testImmutableMagicAccess() {
  108. $this->expectException(\RuntimeException::class);
  109. $vars = [
  110. 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
  111. 'method' => 'GET'
  112. ];
  113. $request = new Request(
  114. $vars,
  115. $this->requestId,
  116. $this->config,
  117. $this->csrfTokenManager,
  118. $this->stream
  119. );
  120. $request->{'nickname'} = 'Janey';
  121. }
  122. public function testGetTheMethodRight() {
  123. $this->expectException(\LogicException::class);
  124. $vars = [
  125. 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
  126. 'method' => 'GET',
  127. ];
  128. $request = new Request(
  129. $vars,
  130. $this->requestId,
  131. $this->config,
  132. $this->csrfTokenManager,
  133. $this->stream
  134. );
  135. $request->post;
  136. }
  137. public function testTheMethodIsRight() {
  138. $vars = [
  139. 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
  140. 'method' => 'GET',
  141. ];
  142. $request = new Request(
  143. $vars,
  144. $this->requestId,
  145. $this->config,
  146. $this->csrfTokenManager,
  147. $this->stream
  148. );
  149. $this->assertSame('GET', $request->method);
  150. $result = $request->get;
  151. $this->assertSame('John Q. Public', $result['name']);
  152. $this->assertSame('Joey', $result['nickname']);
  153. }
  154. public function testJsonPost() {
  155. global $data;
  156. $data = '{"name": "John Q. Public", "nickname": "Joey"}';
  157. $vars = [
  158. 'method' => 'POST',
  159. 'server' => ['CONTENT_TYPE' => 'application/json; utf-8']
  160. ];
  161. $request = new Request(
  162. $vars,
  163. $this->requestId,
  164. $this->config,
  165. $this->csrfTokenManager,
  166. $this->stream
  167. );
  168. $this->assertSame('POST', $request->method);
  169. $result = $request->post;
  170. $this->assertSame('John Q. Public', $result['name']);
  171. $this->assertSame('Joey', $result['nickname']);
  172. $this->assertSame('Joey', $request->params['nickname']);
  173. $this->assertSame('Joey', $request['nickname']);
  174. }
  175. public function testScimJsonPost() {
  176. global $data;
  177. $data = '{"userName":"testusername", "displayName":"Example User"}';
  178. $vars = [
  179. 'method' => 'POST',
  180. 'server' => ['CONTENT_TYPE' => 'application/scim+json; utf-8']
  181. ];
  182. $request = new Request(
  183. $vars,
  184. $this->requestId,
  185. $this->config,
  186. $this->csrfTokenManager,
  187. $this->stream
  188. );
  189. $this->assertSame('POST', $request->method);
  190. $result = $request->post;
  191. $this->assertSame('testusername', $result['userName']);
  192. $this->assertSame('Example User', $result['displayName']);
  193. $this->assertSame('Example User', $request->params['displayName']);
  194. $this->assertSame('Example User', $request['displayName']);
  195. }
  196. public function testCustomJsonPost() {
  197. global $data;
  198. $data = '{"propertyA":"sometestvalue", "propertyB":"someothertestvalue"}';
  199. // Note: the content type used here is fictional and intended to check if the regex for JSON content types works fine
  200. $vars = [
  201. 'method' => 'POST',
  202. 'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8']
  203. ];
  204. $request = new Request(
  205. $vars,
  206. $this->requestId,
  207. $this->config,
  208. $this->csrfTokenManager,
  209. $this->stream
  210. );
  211. $this->assertSame('POST', $request->method);
  212. $result = $request->post;
  213. $this->assertSame('sometestvalue', $result['propertyA']);
  214. $this->assertSame('someothertestvalue', $result['propertyB']);
  215. }
  216. public function notJsonDataProvider() {
  217. return [
  218. ['this is not valid json'],
  219. ['"just a string"'],
  220. ['{"just a string"}'],
  221. ];
  222. }
  223. /**
  224. * @dataProvider notJsonDataProvider
  225. */
  226. public function testNotJsonPost($testData) {
  227. global $data;
  228. $data = $testData;
  229. $vars = [
  230. 'method' => 'POST',
  231. 'server' => ['CONTENT_TYPE' => 'application/json; utf-8']
  232. ];
  233. $request = new Request(
  234. $vars,
  235. $this->requestId,
  236. $this->config,
  237. $this->csrfTokenManager,
  238. $this->stream
  239. );
  240. $this->assertEquals('POST', $request->method);
  241. $result = $request->post;
  242. // ensure there's no error attempting to decode the content
  243. }
  244. public function testNotScimJsonPost() {
  245. global $data;
  246. $data = 'this is not valid scim json';
  247. $vars = [
  248. 'method' => 'POST',
  249. 'server' => ['CONTENT_TYPE' => 'application/scim+json; utf-8']
  250. ];
  251. $request = new Request(
  252. $vars,
  253. $this->requestId,
  254. $this->config,
  255. $this->csrfTokenManager,
  256. $this->stream
  257. );
  258. $this->assertEquals('POST', $request->method);
  259. $result = $request->post;
  260. // ensure there's no error attempting to decode the content
  261. }
  262. public function testNotCustomJsonPost() {
  263. global $data;
  264. $data = 'this is not valid json';
  265. $vars = [
  266. 'method' => 'POST',
  267. 'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8']
  268. ];
  269. $request = new Request(
  270. $vars,
  271. $this->requestId,
  272. $this->config,
  273. $this->csrfTokenManager,
  274. $this->stream
  275. );
  276. $this->assertEquals('POST', $request->method);
  277. $result = $request->post;
  278. // ensure there's no error attempting to decode the content
  279. }
  280. public function testPatch() {
  281. global $data;
  282. $data = http_build_query(['name' => 'John Q. Public', 'nickname' => 'Joey'], '', '&');
  283. $vars = [
  284. 'method' => 'PATCH',
  285. 'server' => ['CONTENT_TYPE' => 'application/x-www-form-urlencoded'],
  286. ];
  287. $request = new Request(
  288. $vars,
  289. $this->requestId,
  290. $this->config,
  291. $this->csrfTokenManager,
  292. $this->stream
  293. );
  294. $this->assertSame('PATCH', $request->method);
  295. $result = $request->patch;
  296. $this->assertSame('John Q. Public', $result['name']);
  297. $this->assertSame('Joey', $result['nickname']);
  298. }
  299. public function testJsonPatchAndPut() {
  300. global $data;
  301. // PUT content
  302. $data = '{"name": "John Q. Public", "nickname": "Joey"}';
  303. $vars = [
  304. 'method' => 'PUT',
  305. 'server' => ['CONTENT_TYPE' => 'application/json; utf-8'],
  306. ];
  307. $request = new Request(
  308. $vars,
  309. $this->requestId,
  310. $this->config,
  311. $this->csrfTokenManager,
  312. $this->stream
  313. );
  314. $this->assertSame('PUT', $request->method);
  315. $result = $request->put;
  316. $this->assertSame('John Q. Public', $result['name']);
  317. $this->assertSame('Joey', $result['nickname']);
  318. // PATCH content
  319. $data = '{"name": "John Q. Public", "nickname": null}';
  320. $vars = [
  321. 'method' => 'PATCH',
  322. 'server' => ['CONTENT_TYPE' => 'application/json; utf-8'],
  323. ];
  324. $request = new Request(
  325. $vars,
  326. $this->requestId,
  327. $this->config,
  328. $this->csrfTokenManager,
  329. $this->stream
  330. );
  331. $this->assertSame('PATCH', $request->method);
  332. $result = $request->patch;
  333. $this->assertSame('John Q. Public', $result['name']);
  334. $this->assertSame(null, $result['nickname']);
  335. }
  336. public function testScimJsonPatchAndPut() {
  337. global $data;
  338. // PUT content
  339. $data = '{"userName": "sometestusername", "displayName": "Example User"}';
  340. $vars = [
  341. 'method' => 'PUT',
  342. 'server' => ['CONTENT_TYPE' => 'application/scim+json; utf-8'],
  343. ];
  344. $request = new Request(
  345. $vars,
  346. $this->requestId,
  347. $this->config,
  348. $this->csrfTokenManager,
  349. $this->stream
  350. );
  351. $this->assertSame('PUT', $request->method);
  352. $result = $request->put;
  353. $this->assertSame('sometestusername', $result['userName']);
  354. $this->assertSame('Example User', $result['displayName']);
  355. // PATCH content
  356. $data = '{"userName": "sometestusername", "displayName": null}';
  357. $vars = [
  358. 'method' => 'PATCH',
  359. 'server' => ['CONTENT_TYPE' => 'application/scim+json; utf-8'],
  360. ];
  361. $request = new Request(
  362. $vars,
  363. $this->requestId,
  364. $this->config,
  365. $this->csrfTokenManager,
  366. $this->stream
  367. );
  368. $this->assertSame('PATCH', $request->method);
  369. $result = $request->patch;
  370. $this->assertSame('sometestusername', $result['userName']);
  371. $this->assertSame(null, $result['displayName']);
  372. }
  373. public function testCustomJsonPatchAndPut() {
  374. global $data;
  375. // PUT content
  376. $data = '{"propertyA": "sometestvalue", "propertyB": "someothertestvalue"}';
  377. $vars = [
  378. 'method' => 'PUT',
  379. 'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8'],
  380. ];
  381. $request = new Request(
  382. $vars,
  383. $this->requestId,
  384. $this->config,
  385. $this->csrfTokenManager,
  386. $this->stream
  387. );
  388. $this->assertSame('PUT', $request->method);
  389. $result = $request->put;
  390. $this->assertSame('sometestvalue', $result['propertyA']);
  391. $this->assertSame('someothertestvalue', $result['propertyB']);
  392. // PATCH content
  393. $data = '{"propertyA": "sometestvalue", "propertyB": null}';
  394. $vars = [
  395. 'method' => 'PATCH',
  396. 'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8'],
  397. ];
  398. $request = new Request(
  399. $vars,
  400. $this->requestId,
  401. $this->config,
  402. $this->csrfTokenManager,
  403. $this->stream
  404. );
  405. $this->assertSame('PATCH', $request->method);
  406. $result = $request->patch;
  407. $this->assertSame('sometestvalue', $result['propertyA']);
  408. $this->assertSame(null, $result['propertyB']);
  409. }
  410. public function testPutStream() {
  411. global $data;
  412. $data = file_get_contents(__DIR__ . '/../../../data/testimage.png');
  413. $vars = [
  414. 'put' => $data,
  415. 'method' => 'PUT',
  416. 'server' => [
  417. 'CONTENT_TYPE' => 'image/png',
  418. 'CONTENT_LENGTH' => (string)strlen($data)
  419. ],
  420. ];
  421. $request = new Request(
  422. $vars,
  423. $this->requestId,
  424. $this->config,
  425. $this->csrfTokenManager,
  426. $this->stream
  427. );
  428. $this->assertSame('PUT', $request->method);
  429. $resource = $request->put;
  430. $contents = stream_get_contents($resource);
  431. $this->assertSame($data, $contents);
  432. try {
  433. $resource = $request->put;
  434. } catch (\LogicException $e) {
  435. return;
  436. }
  437. $this->fail('Expected LogicException.');
  438. }
  439. public function testSetUrlParameters() {
  440. $vars = [
  441. 'post' => [],
  442. 'method' => 'POST',
  443. 'urlParams' => ['id' => '2'],
  444. ];
  445. $request = new Request(
  446. $vars,
  447. $this->requestId,
  448. $this->config,
  449. $this->csrfTokenManager,
  450. $this->stream
  451. );
  452. $newParams = ['id' => '3', 'test' => 'test2'];
  453. $request->setUrlParameters($newParams);
  454. $this->assertSame('test2', $request->getParam('test'));
  455. $this->assertEquals('3', $request->getParam('id'));
  456. $this->assertEquals('3', $request->getParams()['id']);
  457. }
  458. public function testGetRemoteAddressWithoutTrustedRemote() {
  459. $this->config
  460. ->expects($this->once())
  461. ->method('getSystemValue')
  462. ->with('trusted_proxies')
  463. ->willReturn([]);
  464. $request = new Request(
  465. [
  466. 'server' => [
  467. 'REMOTE_ADDR' => '10.0.0.2',
  468. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  469. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  470. ],
  471. ],
  472. $this->requestId,
  473. $this->config,
  474. $this->csrfTokenManager,
  475. $this->stream
  476. );
  477. $this->assertSame('10.0.0.2', $request->getRemoteAddress());
  478. }
  479. public function testGetRemoteAddressWithNoTrustedHeader() {
  480. $this->config
  481. ->expects($this->exactly(2))
  482. ->method('getSystemValue')
  483. ->withConsecutive(
  484. ['trusted_proxies'],
  485. ['forwarded_for_headers'],
  486. )->willReturnOnConsecutiveCalls(
  487. ['10.0.0.2'],
  488. []
  489. );
  490. $request = new Request(
  491. [
  492. 'server' => [
  493. 'REMOTE_ADDR' => '10.0.0.2',
  494. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  495. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  496. ],
  497. ],
  498. $this->requestId,
  499. $this->config,
  500. $this->csrfTokenManager,
  501. $this->stream
  502. );
  503. $this->assertSame('10.0.0.2', $request->getRemoteAddress());
  504. }
  505. public function testGetRemoteAddressWithSingleTrustedRemote() {
  506. $this->config
  507. ->expects($this->exactly(2))
  508. ->method('getSystemValue')
  509. ->withConsecutive(
  510. ['trusted_proxies'],
  511. ['forwarded_for_headers'],
  512. )-> willReturnOnConsecutiveCalls(
  513. ['10.0.0.2'],
  514. ['HTTP_X_FORWARDED'],
  515. );
  516. $request = new Request(
  517. [
  518. 'server' => [
  519. 'REMOTE_ADDR' => '10.0.0.2',
  520. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  521. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  522. ],
  523. ],
  524. $this->requestId,
  525. $this->config,
  526. $this->csrfTokenManager,
  527. $this->stream
  528. );
  529. $this->assertSame('10.4.0.4', $request->getRemoteAddress());
  530. }
  531. public function testGetRemoteAddressWithMultipleTrustedRemotes() {
  532. $this->config
  533. ->expects($this->exactly(2))
  534. ->method('getSystemValue')
  535. ->willReturnMap([
  536. ['trusted_proxies', [], ['10.0.0.2', '::1']],
  537. ['forwarded_for_headers', ['HTTP_X_FORWARDED_FOR'], ['HTTP_X_FORWARDED']],
  538. ]);
  539. $request = new Request(
  540. [
  541. 'server' => [
  542. 'REMOTE_ADDR' => '10.0.0.2',
  543. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4, ::1',
  544. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  545. ],
  546. ],
  547. $this->requestId,
  548. $this->config,
  549. $this->csrfTokenManager,
  550. $this->stream
  551. );
  552. $this->assertSame('10.4.0.4', $request->getRemoteAddress());
  553. }
  554. public function testGetRemoteAddressIPv6WithSingleTrustedRemote() {
  555. $this->config
  556. ->expects($this->exactly(2))
  557. ->method('getSystemValue')
  558. ->withConsecutive(
  559. ['trusted_proxies'],
  560. ['forwarded_for_headers'],
  561. )-> willReturnOnConsecutiveCalls(
  562. ['2001:db8:85a3:8d3:1319:8a2e:370:7348'],
  563. ['HTTP_X_FORWARDED'],
  564. );
  565. $request = new Request(
  566. [
  567. 'server' => [
  568. 'REMOTE_ADDR' => '2001:db8:85a3:8d3:1319:8a2e:370:7348',
  569. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  570. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  571. ],
  572. ],
  573. $this->requestId,
  574. $this->config,
  575. $this->csrfTokenManager,
  576. $this->stream
  577. );
  578. $this->assertSame('10.4.0.4', $request->getRemoteAddress());
  579. }
  580. public function testGetRemoteAddressVerifyPriorityHeader() {
  581. $this->config
  582. ->expects($this->exactly(2))
  583. ->method('getSystemValue')
  584. ->withConsecutive(
  585. ['trusted_proxies'],
  586. ['forwarded_for_headers'],
  587. )-> willReturnOnConsecutiveCalls(
  588. ['10.0.0.2'],
  589. [
  590. 'HTTP_X_FORWARDED',
  591. 'HTTP_X_FORWARDED_FOR',
  592. 'HTTP_CLIENT_IP',
  593. ],
  594. );
  595. $request = new Request(
  596. [
  597. 'server' => [
  598. 'REMOTE_ADDR' => '10.0.0.2',
  599. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  600. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  601. ],
  602. ],
  603. $this->requestId,
  604. $this->config,
  605. $this->csrfTokenManager,
  606. $this->stream
  607. );
  608. $this->assertSame('192.168.0.233', $request->getRemoteAddress());
  609. }
  610. public function testGetRemoteAddressIPv6VerifyPriorityHeader() {
  611. $this->config
  612. ->expects($this->exactly(2))
  613. ->method('getSystemValue')
  614. ->withConsecutive(
  615. ['trusted_proxies'],
  616. ['forwarded_for_headers'],
  617. )-> willReturnOnConsecutiveCalls(
  618. ['2001:db8:85a3:8d3:1319:8a2e:370:7348'],
  619. [
  620. 'HTTP_X_FORWARDED',
  621. 'HTTP_X_FORWARDED_FOR',
  622. 'HTTP_CLIENT_IP',
  623. ],
  624. );
  625. $request = new Request(
  626. [
  627. 'server' => [
  628. 'REMOTE_ADDR' => '2001:db8:85a3:8d3:1319:8a2e:370:7348',
  629. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  630. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  631. ],
  632. ],
  633. $this->requestId,
  634. $this->config,
  635. $this->csrfTokenManager,
  636. $this->stream
  637. );
  638. $this->assertSame('192.168.0.233', $request->getRemoteAddress());
  639. }
  640. public function testGetRemoteAddressWithMatchingCidrTrustedRemote() {
  641. $this->config
  642. ->expects($this->exactly(2))
  643. ->method('getSystemValue')
  644. ->withConsecutive(
  645. ['trusted_proxies'],
  646. ['forwarded_for_headers'],
  647. )-> willReturnOnConsecutiveCalls(
  648. ['192.168.2.0/24'],
  649. ['HTTP_X_FORWARDED_FOR'],
  650. );
  651. $request = new Request(
  652. [
  653. 'server' => [
  654. 'REMOTE_ADDR' => '192.168.2.99',
  655. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  656. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  657. ],
  658. ],
  659. $this->requestId,
  660. $this->config,
  661. $this->csrfTokenManager,
  662. $this->stream
  663. );
  664. $this->assertSame('192.168.0.233', $request->getRemoteAddress());
  665. }
  666. public function testGetRemoteAddressWithNotMatchingCidrTrustedRemote() {
  667. $this->config
  668. ->expects($this->once())
  669. ->method('getSystemValue')
  670. ->with('trusted_proxies')
  671. ->willReturn(['192.168.2.0/24']);
  672. $request = new Request(
  673. [
  674. 'server' => [
  675. 'REMOTE_ADDR' => '192.168.3.99',
  676. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  677. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  678. ],
  679. ],
  680. $this->requestId,
  681. $this->config,
  682. $this->csrfTokenManager,
  683. $this->stream
  684. );
  685. $this->assertSame('192.168.3.99', $request->getRemoteAddress());
  686. }
  687. public function testGetRemoteIpv6AddressWithMatchingIpv6CidrTrustedRemote() {
  688. $this->config
  689. ->expects($this->exactly(2))
  690. ->method('getSystemValue')
  691. ->withConsecutive(
  692. ['trusted_proxies'],
  693. ['forwarded_for_headers']
  694. )->willReturnOnConsecutiveCalls(
  695. ['2001:db8:85a3:8d3:1319:8a20::/95'],
  696. ['HTTP_X_FORWARDED_FOR']
  697. );
  698. $request = new Request(
  699. [
  700. 'server' => [
  701. 'REMOTE_ADDR' => '2001:db8:85a3:8d3:1319:8a21:370:7348',
  702. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  703. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  704. ],
  705. ],
  706. $this->requestId,
  707. $this->config,
  708. $this->csrfTokenManager,
  709. $this->stream
  710. );
  711. $this->assertSame('192.168.0.233', $request->getRemoteAddress());
  712. }
  713. public function testGetRemoteAddressIpv6WithNotMatchingCidrTrustedRemote() {
  714. $this->config
  715. ->expects($this->once())
  716. ->method('getSystemValue')
  717. ->with('trusted_proxies')
  718. ->willReturn(['fd::/8']);
  719. $request = new Request(
  720. [
  721. 'server' => [
  722. 'REMOTE_ADDR' => '2001:db8:85a3:8d3:1319:8a2e:370:7348',
  723. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  724. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  725. ],
  726. ],
  727. $this->requestId,
  728. $this->config,
  729. $this->csrfTokenManager,
  730. $this->stream
  731. );
  732. $this->assertSame('2001:db8:85a3:8d3:1319:8a2e:370:7348', $request->getRemoteAddress());
  733. }
  734. public function testGetRemoteAddressIpv6WithInvalidTrustedProxy() {
  735. $this->config
  736. ->expects($this->once())
  737. ->method('getSystemValue')
  738. ->with('trusted_proxies')
  739. ->willReturn(['fx::/8']);
  740. $request = new Request(
  741. [
  742. 'server' => [
  743. 'REMOTE_ADDR' => '2001:db8:85a3:8d3:1319:8a2e:370:7348',
  744. 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
  745. 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
  746. ],
  747. ],
  748. $this->requestId,
  749. $this->config,
  750. $this->csrfTokenManager,
  751. $this->stream
  752. );
  753. $this->assertSame('2001:db8:85a3:8d3:1319:8a2e:370:7348', $request->getRemoteAddress());
  754. }
  755. public function testGetRemoteAddressWithXForwardedForIPv6() {
  756. $this->config
  757. ->expects($this->exactly(2))
  758. ->method('getSystemValue')
  759. ->withConsecutive(
  760. ['trusted_proxies'],
  761. ['forwarded_for_headers'],
  762. )-> willReturnOnConsecutiveCalls(
  763. ['192.168.2.0/24'],
  764. ['HTTP_X_FORWARDED_FOR'],
  765. );
  766. $request = new Request(
  767. [
  768. 'server' => [
  769. 'REMOTE_ADDR' => '192.168.2.99',
  770. 'HTTP_X_FORWARDED_FOR' => '[2001:db8:85a3:8d3:1319:8a2e:370:7348]',
  771. ],
  772. ],
  773. $this->requestId,
  774. $this->config,
  775. $this->csrfTokenManager,
  776. $this->stream
  777. );
  778. $this->assertSame('2001:db8:85a3:8d3:1319:8a2e:370:7348', $request->getRemoteAddress());
  779. }
  780. /**
  781. * @return array
  782. */
  783. public function httpProtocolProvider() {
  784. return [
  785. // Valid HTTP 1.0
  786. ['HTTP/1.0', 'HTTP/1.0'],
  787. ['http/1.0', 'HTTP/1.0'],
  788. ['HTTp/1.0', 'HTTP/1.0'],
  789. // Valid HTTP 1.1
  790. ['HTTP/1.1', 'HTTP/1.1'],
  791. ['http/1.1', 'HTTP/1.1'],
  792. ['HTTp/1.1', 'HTTP/1.1'],
  793. // Valid HTTP 2.0
  794. ['HTTP/2', 'HTTP/2'],
  795. ['http/2', 'HTTP/2'],
  796. ['HTTp/2', 'HTTP/2'],
  797. // Invalid
  798. ['HTTp/394', 'HTTP/1.1'],
  799. ['InvalidProvider/1.1', 'HTTP/1.1'],
  800. [null, 'HTTP/1.1'],
  801. ['', 'HTTP/1.1'],
  802. ];
  803. }
  804. /**
  805. * @dataProvider httpProtocolProvider
  806. *
  807. * @param mixed $input
  808. * @param string $expected
  809. */
  810. public function testGetHttpProtocol($input, $expected) {
  811. $request = new Request(
  812. [
  813. 'server' => [
  814. 'SERVER_PROTOCOL' => $input,
  815. ],
  816. ],
  817. $this->requestId,
  818. $this->config,
  819. $this->csrfTokenManager,
  820. $this->stream
  821. );
  822. $this->assertSame($expected, $request->getHttpProtocol());
  823. }
  824. public function testGetServerProtocolWithOverride() {
  825. $this->config
  826. ->expects($this->exactly(3))
  827. ->method('getSystemValueString')
  828. ->willReturnMap([
  829. ['overwriteprotocol', '', 'customProtocol'],
  830. ['overwritecondaddr', '', ''],
  831. ]);
  832. $request = new Request(
  833. [],
  834. $this->requestId,
  835. $this->config,
  836. $this->csrfTokenManager,
  837. $this->stream
  838. );
  839. $this->assertSame('customProtocol', $request->getServerProtocol());
  840. }
  841. public function testGetServerProtocolWithProtoValid() {
  842. $this->config
  843. ->method('getSystemValue')
  844. ->willReturnCallback(function ($key, $default) {
  845. if ($key === 'trusted_proxies') {
  846. return ['1.2.3.4'];
  847. }
  848. return $default;
  849. });
  850. $requestHttps = new Request(
  851. [
  852. 'server' => [
  853. 'HTTP_X_FORWARDED_PROTO' => 'HtTpS',
  854. 'REMOTE_ADDR' => '1.2.3.4',
  855. ],
  856. ],
  857. $this->requestId,
  858. $this->config,
  859. $this->csrfTokenManager,
  860. $this->stream
  861. );
  862. $requestHttp = new Request(
  863. [
  864. 'server' => [
  865. 'HTTP_X_FORWARDED_PROTO' => 'HTTp',
  866. 'REMOTE_ADDR' => '1.2.3.4',
  867. ],
  868. ],
  869. $this->requestId,
  870. $this->config,
  871. $this->csrfTokenManager,
  872. $this->stream
  873. );
  874. $this->assertSame('https', $requestHttps->getServerProtocol());
  875. $this->assertSame('http', $requestHttp->getServerProtocol());
  876. }
  877. public function testGetServerProtocolWithHttpsServerValueOn() {
  878. $this->config
  879. ->method('getSystemValue')
  880. ->willReturnCallback(function ($key, $default) {
  881. return $default;
  882. });
  883. $request = new Request(
  884. [
  885. 'server' => [
  886. 'HTTPS' => 'on'
  887. ],
  888. ],
  889. $this->requestId,
  890. $this->config,
  891. $this->csrfTokenManager,
  892. $this->stream
  893. );
  894. $this->assertSame('https', $request->getServerProtocol());
  895. }
  896. public function testGetServerProtocolWithHttpsServerValueOff() {
  897. $this->config
  898. ->method('getSystemValue')
  899. ->willReturnCallback(function ($key, $default) {
  900. return $default;
  901. });
  902. $request = new Request(
  903. [
  904. 'server' => [
  905. 'HTTPS' => 'off'
  906. ],
  907. ],
  908. $this->requestId,
  909. $this->config,
  910. $this->csrfTokenManager,
  911. $this->stream
  912. );
  913. $this->assertSame('http', $request->getServerProtocol());
  914. }
  915. public function testGetServerProtocolWithHttpsServerValueEmpty() {
  916. $this->config
  917. ->method('getSystemValue')
  918. ->willReturnCallback(function ($key, $default) {
  919. return $default;
  920. });
  921. $request = new Request(
  922. [
  923. 'server' => [
  924. 'HTTPS' => ''
  925. ],
  926. ],
  927. $this->requestId,
  928. $this->config,
  929. $this->csrfTokenManager,
  930. $this->stream
  931. );
  932. $this->assertSame('http', $request->getServerProtocol());
  933. }
  934. public function testGetServerProtocolDefault() {
  935. $this->config
  936. ->method('getSystemValue')
  937. ->willReturnCallback(function ($key, $default) {
  938. return $default;
  939. });
  940. $request = new Request(
  941. [],
  942. $this->requestId,
  943. $this->config,
  944. $this->csrfTokenManager,
  945. $this->stream
  946. );
  947. $this->assertSame('http', $request->getServerProtocol());
  948. }
  949. public function testGetServerProtocolBehindLoadBalancers() {
  950. $this->config
  951. ->method('getSystemValue')
  952. ->willReturnCallback(function ($key, $default) {
  953. if ($key === 'trusted_proxies') {
  954. return ['1.2.3.4'];
  955. }
  956. return $default;
  957. });
  958. $request = new Request(
  959. [
  960. 'server' => [
  961. 'HTTP_X_FORWARDED_PROTO' => 'https,http,http',
  962. 'REMOTE_ADDR' => '1.2.3.4',
  963. ],
  964. ],
  965. $this->requestId,
  966. $this->config,
  967. $this->csrfTokenManager,
  968. $this->stream
  969. );
  970. $this->assertSame('https', $request->getServerProtocol());
  971. }
  972. /**
  973. * @dataProvider userAgentProvider
  974. * @param string $testAgent
  975. * @param array $userAgent
  976. * @param bool $matches
  977. */
  978. public function testUserAgent($testAgent, $userAgent, $matches) {
  979. $request = new Request(
  980. [
  981. 'server' => [
  982. 'HTTP_USER_AGENT' => $testAgent,
  983. ]
  984. ],
  985. $this->requestId,
  986. $this->config,
  987. $this->csrfTokenManager,
  988. $this->stream
  989. );
  990. $this->assertSame($matches, $request->isUserAgent($userAgent));
  991. }
  992. /**
  993. * @dataProvider userAgentProvider
  994. * @param string $testAgent
  995. * @param array $userAgent
  996. * @param bool $matches
  997. */
  998. public function testUndefinedUserAgent($testAgent, $userAgent, $matches) {
  999. $request = new Request(
  1000. [],
  1001. $this->requestId,
  1002. $this->config,
  1003. $this->csrfTokenManager,
  1004. $this->stream
  1005. );
  1006. $this->assertFalse($request->isUserAgent($userAgent));
  1007. }
  1008. /**
  1009. * @return array
  1010. */
  1011. public function userAgentProvider() {
  1012. return [
  1013. [
  1014. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
  1015. [
  1016. Request::USER_AGENT_IE
  1017. ],
  1018. true,
  1019. ],
  1020. [
  1021. 'Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0',
  1022. [
  1023. Request::USER_AGENT_IE
  1024. ],
  1025. false,
  1026. ],
  1027. [
  1028. 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
  1029. [
  1030. Request::USER_AGENT_CHROME
  1031. ],
  1032. true,
  1033. ],
  1034. [
  1035. 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36',
  1036. [
  1037. Request::USER_AGENT_CHROME
  1038. ],
  1039. true,
  1040. ],
  1041. [
  1042. 'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36',
  1043. [
  1044. Request::USER_AGENT_ANDROID_MOBILE_CHROME
  1045. ],
  1046. true,
  1047. ],
  1048. [
  1049. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
  1050. [
  1051. Request::USER_AGENT_ANDROID_MOBILE_CHROME
  1052. ],
  1053. false,
  1054. ],
  1055. [
  1056. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
  1057. [
  1058. Request::USER_AGENT_IE,
  1059. Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  1060. ],
  1061. true,
  1062. ],
  1063. [
  1064. 'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36',
  1065. [
  1066. Request::USER_AGENT_IE,
  1067. Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  1068. ],
  1069. true,
  1070. ],
  1071. [
  1072. 'Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0',
  1073. [
  1074. Request::USER_AGENT_FREEBOX
  1075. ],
  1076. false,
  1077. ],
  1078. [
  1079. 'Mozilla/5.0',
  1080. [
  1081. Request::USER_AGENT_FREEBOX
  1082. ],
  1083. true,
  1084. ],
  1085. [
  1086. 'Fake Mozilla/5.0',
  1087. [
  1088. Request::USER_AGENT_FREEBOX
  1089. ],
  1090. false,
  1091. ],
  1092. [
  1093. 'Mozilla/5.0 (Android) ownCloud-android/2.0.0',
  1094. [
  1095. Request::USER_AGENT_CLIENT_ANDROID
  1096. ],
  1097. true,
  1098. ],
  1099. [
  1100. 'Mozilla/5.0 (Android) Nextcloud-android/2.0.0',
  1101. [
  1102. Request::USER_AGENT_CLIENT_ANDROID
  1103. ],
  1104. true,
  1105. ],
  1106. [
  1107. 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.99 Safari/537.36 Vivaldi/2.9.1705.41',
  1108. [
  1109. Request::USER_AGENT_CHROME
  1110. ],
  1111. true
  1112. ],
  1113. [
  1114. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.38 Safari/537.36 Brave/75',
  1115. [
  1116. Request::USER_AGENT_CHROME
  1117. ],
  1118. true
  1119. ],
  1120. [
  1121. 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 OPR/50.0.2762.67',
  1122. [
  1123. Request::USER_AGENT_CHROME
  1124. ],
  1125. true
  1126. ]
  1127. ];
  1128. }
  1129. public function testInsecureServerHostServerNameHeader() {
  1130. $request = new Request(
  1131. [
  1132. 'server' => [
  1133. 'SERVER_NAME' => 'from.server.name:8080',
  1134. ]
  1135. ],
  1136. $this->requestId,
  1137. $this->config,
  1138. $this->csrfTokenManager,
  1139. $this->stream
  1140. );
  1141. $this->assertSame('from.server.name:8080', $request->getInsecureServerHost());
  1142. }
  1143. public function testInsecureServerHostHttpHostHeader() {
  1144. $request = new Request(
  1145. [
  1146. 'server' => [
  1147. 'SERVER_NAME' => 'from.server.name:8080',
  1148. 'HTTP_HOST' => 'from.host.header:8080',
  1149. ]
  1150. ],
  1151. $this->requestId,
  1152. $this->config,
  1153. $this->csrfTokenManager,
  1154. $this->stream
  1155. );
  1156. $this->assertSame('from.host.header:8080', $request->getInsecureServerHost());
  1157. }
  1158. public function testInsecureServerHostHttpFromForwardedHeaderSingle() {
  1159. $this->config
  1160. ->method('getSystemValue')
  1161. ->willReturnCallback(function ($key, $default) {
  1162. if ($key === 'trusted_proxies') {
  1163. return ['1.2.3.4'];
  1164. }
  1165. return $default;
  1166. });
  1167. $request = new Request(
  1168. [
  1169. 'server' => [
  1170. 'SERVER_NAME' => 'from.server.name:8080',
  1171. 'HTTP_HOST' => 'from.host.header:8080',
  1172. 'HTTP_X_FORWARDED_HOST' => 'from.forwarded.host:8080',
  1173. 'REMOTE_ADDR' => '1.2.3.4',
  1174. ]
  1175. ],
  1176. $this->requestId,
  1177. $this->config,
  1178. $this->csrfTokenManager,
  1179. $this->stream
  1180. );
  1181. $this->assertSame('from.forwarded.host:8080', $request->getInsecureServerHost());
  1182. }
  1183. public function testInsecureServerHostHttpFromForwardedHeaderStacked() {
  1184. $this->config
  1185. ->method('getSystemValue')
  1186. ->willReturnCallback(function ($key, $default) {
  1187. if ($key === 'trusted_proxies') {
  1188. return ['1.2.3.4'];
  1189. }
  1190. return $default;
  1191. });
  1192. $request = new Request(
  1193. [
  1194. 'server' => [
  1195. 'SERVER_NAME' => 'from.server.name:8080',
  1196. 'HTTP_HOST' => 'from.host.header:8080',
  1197. 'HTTP_X_FORWARDED_HOST' => 'from.forwarded.host2:8080,another.one:9000',
  1198. 'REMOTE_ADDR' => '1.2.3.4',
  1199. ]
  1200. ],
  1201. $this->requestId,
  1202. $this->config,
  1203. $this->csrfTokenManager,
  1204. $this->stream
  1205. );
  1206. $this->assertSame('from.forwarded.host2:8080', $request->getInsecureServerHost());
  1207. }
  1208. public function testGetServerHostWithOverwriteHost() {
  1209. $this->config
  1210. ->method('getSystemValueString')
  1211. ->willReturnCallback(function ($key, $default) {
  1212. if ($key === 'overwritecondaddr') {
  1213. return '';
  1214. } elseif ($key === 'overwritehost') {
  1215. return 'my.overwritten.host';
  1216. }
  1217. return $default;
  1218. });
  1219. $request = new Request(
  1220. [],
  1221. $this->requestId,
  1222. $this->config,
  1223. $this->csrfTokenManager,
  1224. $this->stream
  1225. );
  1226. $this->assertSame('my.overwritten.host', $request->getServerHost());
  1227. }
  1228. public function testGetServerHostWithTrustedDomain() {
  1229. $this->config
  1230. ->method('getSystemValue')
  1231. ->willReturnCallback(function ($key, $default) {
  1232. if ($key === 'trusted_proxies') {
  1233. return ['1.2.3.4'];
  1234. } elseif ($key === 'trusted_domains') {
  1235. return ['my.trusted.host'];
  1236. }
  1237. return $default;
  1238. });
  1239. $request = new Request(
  1240. [
  1241. 'server' => [
  1242. 'HTTP_X_FORWARDED_HOST' => 'my.trusted.host',
  1243. 'REMOTE_ADDR' => '1.2.3.4',
  1244. ],
  1245. ],
  1246. $this->requestId,
  1247. $this->config,
  1248. $this->csrfTokenManager,
  1249. $this->stream
  1250. );
  1251. $this->assertSame('my.trusted.host', $request->getServerHost());
  1252. }
  1253. public function testGetServerHostWithUntrustedDomain() {
  1254. $this->config
  1255. ->method('getSystemValue')
  1256. ->willReturnCallback(function ($key, $default) {
  1257. if ($key === 'trusted_proxies') {
  1258. return ['1.2.3.4'];
  1259. } elseif ($key === 'trusted_domains') {
  1260. return ['my.trusted.host'];
  1261. }
  1262. return $default;
  1263. });
  1264. $request = new Request(
  1265. [
  1266. 'server' => [
  1267. 'HTTP_X_FORWARDED_HOST' => 'my.untrusted.host',
  1268. 'REMOTE_ADDR' => '1.2.3.4',
  1269. ],
  1270. ],
  1271. $this->requestId,
  1272. $this->config,
  1273. $this->csrfTokenManager,
  1274. $this->stream
  1275. );
  1276. $this->assertSame('my.trusted.host', $request->getServerHost());
  1277. }
  1278. public function testGetServerHostWithNoTrustedDomain() {
  1279. $this->config
  1280. ->method('getSystemValue')
  1281. ->willReturnCallback(function ($key, $default) {
  1282. if ($key === 'trusted_proxies') {
  1283. return ['1.2.3.4'];
  1284. }
  1285. return $default;
  1286. });
  1287. $request = new Request(
  1288. [
  1289. 'server' => [
  1290. 'HTTP_X_FORWARDED_HOST' => 'my.untrusted.host',
  1291. 'REMOTE_ADDR' => '1.2.3.4',
  1292. ],
  1293. ],
  1294. $this->requestId,
  1295. $this->config,
  1296. $this->csrfTokenManager,
  1297. $this->stream
  1298. );
  1299. $this->assertSame('', $request->getServerHost());
  1300. }
  1301. /**
  1302. * @return array
  1303. */
  1304. public function dataGetServerHostTrustedDomain() {
  1305. return [
  1306. 'is array' => ['my.trusted.host', ['my.trusted.host']],
  1307. 'is array but undefined index 0' => ['my.trusted.host', [2 => 'my.trusted.host']],
  1308. 'is string' => ['my.trusted.host', 'my.trusted.host'],
  1309. 'is null' => ['', null],
  1310. ];
  1311. }
  1312. /**
  1313. * @dataProvider dataGetServerHostTrustedDomain
  1314. * @param $expected
  1315. * @param $trustedDomain
  1316. */
  1317. public function testGetServerHostTrustedDomain($expected, $trustedDomain) {
  1318. $this->config
  1319. ->method('getSystemValue')
  1320. ->willReturnCallback(function ($key, $default) use ($trustedDomain) {
  1321. if ($key === 'trusted_proxies') {
  1322. return ['1.2.3.4'];
  1323. }
  1324. if ($key === 'trusted_domains') {
  1325. return $trustedDomain;
  1326. }
  1327. return $default;
  1328. });
  1329. $request = new Request(
  1330. [
  1331. 'server' => [
  1332. 'HTTP_X_FORWARDED_HOST' => 'my.untrusted.host',
  1333. 'REMOTE_ADDR' => '1.2.3.4',
  1334. ],
  1335. ],
  1336. $this->requestId,
  1337. $this->config,
  1338. $this->csrfTokenManager,
  1339. $this->stream
  1340. );
  1341. $this->assertSame($expected, $request->getServerHost());
  1342. }
  1343. public function testGetOverwriteHostDefaultNull() {
  1344. $this->config
  1345. ->expects($this->once())
  1346. ->method('getSystemValueString')
  1347. ->with('overwritehost')
  1348. ->willReturn('');
  1349. $request = new Request(
  1350. [],
  1351. $this->requestId,
  1352. $this->config,
  1353. $this->csrfTokenManager,
  1354. $this->stream
  1355. );
  1356. $this->assertNull(self::invokePrivate($request, 'getOverwriteHost'));
  1357. }
  1358. public function testGetOverwriteHostWithOverwrite() {
  1359. $this->config
  1360. ->expects($this->exactly(3))
  1361. ->method('getSystemValueString')
  1362. ->willReturnMap([
  1363. ['overwritehost', '', 'www.owncloud.org'],
  1364. ['overwritecondaddr', '', ''],
  1365. ]);
  1366. $request = new Request(
  1367. [],
  1368. $this->requestId,
  1369. $this->config,
  1370. $this->csrfTokenManager,
  1371. $this->stream
  1372. );
  1373. $this->assertSame('www.owncloud.org', self::invokePrivate($request, 'getOverwriteHost'));
  1374. }
  1375. public function testGetPathInfoNotProcessible() {
  1376. $this->expectException(\Exception::class);
  1377. $this->expectExceptionMessage('The requested uri(/foo.php) cannot be processed by the script \'/var/www/index.php\')');
  1378. $request = new Request(
  1379. [
  1380. 'server' => [
  1381. 'REQUEST_URI' => '/foo.php',
  1382. 'SCRIPT_NAME' => '/var/www/index.php',
  1383. ]
  1384. ],
  1385. $this->requestId,
  1386. $this->config,
  1387. $this->csrfTokenManager,
  1388. $this->stream
  1389. );
  1390. $request->getPathInfo();
  1391. }
  1392. public function testGetRawPathInfoNotProcessible() {
  1393. $this->expectException(\Exception::class);
  1394. $this->expectExceptionMessage('The requested uri(/foo.php) cannot be processed by the script \'/var/www/index.php\')');
  1395. $request = new Request(
  1396. [
  1397. 'server' => [
  1398. 'REQUEST_URI' => '/foo.php',
  1399. 'SCRIPT_NAME' => '/var/www/index.php',
  1400. ]
  1401. ],
  1402. $this->requestId,
  1403. $this->config,
  1404. $this->csrfTokenManager,
  1405. $this->stream
  1406. );
  1407. $request->getRawPathInfo();
  1408. }
  1409. /**
  1410. * @dataProvider genericPathInfoProvider
  1411. * @param string $requestUri
  1412. * @param string $scriptName
  1413. * @param string $expected
  1414. */
  1415. public function testGetPathInfoWithoutSetEnvGeneric($requestUri, $scriptName, $expected) {
  1416. $request = new Request(
  1417. [
  1418. 'server' => [
  1419. 'REQUEST_URI' => $requestUri,
  1420. 'SCRIPT_NAME' => $scriptName,
  1421. ]
  1422. ],
  1423. $this->requestId,
  1424. $this->config,
  1425. $this->csrfTokenManager,
  1426. $this->stream
  1427. );
  1428. $this->assertSame($expected, $request->getPathInfo());
  1429. }
  1430. /**
  1431. * @dataProvider genericPathInfoProvider
  1432. * @param string $requestUri
  1433. * @param string $scriptName
  1434. * @param string $expected
  1435. */
  1436. public function testGetRawPathInfoWithoutSetEnvGeneric($requestUri, $scriptName, $expected) {
  1437. $request = new Request(
  1438. [
  1439. 'server' => [
  1440. 'REQUEST_URI' => $requestUri,
  1441. 'SCRIPT_NAME' => $scriptName,
  1442. ]
  1443. ],
  1444. $this->requestId,
  1445. $this->config,
  1446. $this->csrfTokenManager,
  1447. $this->stream
  1448. );
  1449. $this->assertSame($expected, $request->getRawPathInfo());
  1450. }
  1451. /**
  1452. * @dataProvider rawPathInfoProvider
  1453. * @param string $requestUri
  1454. * @param string $scriptName
  1455. * @param string $expected
  1456. */
  1457. public function testGetRawPathInfoWithoutSetEnv($requestUri, $scriptName, $expected) {
  1458. $request = new Request(
  1459. [
  1460. 'server' => [
  1461. 'REQUEST_URI' => $requestUri,
  1462. 'SCRIPT_NAME' => $scriptName,
  1463. ]
  1464. ],
  1465. $this->requestId,
  1466. $this->config,
  1467. $this->csrfTokenManager,
  1468. $this->stream
  1469. );
  1470. $this->assertSame($expected, $request->getRawPathInfo());
  1471. }
  1472. /**
  1473. * @dataProvider pathInfoProvider
  1474. * @param string $requestUri
  1475. * @param string $scriptName
  1476. * @param string $expected
  1477. */
  1478. public function testGetPathInfoWithoutSetEnv($requestUri, $scriptName, $expected) {
  1479. $request = new Request(
  1480. [
  1481. 'server' => [
  1482. 'REQUEST_URI' => $requestUri,
  1483. 'SCRIPT_NAME' => $scriptName,
  1484. ]
  1485. ],
  1486. $this->requestId,
  1487. $this->config,
  1488. $this->csrfTokenManager,
  1489. $this->stream
  1490. );
  1491. $this->assertSame($expected, $request->getPathInfo());
  1492. }
  1493. /**
  1494. * @return array
  1495. */
  1496. public function genericPathInfoProvider() {
  1497. return [
  1498. ['/core/index.php?XDEBUG_SESSION_START=14600', '/core/index.php', ''],
  1499. ['/index.php/apps/files/', 'index.php', '/apps/files/'],
  1500. ['/index.php/apps/files/../&amp;/&?someQueryParameter=QueryParam', 'index.php', '/apps/files/../&amp;/&'],
  1501. ['/remote.php/漢字編碼方法 / 汉字编码方法', 'remote.php', '/漢字編碼方法 / 汉字编码方法'],
  1502. ['///removeTrailin//gSlashes///', 'remote.php', '/removeTrailin/gSlashes/'],
  1503. ['/', '/', ''],
  1504. ['', '', ''],
  1505. ];
  1506. }
  1507. /**
  1508. * @return array
  1509. */
  1510. public function rawPathInfoProvider() {
  1511. return [
  1512. ['/foo%2Fbar/subfolder', '', 'foo%2Fbar/subfolder'],
  1513. ];
  1514. }
  1515. /**
  1516. * @return array
  1517. */
  1518. public function pathInfoProvider() {
  1519. return [
  1520. ['/foo%2Fbar/subfolder', '', 'foo/bar/subfolder'],
  1521. ];
  1522. }
  1523. public function testGetRequestUriWithoutOverwrite() {
  1524. $this->config
  1525. ->expects($this->once())
  1526. ->method('getSystemValueString')
  1527. ->with('overwritewebroot')
  1528. ->willReturn('');
  1529. $request = new Request(
  1530. [
  1531. 'server' => [
  1532. 'REQUEST_URI' => '/test.php'
  1533. ]
  1534. ],
  1535. $this->requestId,
  1536. $this->config,
  1537. $this->csrfTokenManager,
  1538. $this->stream
  1539. );
  1540. $this->assertSame('/test.php', $request->getRequestUri());
  1541. }
  1542. public function providesGetRequestUriWithOverwriteData() {
  1543. return [
  1544. ['/scriptname.php/some/PathInfo', '/owncloud/', ''],
  1545. ['/scriptname.php/some/PathInfo', '/owncloud/', '123', '123.123.123.123'],
  1546. ];
  1547. }
  1548. /**
  1549. * @dataProvider providesGetRequestUriWithOverwriteData
  1550. */
  1551. public function testGetRequestUriWithOverwrite($expectedUri, $overwriteWebRoot, $overwriteCondAddr, $remoteAddr = '') {
  1552. $this->config
  1553. ->expects($this->exactly(2))
  1554. ->method('getSystemValueString')
  1555. ->willReturnMap([
  1556. ['overwritewebroot', '', $overwriteWebRoot],
  1557. ['overwritecondaddr', '', $overwriteCondAddr],
  1558. ]);
  1559. $request = $this->getMockBuilder(Request::class)
  1560. ->setMethods(['getScriptName'])
  1561. ->setConstructorArgs([
  1562. [
  1563. 'server' => [
  1564. 'REQUEST_URI' => '/test.php/some/PathInfo',
  1565. 'SCRIPT_NAME' => '/test.php',
  1566. 'REMOTE_ADDR' => $remoteAddr
  1567. ]
  1568. ],
  1569. $this->requestId,
  1570. $this->config,
  1571. $this->csrfTokenManager,
  1572. $this->stream
  1573. ])
  1574. ->getMock();
  1575. $request
  1576. ->expects($this->once())
  1577. ->method('getScriptName')
  1578. ->willReturn('/scriptname.php');
  1579. $this->assertSame($expectedUri, $request->getRequestUri());
  1580. }
  1581. public function testPassesCSRFCheckWithGet() {
  1582. /** @var Request $request */
  1583. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1584. ->setMethods(['getScriptName'])
  1585. ->setConstructorArgs([
  1586. [
  1587. 'get' => [
  1588. 'requesttoken' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1589. ],
  1590. 'cookies' => [
  1591. 'nc_sameSiteCookiestrict' => 'true',
  1592. 'nc_sameSiteCookielax' => 'true',
  1593. ],
  1594. ],
  1595. $this->requestId,
  1596. $this->config,
  1597. $this->csrfTokenManager,
  1598. $this->stream
  1599. ])
  1600. ->getMock();
  1601. $token = new CsrfToken('AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds');
  1602. $this->csrfTokenManager
  1603. ->expects($this->once())
  1604. ->method('isTokenValid')
  1605. ->with($token)
  1606. ->willReturn(true);
  1607. $this->assertTrue($request->passesCSRFCheck());
  1608. }
  1609. public function testPassesCSRFCheckWithPost() {
  1610. /** @var Request $request */
  1611. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1612. ->setMethods(['getScriptName'])
  1613. ->setConstructorArgs([
  1614. [
  1615. 'post' => [
  1616. 'requesttoken' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1617. ],
  1618. 'cookies' => [
  1619. 'nc_sameSiteCookiestrict' => 'true',
  1620. 'nc_sameSiteCookielax' => 'true',
  1621. ],
  1622. ],
  1623. $this->requestId,
  1624. $this->config,
  1625. $this->csrfTokenManager,
  1626. $this->stream
  1627. ])
  1628. ->getMock();
  1629. $token = new CsrfToken('AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds');
  1630. $this->csrfTokenManager
  1631. ->expects($this->once())
  1632. ->method('isTokenValid')
  1633. ->with($token)
  1634. ->willReturn(true);
  1635. $this->assertTrue($request->passesCSRFCheck());
  1636. }
  1637. public function testPassesCSRFCheckWithHeader() {
  1638. /** @var Request $request */
  1639. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1640. ->setMethods(['getScriptName'])
  1641. ->setConstructorArgs([
  1642. [
  1643. 'server' => [
  1644. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1645. ],
  1646. 'cookies' => [
  1647. 'nc_sameSiteCookiestrict' => 'true',
  1648. 'nc_sameSiteCookielax' => 'true',
  1649. ],
  1650. ],
  1651. $this->requestId,
  1652. $this->config,
  1653. $this->csrfTokenManager,
  1654. $this->stream
  1655. ])
  1656. ->getMock();
  1657. $token = new CsrfToken('AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds');
  1658. $this->csrfTokenManager
  1659. ->expects($this->once())
  1660. ->method('isTokenValid')
  1661. ->with($token)
  1662. ->willReturn(true);
  1663. $this->assertTrue($request->passesCSRFCheck());
  1664. }
  1665. public function testPassesCSRFCheckWithGetAndWithoutCookies() {
  1666. /** @var Request $request */
  1667. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1668. ->setMethods(['getScriptName'])
  1669. ->setConstructorArgs([
  1670. [
  1671. 'get' => [
  1672. 'requesttoken' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1673. ],
  1674. ],
  1675. $this->requestId,
  1676. $this->config,
  1677. $this->csrfTokenManager,
  1678. $this->stream
  1679. ])
  1680. ->getMock();
  1681. $this->csrfTokenManager
  1682. ->expects($this->once())
  1683. ->method('isTokenValid')
  1684. ->willReturn(true);
  1685. $this->assertTrue($request->passesCSRFCheck());
  1686. }
  1687. public function testPassesCSRFCheckWithPostAndWithoutCookies() {
  1688. /** @var Request $request */
  1689. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1690. ->setMethods(['getScriptName'])
  1691. ->setConstructorArgs([
  1692. [
  1693. 'post' => [
  1694. 'requesttoken' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1695. ],
  1696. ],
  1697. $this->requestId,
  1698. $this->config,
  1699. $this->csrfTokenManager,
  1700. $this->stream
  1701. ])
  1702. ->getMock();
  1703. $this->csrfTokenManager
  1704. ->expects($this->once())
  1705. ->method('isTokenValid')
  1706. ->willReturn(true);
  1707. $this->assertTrue($request->passesCSRFCheck());
  1708. }
  1709. public function testPassesCSRFCheckWithHeaderAndWithoutCookies() {
  1710. /** @var Request $request */
  1711. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1712. ->setMethods(['getScriptName'])
  1713. ->setConstructorArgs([
  1714. [
  1715. 'server' => [
  1716. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1717. ],
  1718. ],
  1719. $this->requestId,
  1720. $this->config,
  1721. $this->csrfTokenManager,
  1722. $this->stream
  1723. ])
  1724. ->getMock();
  1725. $this->csrfTokenManager
  1726. ->expects($this->once())
  1727. ->method('isTokenValid')
  1728. ->willReturn(true);
  1729. $this->assertTrue($request->passesCSRFCheck());
  1730. }
  1731. public function testFailsCSRFCheckWithHeaderAndNotAllChecksPassing() {
  1732. /** @var Request $request */
  1733. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1734. ->setMethods(['getScriptName'])
  1735. ->setConstructorArgs([
  1736. [
  1737. 'server' => [
  1738. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1739. ],
  1740. 'cookies' => [
  1741. session_name() => 'asdf',
  1742. 'nc_sameSiteCookiestrict' => 'true',
  1743. ],
  1744. ],
  1745. $this->requestId,
  1746. $this->config,
  1747. $this->csrfTokenManager,
  1748. $this->stream
  1749. ])
  1750. ->getMock();
  1751. $this->csrfTokenManager
  1752. ->expects($this->never())
  1753. ->method('isTokenValid');
  1754. $this->assertFalse($request->passesCSRFCheck());
  1755. }
  1756. public function testPassesStrictCookieCheckWithAllCookiesAndStrict() {
  1757. /** @var Request $request */
  1758. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1759. ->setMethods(['getScriptName', 'getCookieParams'])
  1760. ->setConstructorArgs([
  1761. [
  1762. 'server' => [
  1763. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1764. ],
  1765. 'cookies' => [
  1766. session_name() => 'asdf',
  1767. '__Host-nc_sameSiteCookiestrict' => 'true',
  1768. '__Host-nc_sameSiteCookielax' => 'true',
  1769. ],
  1770. ],
  1771. $this->requestId,
  1772. $this->config,
  1773. $this->csrfTokenManager,
  1774. $this->stream
  1775. ])
  1776. ->getMock();
  1777. $request
  1778. ->expects($this->any())
  1779. ->method('getCookieParams')
  1780. ->willReturn([
  1781. 'secure' => true,
  1782. 'path' => '/',
  1783. ]);
  1784. $this->assertTrue($request->passesStrictCookieCheck());
  1785. }
  1786. public function testFailsStrictCookieCheckWithAllCookiesAndMissingStrict() {
  1787. /** @var Request $request */
  1788. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1789. ->setMethods(['getScriptName', 'getCookieParams'])
  1790. ->setConstructorArgs([
  1791. [
  1792. 'server' => [
  1793. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1794. ],
  1795. 'cookies' => [
  1796. session_name() => 'asdf',
  1797. 'nc_sameSiteCookiestrict' => 'true',
  1798. 'nc_sameSiteCookielax' => 'true',
  1799. ],
  1800. ],
  1801. $this->requestId,
  1802. $this->config,
  1803. $this->csrfTokenManager,
  1804. $this->stream
  1805. ])
  1806. ->getMock();
  1807. $request
  1808. ->expects($this->any())
  1809. ->method('getCookieParams')
  1810. ->willReturn([
  1811. 'secure' => true,
  1812. 'path' => '/',
  1813. ]);
  1814. $this->assertFalse($request->passesStrictCookieCheck());
  1815. }
  1816. public function testGetCookieParams() {
  1817. /** @var Request $request */
  1818. $request = $this->getMockBuilder(Request::class)
  1819. ->setMethods(['getScriptName'])
  1820. ->setConstructorArgs([
  1821. [],
  1822. $this->requestId,
  1823. $this->config,
  1824. $this->csrfTokenManager,
  1825. $this->stream
  1826. ])
  1827. ->getMock();
  1828. $actual = $request->getCookieParams();
  1829. $this->assertSame(session_get_cookie_params(), $actual);
  1830. }
  1831. public function testPassesStrictCookieCheckWithAllCookies() {
  1832. /** @var Request $request */
  1833. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1834. ->setMethods(['getScriptName'])
  1835. ->setConstructorArgs([
  1836. [
  1837. 'server' => [
  1838. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1839. ],
  1840. 'cookies' => [
  1841. session_name() => 'asdf',
  1842. 'nc_sameSiteCookiestrict' => 'true',
  1843. 'nc_sameSiteCookielax' => 'true',
  1844. ],
  1845. ],
  1846. $this->requestId,
  1847. $this->config,
  1848. $this->csrfTokenManager,
  1849. $this->stream
  1850. ])
  1851. ->getMock();
  1852. $this->assertTrue($request->passesStrictCookieCheck());
  1853. }
  1854. public function testPassesStrictCookieCheckWithRandomCookies() {
  1855. /** @var Request $request */
  1856. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1857. ->setMethods(['getScriptName'])
  1858. ->setConstructorArgs([
  1859. [
  1860. 'server' => [
  1861. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1862. ],
  1863. 'cookies' => [
  1864. 'RandomCookie' => 'asdf',
  1865. ],
  1866. ],
  1867. $this->requestId,
  1868. $this->config,
  1869. $this->csrfTokenManager,
  1870. $this->stream
  1871. ])
  1872. ->getMock();
  1873. $this->assertTrue($request->passesStrictCookieCheck());
  1874. }
  1875. public function testFailsStrictCookieCheckWithSessionCookie() {
  1876. /** @var Request $request */
  1877. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1878. ->setMethods(['getScriptName'])
  1879. ->setConstructorArgs([
  1880. [
  1881. 'server' => [
  1882. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1883. ],
  1884. 'cookies' => [
  1885. session_name() => 'asdf',
  1886. ],
  1887. ],
  1888. $this->requestId,
  1889. $this->config,
  1890. $this->csrfTokenManager,
  1891. $this->stream
  1892. ])
  1893. ->getMock();
  1894. $this->assertFalse($request->passesStrictCookieCheck());
  1895. }
  1896. public function testFailsStrictCookieCheckWithRememberMeCookie() {
  1897. /** @var Request $request */
  1898. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1899. ->setMethods(['getScriptName'])
  1900. ->setConstructorArgs([
  1901. [
  1902. 'server' => [
  1903. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1904. ],
  1905. 'cookies' => [
  1906. 'nc_token' => 'asdf',
  1907. ],
  1908. ],
  1909. $this->requestId,
  1910. $this->config,
  1911. $this->csrfTokenManager,
  1912. $this->stream
  1913. ])
  1914. ->getMock();
  1915. $this->assertFalse($request->passesStrictCookieCheck());
  1916. }
  1917. public function testFailsCSRFCheckWithPostAndWithCookies() {
  1918. /** @var Request $request */
  1919. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1920. ->setMethods(['getScriptName'])
  1921. ->setConstructorArgs([
  1922. [
  1923. 'post' => [
  1924. 'requesttoken' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1925. ],
  1926. 'cookies' => [
  1927. session_name() => 'asdf',
  1928. 'foo' => 'bar',
  1929. ],
  1930. ],
  1931. $this->requestId,
  1932. $this->config,
  1933. $this->csrfTokenManager,
  1934. $this->stream
  1935. ])
  1936. ->getMock();
  1937. $this->csrfTokenManager
  1938. ->expects($this->never())
  1939. ->method('isTokenValid');
  1940. $this->assertFalse($request->passesCSRFCheck());
  1941. }
  1942. public function testFailStrictCookieCheckWithOnlyLaxCookie() {
  1943. /** @var Request $request */
  1944. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1945. ->setMethods(['getScriptName'])
  1946. ->setConstructorArgs([
  1947. [
  1948. 'server' => [
  1949. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1950. ],
  1951. 'cookies' => [
  1952. session_name() => 'asdf',
  1953. 'nc_sameSiteCookielax' => 'true',
  1954. ],
  1955. ],
  1956. $this->requestId,
  1957. $this->config,
  1958. $this->csrfTokenManager,
  1959. $this->stream
  1960. ])
  1961. ->getMock();
  1962. $this->assertFalse($request->passesStrictCookieCheck());
  1963. }
  1964. public function testFailStrictCookieCheckWithOnlyStrictCookie() {
  1965. /** @var Request $request */
  1966. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1967. ->setMethods(['getScriptName'])
  1968. ->setConstructorArgs([
  1969. [
  1970. 'server' => [
  1971. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1972. ],
  1973. 'cookies' => [
  1974. session_name() => 'asdf',
  1975. 'nc_sameSiteCookiestrict' => 'true',
  1976. ],
  1977. ],
  1978. $this->requestId,
  1979. $this->config,
  1980. $this->csrfTokenManager,
  1981. $this->stream
  1982. ])
  1983. ->getMock();
  1984. $this->assertFalse($request->passesStrictCookieCheck());
  1985. }
  1986. public function testPassesLaxCookieCheck() {
  1987. /** @var Request $request */
  1988. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  1989. ->setMethods(['getScriptName'])
  1990. ->setConstructorArgs([
  1991. [
  1992. 'server' => [
  1993. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  1994. ],
  1995. 'cookies' => [
  1996. session_name() => 'asdf',
  1997. 'nc_sameSiteCookielax' => 'true',
  1998. ],
  1999. ],
  2000. $this->requestId,
  2001. $this->config,
  2002. $this->csrfTokenManager,
  2003. $this->stream
  2004. ])
  2005. ->getMock();
  2006. $this->assertTrue($request->passesLaxCookieCheck());
  2007. }
  2008. public function testFailsLaxCookieCheckWithOnlyStrictCookie() {
  2009. /** @var Request $request */
  2010. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  2011. ->setMethods(['getScriptName'])
  2012. ->setConstructorArgs([
  2013. [
  2014. 'server' => [
  2015. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  2016. ],
  2017. 'cookies' => [
  2018. session_name() => 'asdf',
  2019. 'nc_sameSiteCookiestrict' => 'true',
  2020. ],
  2021. ],
  2022. $this->requestId,
  2023. $this->config,
  2024. $this->csrfTokenManager,
  2025. $this->stream
  2026. ])
  2027. ->getMock();
  2028. $this->assertFalse($request->passesLaxCookieCheck());
  2029. }
  2030. public function testSkipCookieCheckForOCSRequests() {
  2031. /** @var Request $request */
  2032. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  2033. ->setMethods(['getScriptName'])
  2034. ->setConstructorArgs([
  2035. [
  2036. 'server' => [
  2037. 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
  2038. 'HTTP_OCS_APIREQUEST' => 'true',
  2039. ],
  2040. 'cookies' => [
  2041. session_name() => 'asdf',
  2042. 'nc_sameSiteCookiestrict' => 'false',
  2043. ],
  2044. ],
  2045. $this->requestId,
  2046. $this->config,
  2047. $this->csrfTokenManager,
  2048. $this->stream
  2049. ])
  2050. ->getMock();
  2051. $this->assertTrue($request->passesStrictCookieCheck());
  2052. }
  2053. /**
  2054. * @return array
  2055. */
  2056. public function invalidTokenDataProvider() {
  2057. return [
  2058. ['InvalidSentToken'],
  2059. ['InvalidSentToken:InvalidSecret'],
  2060. [''],
  2061. ];
  2062. }
  2063. /**
  2064. * @dataProvider invalidTokenDataProvider
  2065. * @param string $invalidToken
  2066. */
  2067. public function testPassesCSRFCheckWithInvalidToken($invalidToken) {
  2068. /** @var Request $request */
  2069. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  2070. ->setMethods(['getScriptName'])
  2071. ->setConstructorArgs([
  2072. [
  2073. 'server' => [
  2074. 'HTTP_REQUESTTOKEN' => $invalidToken,
  2075. ],
  2076. ],
  2077. $this->requestId,
  2078. $this->config,
  2079. $this->csrfTokenManager,
  2080. $this->stream
  2081. ])
  2082. ->getMock();
  2083. $token = new CsrfToken($invalidToken);
  2084. $this->csrfTokenManager
  2085. ->expects($this->any())
  2086. ->method('isTokenValid')
  2087. ->with($token)
  2088. ->willReturn(false);
  2089. $this->assertFalse($request->passesCSRFCheck());
  2090. }
  2091. public function testPassesCSRFCheckWithoutTokenFail() {
  2092. /** @var Request $request */
  2093. $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
  2094. ->setMethods(['getScriptName'])
  2095. ->setConstructorArgs([
  2096. [],
  2097. $this->requestId,
  2098. $this->config,
  2099. $this->csrfTokenManager,
  2100. $this->stream
  2101. ])
  2102. ->getMock();
  2103. $this->assertFalse($request->passesCSRFCheck());
  2104. }
  2105. }