4
0

crash.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // POST https://lynx-share.com/api/crash.php — receives one crash / freeze report from LYNX (opt-in, see PRIVACY.md).
  3. // Body: the report JSON, gzip-compressed (Content-Encoding: gzip) or plain. Answers {"ok":true,"id":"<report id>"}.
  4. // The sender's address is never stored: a salted hash of it, valid for one day, only counts reports for the limit.
  5. declare(strict_types=1);
  6. require __DIR__ . '/_common.php';
  7. if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
  8. header('Allow: POST');
  9. lynx_fail(405, 'POST only');
  10. }
  11. $length = (int) ($_SERVER['CONTENT_LENGTH'] ?? 0);
  12. if ($length <= 0) {
  13. lynx_fail(411, 'empty body');
  14. }
  15. if ($length > LYNX_MAX_RAW) {
  16. lynx_fail(413, 'too large');
  17. }
  18. $body = file_get_contents('php://input', false, null, 0, LYNX_MAX_RAW + 1);
  19. if ($body === false || $body === '') {
  20. lynx_fail(400, 'empty body');
  21. }
  22. // gzip (magic 1f 8b) or plain JSON; the decoded size is checked while inflating (no zip bombs)
  23. if (strncmp($body, "\x1f\x8b", 2) === 0) {
  24. if (strlen($body) > LYNX_MAX_GZIP) {
  25. lynx_fail(413, 'too large');
  26. }
  27. $ctx = inflate_init(ZLIB_ENCODING_GZIP);
  28. $json = '';
  29. foreach (str_split($body, 65536) as $chunk) {
  30. $out = @inflate_add($ctx, $chunk, ZLIB_SYNC_FLUSH);
  31. if ($out === false) {
  32. lynx_fail(400, 'bad gzip');
  33. }
  34. $json .= $out;
  35. if (strlen($json) > LYNX_MAX_RAW) {
  36. lynx_fail(413, 'too large');
  37. }
  38. }
  39. } else {
  40. if (strlen($body) > LYNX_MAX_RAW) {
  41. lynx_fail(413, 'too large');
  42. }
  43. $json = $body;
  44. }
  45. $report = json_decode($json, true, 32);
  46. if (!is_array($report)) {
  47. lynx_fail(400, 'not JSON');
  48. }
  49. // ---- schema 1 -------------------------------------------------------------------------------------------------
  50. $allowed = ['schema', 'id', 'kind', 'test', 'created', 'app', 'system', 'audio', 'settings', 'crash', 'hang', 'steps',
  51. 'plugins', 'user', 'notes'];
  52. foreach (array_keys($report) as $key) {
  53. if (!in_array($key, $allowed, true)) {
  54. lynx_fail(400, 'unknown field');
  55. }
  56. }
  57. if (($report['schema'] ?? null) !== 1) {
  58. lynx_fail(400, 'unsupported schema');
  59. }
  60. $id = $report['id'] ?? '';
  61. if (!is_string($id) || !lynx_valid_id($id)) {
  62. lynx_fail(400, 'bad id');
  63. }
  64. if (!in_array($report['kind'] ?? '', ['crash', 'hang', 'unclean', 'manual'], true)) {
  65. lynx_fail(400, 'bad kind');
  66. }
  67. if (isset($report['test']) && !is_bool($report['test'])) {
  68. lynx_fail(400, 'bad test flag');
  69. }
  70. $app = $report['app'] ?? null;
  71. if (!is_array($app) || !is_string($app['version'] ?? null) || !preg_match('/^[0-9A-Za-z.+_ -]{1,40}$/', $app['version'])) {
  72. lynx_fail(400, 'bad version');
  73. }
  74. foreach (['build', 'commit'] as $k) {
  75. if (isset($app[$k]) && (!is_string($app[$k]) || !preg_match('/^[0-9A-Za-z.+_: -]{0,64}$/', $app[$k]))) {
  76. lynx_fail(400, 'bad ' . $k);
  77. }
  78. }
  79. foreach (['system', 'audio', 'settings', 'crash', 'hang', 'user'] as $k) {
  80. if (isset($report[$k]) && !is_array($report[$k])) {
  81. lynx_fail(400, 'bad ' . $k);
  82. }
  83. }
  84. foreach (['steps', 'plugins'] as $k) {
  85. if (isset($report[$k]) && (!is_array($report[$k]) || !array_is_list($report[$k]) || count($report[$k]) > 500)) {
  86. lynx_fail(400, 'bad ' . $k);
  87. }
  88. }
  89. // ---- limits (per hashed address and day, and for everyone) ----------------------------------------------------
  90. $dir = lynx_storage_dir();
  91. $today = gmdate('Y-m-d');
  92. $ip = $_SERVER['REMOTE_ADDR'] ?? '';
  93. foreach (['HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR'] as $h) { // (behind the host's CDN)
  94. if (!empty($_SERVER[$h])) {
  95. $ip = trim(explode(',', (string) $_SERVER[$h])[0]);
  96. break;
  97. }
  98. }
  99. $who = substr(hash_hmac('sha256', $today . '|' . $ip, lynx_salt()), 0, 32);
  100. unset($ip);
  101. @mkdir($dir . '/rate', 0700, true);
  102. $rateFile = $dir . '/rate/' . $today . '.json';
  103. $fh = fopen($rateFile, 'c+');
  104. if ($fh === false || !flock($fh, LOCK_EX)) {
  105. lynx_fail(503, 'busy');
  106. }
  107. $counts = json_decode((string) stream_get_contents($fh), true) ?: [];
  108. $total = array_sum($counts);
  109. if (($counts[$who] ?? 0) >= LYNX_PER_IP_PER_DAY || $total >= LYNX_PER_DAY) {
  110. flock($fh, LOCK_UN);
  111. fclose($fh);
  112. header('Retry-After: 86400');
  113. lynx_fail(429, 'too many reports today');
  114. }
  115. // ---- store ----------------------------------------------------------------------------------------------------
  116. $month = gmdate('Y-m');
  117. $monthDir = $dir . '/' . $month;
  118. if (!is_dir($monthDir) && !@mkdir($monthDir, 0700, true)) {
  119. lynx_fail(503, 'storage unavailable');
  120. }
  121. $file = $monthDir . '/' . $id . '.json.gz';
  122. if (!file_exists($file)) { // the same report sent again (a lost answer) is stored once
  123. if (count(glob($monthDir . '/*.json.gz') ?: []) >= LYNX_MAX_PER_MONTH) {
  124. lynx_fail(507, 'storage full');
  125. }
  126. $report['received'] = gmdate('c');
  127. $data = gzencode(json_encode($report, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE), 9);
  128. if ($data === false || file_put_contents($file . '.tmp', $data, LOCK_EX) === false || !rename($file . '.tmp', $file)) {
  129. lynx_fail(503, 'storage unavailable');
  130. }
  131. @chmod($file, 0600);
  132. $summary = [
  133. 'id' => $id,
  134. 'month' => $month,
  135. 'received' => $report['received'],
  136. 'kind' => $report['kind'],
  137. 'test' => (bool) ($report['test'] ?? false),
  138. 'version' => $app['version'],
  139. 'bytes' => strlen($data),
  140. ];
  141. file_put_contents($dir . '/index.jsonl', json_encode($summary, JSON_UNESCAPED_SLASHES) . "\n", FILE_APPEND | LOCK_EX);
  142. $counts[$who] = ($counts[$who] ?? 0) + 1;
  143. ftruncate($fh, 0);
  144. rewind($fh);
  145. fwrite($fh, json_encode($counts));
  146. }
  147. flock($fh, LOCK_UN);
  148. fclose($fh);
  149. // yesterday's counters are no longer needed
  150. foreach (glob($dir . '/rate/*.json') ?: [] as $old) {
  151. if (basename($old) !== $today . '.json') {
  152. @unlink($old);
  153. }
  154. }
  155. lynx_json(200, ['ok' => true, 'id' => $id]);