raqibo 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +12 -0
  2. package/package.json +11 -2
  3. package/src/cli.js +49 -5
package/README.md CHANGED
@@ -44,6 +44,18 @@ raqibo monitors apply monitors.yaml --prune # and delete monitors the file doe
44
44
  The file holds alert destinations, so keep it private. Credentials for the sites checked are
45
45
  never in it.
46
46
 
47
+ ## More
48
+
49
+ - [Command-line documentation](https://raqibo.com/docs/cli) and the [API reference](https://raqibo.com/docs/api)
50
+ - The same checks in a browser, with no account: [raqibo.com/tools](https://raqibo.com/tools) -
51
+ [SSL certificate checker](https://raqibo.com/tools/ssl-certificate-inspector),
52
+ [DNS lookup](https://raqibo.com/tools/dns-lookup),
53
+ [email security checker](https://raqibo.com/tools/email-dns-checker),
54
+ [broken link checker](https://raqibo.com/tools/broken-link-checker) and more
55
+ - Have them run on a schedule and be told when something breaks:
56
+ [website monitoring](https://raqibo.com/monitoring) - uptime, certificates, domain expiry, DNS,
57
+ page changes and [cron jobs](https://raqibo.com/monitoring/cron-job-monitoring). Free to start.
58
+
47
59
  ## Development
48
60
 
49
61
  ```sh
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "raqibo",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Website checks from a server, for terminals and CI: uptime, redirects, TLS, headers, domain, email security, broken links, robots.txt - and monitors as a file.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -22,10 +22,19 @@
22
22
  "keywords": [
23
23
  "monitoring",
24
24
  "uptime",
25
+ "uptime-monitoring",
26
+ "website-monitoring",
25
27
  "ssl",
28
+ "ssl-certificate",
29
+ "domain-expiry",
30
+ "dns",
31
+ "spf",
32
+ "dmarc",
26
33
  "security-headers",
27
34
  "broken-links",
28
- "ci"
35
+ "robots-txt",
36
+ "ci",
37
+ "cli"
29
38
  ],
30
39
  "license": "MIT"
31
40
  }
package/src/cli.js CHANGED
@@ -10,7 +10,7 @@
10
10
  import { readFile, writeFile } from "node:fs/promises";
11
11
  import { parseArgs } from "node:util";
12
12
 
13
- export const VERSION = "0.1.0";
13
+ export const VERSION = "0.1.2";
14
14
  export const DEFAULT_API = "https://api.raqibo.com";
15
15
 
16
16
  export const PASS = 0;
@@ -64,16 +64,40 @@ Options:
64
64
  -v, --version The version
65
65
  `;
66
66
 
67
+ /** An API key goes only over https, or to this machine: never in clear text to a network. */
68
+ export function safeForKey(api) {
69
+ try {
70
+ const url = new URL(api);
71
+ return url.protocol === "https:" || ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
72
+ } catch {
73
+ return false;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Text from a checked site, made safe to print: a page's own address or a server's message can
79
+ * carry terminal control sequences, which in a build log could hide a failure or fake an "OK".
80
+ */
81
+ export function printable(text) {
82
+ // eslint-disable-next-line no-control-regex
83
+ return String(text).replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g, "");
84
+ }
85
+
67
86
  /** POST/GET to the API; resolves to the parsed body or rejects with ApiError. */
68
87
  export async function call(env, path, { method = "GET", body, key, text = false } = {}) {
69
88
  const headers = { "User-Agent": `raqibo-cli/${VERSION}`, Accept: text ? "application/yaml" : "application/json" };
70
89
  if (body !== undefined) headers["Content-Type"] = "application/json";
71
90
  if (key) headers["X-Api-Key"] = key;
91
+ if (key && !safeForKey(env.api)) {
92
+ throw new ApiError(0, "insecure_api", `Refusing to send the API key to ${env.api}: use an https address.`);
93
+ }
72
94
  let response;
73
95
  try {
74
96
  response = await env.fetch(`${env.api}${path}`, {
75
97
  method,
76
98
  headers,
99
+ // With a key, a redirect is an error: the key must not follow one to another host.
100
+ redirect: key ? "error" : "follow",
77
101
  body: body === undefined ? undefined : JSON.stringify(body),
78
102
  });
79
103
  } catch (cause) {
@@ -98,6 +122,15 @@ export async function call(env, path, { method = "GET", body, key, text = false
98
122
  return text ? response.text() : response.json();
99
123
  }
100
124
 
125
+ /** Whether two addresses are the same once written the standard way (a bare host gets its "/"). */
126
+ function sameUrl(a, b) {
127
+ try {
128
+ return new URL(a).href === new URL(b).href;
129
+ } catch {
130
+ return a === b;
131
+ }
132
+ }
133
+
101
134
  function gradeAtLeast(grade, minimum) {
102
135
  return GRADES.indexOf(grade) <= GRADES.indexOf(minimum);
103
136
  }
@@ -142,7 +175,7 @@ const CHECKS = {
142
175
  const most = checkNumber(options["max-redirects"], "--max-redirects");
143
176
  if (answer.redirect_count > most) failures.push(`${answer.redirect_count} redirects; at most ${most} allowed.`);
144
177
  }
145
- if (options.expect !== undefined && answer.final_url !== options.expect) {
178
+ if (options.expect !== undefined && sameUrl(answer.final_url, options.expect) === false) {
146
179
  failures.push(`Ends at ${answer.final_url}, not ${options.expect}.`);
147
180
  }
148
181
  if (answer.downgrades_to_http) failures.push("The chain drops from https to http on the way.");
@@ -208,6 +241,14 @@ const CHECKS = {
208
241
 
209
242
  async links(env, target) {
210
243
  const answer = await call(env, "/v1/tools/broken-links", { method: "POST", body: { url: need(target, "a page") } });
244
+ if (answer.page_status < 200 || answer.page_status >= 300) {
245
+ // The page did not load, so there were no links to check: that is a failure, not a pass.
246
+ return {
247
+ code: FAIL,
248
+ lines: [`FAIL the page itself answered HTTP ${answer.page_status} ${answer.page_url}`],
249
+ answer,
250
+ };
251
+ }
211
252
  const bad = answer.links.filter((l) => l.state === "broken" || l.state === "unreachable");
212
253
  const lines = [
213
254
  `${bad.length ? "FAIL" : "OK"} ${bad.length} broken of ${answer.checked} checked (${answer.found} found) ${answer.page_url}`,
@@ -309,7 +350,7 @@ export async function main(argv, env) {
309
350
  else if (Object.hasOwn(CHECKS, command)) result = await CHECKS[command](run, rest[0], options);
310
351
  else throw new UsageError(`Unknown command "${command}". See raqibo --help.`);
311
352
  if (options.json && result.answer !== undefined) env.out(JSON.stringify(result.answer, null, 2));
312
- else env.out(result.lines.filter((line) => line !== null).join("\n"));
353
+ else env.out(printable(result.lines.filter((line) => line !== null).join("\n")));
313
354
  return result.code;
314
355
  } catch (error) {
315
356
  if (error instanceof UsageError) {
@@ -318,9 +359,12 @@ export async function main(argv, env) {
318
359
  }
319
360
  if (error instanceof ApiError) {
320
361
  const wait = error.retryAfter ? ` Try again in ${error.retryAfter} s.` : "";
321
- env.err(`Could not check: ${error.message}${wait}`);
362
+ env.err(printable(`Could not check: ${error.message}${wait}`));
322
363
  return ERROR;
323
364
  }
324
- throw error;
365
+ // Anything else - a file that is not there, an answer that is not JSON - is also "could
366
+ // not check", never the 1 that means the site failed.
367
+ env.err(printable(`Could not run: ${error?.message ?? error}`));
368
+ return ERROR;
325
369
  }
326
370
  }