RequestTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\Request;
  4. class RequestTest extends TestCase
  5. {
  6. public function reset()
  7. {
  8. $_SERVER = [];
  9. $_GET = [];
  10. $_POST = [];
  11. }
  12. /**
  13. * Returns random query safe characters.
  14. *
  15. * @access public
  16. * @return string
  17. */
  18. public function getRandomQueryChars()
  19. {
  20. $queryChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=-_.!~*()';
  21. $queryCharCount = strlen($queryChars) - 1;
  22. $resultLength = random_int(1, 10);
  23. $result = '';
  24. for ($i = 0; $i < $resultLength; ++$i) {
  25. $result .= $queryChars[random_int(0, $queryCharCount)];
  26. }
  27. return $result;
  28. }
  29. public function testView()
  30. {
  31. $this->reset();
  32. $_SERVER['REQUEST_METHOD'] = 'GET';
  33. $request = new Request;
  34. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  35. $this->assertEquals('view', $request->getOperation());
  36. }
  37. /**
  38. * @dataProvider provideRequestUris
  39. */
  40. public function testRequestUriValidation($uri, $expected)
  41. {
  42. $this->reset();
  43. if ($uri !== null) {
  44. $_SERVER['REQUEST_URI'] = $uri;
  45. }
  46. $request = new Request;
  47. $this->assertSame($expected, $request->getRequestUri());
  48. }
  49. public function provideRequestUris()
  50. {
  51. return [
  52. [null, '/'],
  53. ['/', '/'],
  54. ['/path/to/privatebin?query=value', '/path/to/privatebin'],
  55. ['?query=value', '/'],
  56. ['http://[', '/'],
  57. ];
  58. }
  59. public function testRead()
  60. {
  61. $this->reset();
  62. $id = Helper::getRandomId();
  63. $_SERVER['REQUEST_METHOD'] = 'GET';
  64. $_SERVER['QUERY_STRING'] = $id;
  65. $_GET[$id] = '';
  66. $request = new Request;
  67. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  68. $this->assertEquals($id, $request->getParam('pasteid'));
  69. $this->assertEquals('read', $request->getOperation());
  70. }
  71. /**
  72. * paste IDs are 8 bytes hex encoded strings, if unlucky, this turns into
  73. * a numeric string that PHP will cast to an int, for example in array keys
  74. * @see https://www.php.net/manual/en/language.types.array.php
  75. */
  76. public function testReadNumeric()
  77. {
  78. $this->reset();
  79. $id = '1234567812345678';
  80. $_SERVER['REQUEST_METHOD'] = 'GET';
  81. $_SERVER['QUERY_STRING'] = $id;
  82. $_GET[$id] = '';
  83. $request = new Request;
  84. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  85. $this->assertEquals($id, $request->getParam('pasteid'));
  86. $this->assertEquals('read', $request->getOperation());
  87. }
  88. public function testDelete()
  89. {
  90. $this->reset();
  91. $id = Helper::getRandomId();
  92. $_SERVER['REQUEST_METHOD'] = 'GET';
  93. $_GET['pasteid'] = $id;
  94. $_GET['deletetoken'] = 'bar';
  95. $request = new Request;
  96. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  97. $this->assertEquals('delete', $request->getOperation());
  98. $this->assertEquals($id, $request->getParam('pasteid'));
  99. $this->assertEquals('bar', $request->getParam('deletetoken'));
  100. }
  101. public function testApiCreate()
  102. {
  103. $this->reset();
  104. $_SERVER['REQUEST_METHOD'] = 'PUT';
  105. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  106. $file = Helper::createTempFile();
  107. file_put_contents($file, '{"ct":"foo"}');
  108. Request::setInputStream($file);
  109. $request = new Request;
  110. unlink($file);
  111. $this->assertTrue($request->isJsonApiCall(), 'is JSON API call');
  112. $this->assertEquals('create', $request->getOperation());
  113. $this->assertEquals('foo', $request->getParam('ct'));
  114. }
  115. public function testApiCreateAlternative()
  116. {
  117. $this->reset();
  118. $_SERVER['REQUEST_METHOD'] = 'POST';
  119. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  120. $file = Helper::createTempFile();
  121. file_put_contents($file, '{"ct":"foo"}');
  122. Request::setInputStream($file);
  123. $request = new Request;
  124. $this->assertTrue($request->isJsonApiCall(), 'is JSON API call');
  125. $this->assertEquals('create', $request->getOperation());
  126. $this->assertEquals('foo', $request->getParam('ct'));
  127. }
  128. public function testApiRead()
  129. {
  130. $this->reset();
  131. $id = Helper::getRandomId();
  132. $_SERVER['REQUEST_METHOD'] = 'GET';
  133. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  134. $_SERVER['QUERY_STRING'] = $id;
  135. $_GET[$id] = '';
  136. $request = new Request;
  137. $this->assertTrue($request->isJsonApiCall(), 'is JSON API call');
  138. $this->assertEquals($id, $request->getParam('pasteid'));
  139. $this->assertEquals('read', $request->getOperation());
  140. }
  141. public function testApiDelete()
  142. {
  143. $this->reset();
  144. $id = Helper::getRandomId();
  145. $_SERVER['REQUEST_METHOD'] = 'POST';
  146. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  147. $_SERVER['QUERY_STRING'] = $id;
  148. $_GET = [$id => ''];
  149. $file = Helper::createTempFile();
  150. file_put_contents($file, '{"deletetoken":"bar"}');
  151. Request::setInputStream($file);
  152. $request = new Request;
  153. $this->assertTrue($request->isJsonApiCall(), 'is JSON API call');
  154. $this->assertEquals('delete', $request->getOperation());
  155. $this->assertEquals($id, $request->getParam('pasteid'));
  156. $this->assertEquals('bar', $request->getParam('deletetoken'));
  157. }
  158. public function testPostGarbage()
  159. {
  160. $this->reset();
  161. $_SERVER['REQUEST_METHOD'] = 'POST';
  162. $file = Helper::createTempFile();
  163. file_put_contents($file, random_bytes(256));
  164. Request::setInputStream($file);
  165. $request = new Request;
  166. unlink($file);
  167. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  168. $this->assertEquals('create', $request->getOperation());
  169. }
  170. public function testPostScalarJson()
  171. {
  172. // a valid JSON scalar body (number, boolean or quoted string) must not
  173. // be treated as a set of parameters, so that it is rejected cleanly
  174. // instead of triggering a type error further down
  175. foreach (['1', '0.0', 'true', 'false', '"example"'] as $scalar) {
  176. $this->reset();
  177. $_SERVER['REQUEST_METHOD'] = 'POST';
  178. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  179. $file = Helper::createTempFile();
  180. file_put_contents($file, $scalar);
  181. Request::setInputStream($file);
  182. $request = new Request;
  183. unlink($file);
  184. $this->assertTrue($request->isJsonApiCall(), 'is JSON API call');
  185. $this->assertEquals('create', $request->getOperation());
  186. $this->assertEquals('', $request->getParam('ct'), "scalar body {$scalar} yields no parameters");
  187. }
  188. }
  189. /**
  190. * @dataProvider provideInvalidRoutingParameters
  191. */
  192. public function testPostInvalidRoutingParameters($parameters)
  193. {
  194. $this->reset();
  195. $_SERVER['REQUEST_METHOD'] = 'POST';
  196. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  197. $file = Helper::createTempFile();
  198. file_put_contents($file, json_encode($parameters));
  199. Request::setInputStream($file);
  200. $request = new Request;
  201. unlink($file);
  202. $this->assertSame('create', $request->getOperation());
  203. }
  204. public function provideInvalidRoutingParameters()
  205. {
  206. return [
  207. [['pasteid' => '0123456789abcdef', 'deletetoken' => ['invalid']]],
  208. [['pasteid' => '0123456789abcdef', 'deletetoken' => 123]],
  209. [['pasteid' => ['0123456789abcdef'], 'deletetoken' => 'invalid']],
  210. [['jsonld' => ['paste']]],
  211. [['link' => ['https://example.com/'], 'shortenviayourls' => true]],
  212. [['link' => 123, 'shortenviashlink' => true]],
  213. ];
  214. }
  215. public function testReadWithNegotiation()
  216. {
  217. $this->reset();
  218. $id = Helper::getRandomId();
  219. $_SERVER['REQUEST_METHOD'] = 'GET';
  220. $_SERVER['HTTP_ACCEPT'] = 'text/html,text/html; charset=UTF-8,application/xhtml+xml, application/xml;q=0.9,*/*;q=0.8, text/csv,application/json';
  221. $_SERVER['QUERY_STRING'] = $id;
  222. $_GET[$id] = '';
  223. $request = new Request;
  224. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  225. $this->assertEquals($id, $request->getParam('pasteid'));
  226. $this->assertEquals('read', $request->getOperation());
  227. }
  228. public function testReadWithXhtmlNegotiation()
  229. {
  230. $this->reset();
  231. $id = Helper::getRandomId();
  232. $_SERVER['REQUEST_METHOD'] = 'GET';
  233. $_SERVER['HTTP_ACCEPT'] = 'application/xhtml+xml,text/html,text/html; charset=UTF-8, application/xml;q=0.9,*/*;q=0.8, text/csv,application/json';
  234. $_SERVER['QUERY_STRING'] = $id;
  235. $_GET[$id] = '';
  236. $request = new Request;
  237. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  238. $this->assertEquals($id, $request->getParam('pasteid'));
  239. $this->assertEquals('read', $request->getOperation());
  240. }
  241. public function testApiReadWithNegotiation()
  242. {
  243. $this->reset();
  244. $id = Helper::getRandomId();
  245. $_SERVER['REQUEST_METHOD'] = 'GET';
  246. $_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, application/json, text/html,text/html; charset=UTF-8,application/xhtml+xml, */*;q=0.8';
  247. $_SERVER['QUERY_STRING'] = $id;
  248. $_GET[$id] = '';
  249. $request = new Request;
  250. $this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
  251. $this->assertEquals($id, $request->getParam('pasteid'));
  252. $this->assertEquals('read', $request->getOperation());
  253. }
  254. public function testReadWithFailedNegotiation()
  255. {
  256. $this->reset();
  257. $id = Helper::getRandomId();
  258. $_SERVER['REQUEST_METHOD'] = 'GET';
  259. $_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
  260. $_SERVER['QUERY_STRING'] = $id;
  261. $_GET[$id] = '';
  262. $request = new Request;
  263. $this->assertFalse($request->isJsonApiCall(), 'is HTML call');
  264. $this->assertEquals($id, $request->getParam('pasteid'));
  265. $this->assertEquals('read', $request->getOperation());
  266. }
  267. public function testPasteIdExtraction()
  268. {
  269. $this->reset();
  270. $id = Helper::getRandomId();
  271. $path = '/' . $this->getRandomQueryChars() . '/';
  272. $queryParams = [$id];
  273. $queryParamCount = random_int(1, 5);
  274. for ($i = 0; $i < $queryParamCount; ++$i) {
  275. array_push($queryParams, $this->getRandomQueryChars());
  276. }
  277. shuffle($queryParams);
  278. $_SERVER['REQUEST_METHOD'] = 'GET';
  279. $_SERVER['QUERY_STRING'] = implode('&', $queryParams);
  280. $_SERVER['REQUEST_URI'] = $path . '?' . $_SERVER['QUERY_STRING'];
  281. $_GET[$id] = '';
  282. $request = new Request;
  283. $this->assertEquals($id, $request->getParam('pasteid'));
  284. $this->assertEquals($path, $request->getRequestUri());
  285. }
  286. }