@metric-im/administrate 2.1.1 → 2.1.4

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.
@@ -1,10 +1,20 @@
1
1
  {
2
2
  "permissions": {
3
3
  "allow": [
4
- "Read(//home/msprague/workspace/sitewell/**)",
5
- "Read(//home/msprague/workspace/rootz/data-wallet-demos/**)"
4
+ "Read(//home/msprague/workspace/rootz/epistery/docs/**)",
5
+ "Read(//home/msprague/workspace/rootz/epistery/src/**)",
6
+ "Read(//home/msprague/workspace/rootz/epistery/**)",
7
+ "Read(//home/msprague/workspace/rootz/rhonda/node_modules/epistery/**)",
8
+ "Read(//home/msprague/workspace/rootz/rhonda/**)",
9
+ "Read(//home/msprague/workspace/metric-im/account-control/**)",
10
+ "Bash(sed:*)",
11
+ "Bash(git checkout:*)",
12
+ "Bash(npm view *)",
13
+ "Bash(git --no-pager diff --stat)",
14
+ "Bash(node -p \"require\\('./package-lock.json'\\).packages['node_modules/epistery'].version\")",
15
+ "Bash(git *)"
6
16
  ],
7
17
  "deny": [],
8
18
  "ask": []
9
19
  }
10
- }
20
+ }
package/certify.mjs CHANGED
@@ -3,6 +3,9 @@ import Acme from 'acme-client';
3
3
  import tls from 'tls';
4
4
  import { Config } from 'epistery';
5
5
  import moment from 'moment';
6
+ import fs from 'fs';
7
+ import { resolve } from 'path';
8
+ import { Site } from './multisite.mjs';
6
9
 
7
10
  const MAX_AGE = 75; // days
8
11
  const MAX_WAIT_TIME = 60; // seconds
@@ -42,6 +45,14 @@ export class Certify {
42
45
  app.use('/',instance.routes());
43
46
  return instance;
44
47
  }
48
+ // A name is certifiable when it parses as a hostname and, where a sites
49
+ // folder declares what this front publishes, is one of those sites.
50
+ static WillCertify(sitename) {
51
+ if (Site.WashName(sitename) !== String(sitename).toLowerCase()) return false;
52
+ const root = resolve('./sites');
53
+ if (!fs.existsSync(root)) return true; // standalone Certify — nothing to check against
54
+ return !!Site.Dir(sitename);
55
+ }
45
56
  get SNI() {
46
57
  return {SNICallback: async (hostname, cb) => {
47
58
  try {
@@ -128,6 +139,12 @@ export class Certify {
128
139
  if (sitename === 'localhost' || sitename.includes('.local') || sitename.match(/^\d+\.\d+\.\d+\.\d+$/)) {
129
140
  return null; // Use default certificate
130
141
  }
142
+ // The SNI name comes off the wire, so it decides a Config path and an ACME
143
+ // order. Neither should be reachable by an arbitrary string: a junk name
144
+ // spends a failed authorization, and enough of those exhaust the account's
145
+ // rate limit and block renewal for every real domain. Mint only for a
146
+ // hostname we actually publish.
147
+ if (!Certify.WillCertify(sitename)) return null;
131
148
 
132
149
  // Load domain-specific config to check SSL section
133
150
  await this.config.setPath(`/${sitename}`);
package/multisite.mjs CHANGED
@@ -4,7 +4,7 @@ import tls from 'tls';
4
4
  import { Config } from 'epistery';
5
5
  import moment from 'moment';
6
6
  import fs from "fs";
7
- import {resolve} from "path";
7
+ import {resolve, sep} from "path";
8
8
  import child_process from "child_process";
9
9
  import axios from 'axios';
10
10
 
@@ -157,7 +157,12 @@ export class MultiSite {
157
157
  }
158
158
 
159
159
  // Create new site
160
- this.sites[domain] = Site.Clone(domain, this);
160
+ const site = Site.Clone(domain, this);
161
+ if (!site) {
162
+ delete this.sites[domain];
163
+ return;
164
+ }
165
+ this.sites[domain] = site;
161
166
  console.log(`Restarted site for domain: ${domain}`);
162
167
  }
163
168
 
@@ -221,8 +226,13 @@ export class MultiSite {
221
226
  if (fs.existsSync(resolve('./sites'))) {
222
227
  instance.sites = (fs.readdirSync(resolve('./sites'))).reduce((result,hostName)=>{
223
228
  const domainName = Site.WashName(hostName);
229
+ const dir = Site.Dir(domainName);
230
+ if (!dir) {
231
+ console.error(`Ignoring sites/${hostName}: not a usable domain name`);
232
+ return result;
233
+ }
224
234
  const options = {
225
- cwd: resolve(`./sites/${hostName}`),
235
+ cwd: dir,
226
236
  env: {PORT:instance.spawnPort,meta:instance.options}
227
237
  };
228
238
  result[domainName] = Site.Spawn(domainName,options,instance);
@@ -329,6 +339,11 @@ export class MultiSite {
329
339
 
330
340
  res.status(502).send('Bad Gateway. Try reloading.');
331
341
  }
342
+ } else if (!Site.Dir(domain)) {
343
+ // No sites/<domain> — nothing is published here. Say so and stop.
344
+ // Spawning from an unmatched Host is what let a request choose
345
+ // the directory npm ran in.
346
+ res.status(404).send('Not found');
332
347
  } else {
333
348
  // Check if already spawning to prevent race condition
334
349
  if (this.spawning.has(domain)) {
@@ -339,7 +354,12 @@ export class MultiSite {
339
354
  } else {
340
355
  // Mark as spawning
341
356
  this.spawning.add(domain);
342
- this.sites[domain] = Site.Clone(domain,this);
357
+ const site = Site.Clone(domain,this);
358
+ if (!site) {
359
+ this.spawning.delete(domain);
360
+ return res.status(404).send('Not found');
361
+ }
362
+ this.sites[domain] = site;
343
363
  // Remove from spawning set after spawn completes
344
364
  setTimeout(() => {
345
365
  this.spawning.delete(domain);
@@ -372,12 +392,17 @@ export class Site {
372
392
  return instance;
373
393
  }
374
394
  static Clone(name, multisite) {
395
+ // The site runs from its own sites/<name> dir — same as the startup
396
+ // path. Using process.cwd() (the harness dir) here spawned the child
397
+ // in the wrong directory, so restartSite() and add-a-site-after-boot
398
+ // never launched the actual site.
399
+ const dir = Site.Dir(name);
400
+ if (!dir) {
401
+ console.error(`Refusing to spawn ${name}: no sites/${name} directory`);
402
+ return null;
403
+ }
375
404
  const options = {
376
- // The site runs from its own sites/<name> dir — same as the startup
377
- // path. Using process.cwd() (the harness dir) here spawned the child
378
- // in the wrong directory, so restartSite() and add-a-site-after-boot
379
- // never launched the actual site.
380
- cwd: resolve(`./sites/${name}`),
405
+ cwd: dir,
381
406
  env: {PORT:multisite.spawnPort,meta:multisite.options}
382
407
  };
383
408
  const instance = new Site(name,options);
@@ -443,9 +468,29 @@ export class Site {
443
468
  this.proc = null;
444
469
  }
445
470
  }
471
+ // A Host header is a hostname or it is nothing. A bare IP, an IPv6 literal,
472
+ // an absent Host, or a header carrying path syntax are all not site names,
473
+ // and "" is what keeps them out of `sites/${name}`. Without this the Host
474
+ // header picks the directory that `npm run start` runs in.
446
475
  static WashName(hostName="") {
447
- if (!hostName || hostName.match(/^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/)) return "";
448
- else return hostName.toLowerCase();
476
+ const name = String(hostName).toLowerCase();
477
+ if (!name || name.length > 253) return "";
478
+ if (name.match(/^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/)) return "";
479
+ const label = '[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?';
480
+ if (!name.match(new RegExp(`^${label}(?:\\.${label})*$`))) return "";
481
+ return name;
482
+ }
483
+
484
+ // Where a site runs from. Null unless sites/<name> is a real entry that
485
+ // stays inside the sites folder: a site exists because someone symlinked
486
+ // it, never because a request asked for it.
487
+ static Dir(name) {
488
+ const washed = Site.WashName(name);
489
+ if (!washed) return null;
490
+ const root = resolve('./sites');
491
+ const dir = resolve(root, washed);
492
+ if (dir === root || !dir.startsWith(root + sep)) return null;
493
+ return fs.existsSync(dir) ? dir : null;
449
494
  }
450
495
  static SafeName(hostName) {
451
496
  return Site.WashName(hostName).replace(/[^a-z0-9-]+/g,'_');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metric-im/administrate",
3
- "version": "2.1.1",
3
+ "version": "2.1.4",
4
4
  "description": "Tools for site administration",
5
5
  "homepage": "https://github.com/metric-im/administrate#readme",
6
6
  "bugs": {
@@ -22,7 +22,7 @@
22
22
  "acme-client": "^5.4.0",
23
23
  "axios": "^1.7.0",
24
24
  "compression": "^1.8.1",
25
- "epistery": "2.2",
25
+ "epistery": "^2.2.11",
26
26
  "express": "^5.1.0",
27
27
  "moment": "^2.30.1"
28
28
  }