@plotdb/registry 0.0.13 → 0.0.14

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,37 @@
1
1
  # Change Logs
2
2
 
3
+ ## v0.0.14
4
+
5
+ - features:
6
+ - provider `scopes` option: a list of allowed package scopes ( e.g. `["@plotdb"]` ),
7
+ matched before `check` and compared for equality, so no caller has to get a regex
8
+ right. a hand written gate is easy to get wrong in a way that opens the whole
9
+ upstream: an unanchored `/@plotdb/` also accepts `@plotdbx/foo` ( prefix not
10
+ terminated ) and `@evil/plotdb-ish` ( not anchored ), which is enough to get an
11
+ arbitrary npm package served under our own domain. a name whose scope is not listed
12
+ is rejected with 998 ( skip ), so probing random scopes leaves no `.reg.404` files
13
+ behind. README's example used the unanchored form - it now uses `scopes`, and spells
14
+ out `^@plotdb/` for the cases that still need a regex.
15
+ - `check` now also receives the parsed `scope` / `pkg` next to `name` / `version`.
16
+ - `registry.route` / `registry.designate` warn once per provider when their entry
17
+ provider has neither `scopes` nor `check`: everything reachable through its chain
18
+ ( e.g. all of npm ) is then served from this origin. `open: true` on the provider
19
+ declares that as intended and silences it.
20
+ - bug fix:
21
+ - the `X-Accel-Redirect` uri was built from the raw request url, while everything else
22
+ used the rectified one. nginx unescapes that header but does not collapse dot
23
+ segments, so a percent-encoded `..%2f` ( or a double-encoded `%252e%252e%252f` ) that
24
+ survived nginx's own uri normalization would have made the internal redirect read
25
+ files outside the cache root. it is now built from the parsed `name` / `version` /
26
+ `path`, and a path that decodes to anything containing a `..` segment ( or a nul
27
+ byte ) is rejected with 404.
28
+ - `designate` applies the provider's gate itself: a version already on disk takes the
29
+ no-fetch path, which used to repoint `main` without any `check`.
30
+ - tweaks:
31
+ - a version coming back from a provider ( npm `dist-tags.latest`, a custom provider's
32
+ version list ) reaches a redirect url and fs paths, so it is now accepted only if it
33
+ is a plain specific version.
34
+
3
35
  ## v0.0.13
4
36
 
5
37
  - bug fix:
package/README.md CHANGED
@@ -29,14 +29,38 @@ First, include `@plotdb/registry`:
29
29
 
30
30
  prepare a provider:
31
31
 
32
- # our own provider, only fetch modules in @plotdb scope
32
+ # our own provider, only fetch modules in the @plotdb scope
33
33
  myprovider = new registry do
34
- check: ({name, version, path}) ->
35
- if /@plotdb/.exec(name) => return Promise.resolve!
36
- return lderror.reject 403 # or 998 (skipped) to prevent creating reg.404 file
34
+ scopes: ["@plotdb"]
37
35
  # chain a default jsdelivr provider
38
36
  myprovider.chain(registry.provider.jsdelivr);
39
37
 
38
+ `scopes` is the recommended gate: scopes are compared for equality, so lookalike
39
+ scopes cannot slip through. **do not** hand-roll this with a regex -- an unanchored
40
+ one is a hole:
41
+
42
+ check: ({name}) -> if /@plotdb/.exec(name) => ... # WRONG
43
+
44
+ that accepts `@plotdbx/foo` ( prefix not terminated ) and `@evil/plotdb-ish`
45
+ ( not anchored at the start ), which is enough to get an arbitrary package from the
46
+ chained upstream served under your own domain. if you must write it by hand, anchor
47
+ both ends of the scope, including the `/`: `^@plotdb/`. for several scopes:
48
+ `^(?:@plotdb|@makeform)/`.
49
+
50
+ `check` is still available for rules `scopes` cannot express, and receives the parsed
51
+ `scope` / `pkg` so it never has to parse the name itself:
52
+
53
+ myprovider = new registry do
54
+ scopes: ["@plotdb", "@makeform"]
55
+ check: ({scope, pkg, name, version}) ->
56
+ if scope == \@makeform and pkg.startsWith(\private-) => return lderror.reject 403
57
+ Promise.resolve!
58
+
59
+ a provider used as a route's entry point with neither `scopes` nor `check` serves
60
+ everything reachable through its chain ( e.g. all of npm ) from your own origin;
61
+ `registry.route` / `registry.designate` print a warning when they are given one.
62
+ pass `open: true` to the provider if that is intended.
63
+
40
64
  add a route:
41
65
 
42
66
  app.get("/mylib/*", registry.route({
@@ -183,8 +207,19 @@ A registry provider can be either following format:
183
207
  A registry provider object should contain following fields:
184
208
 
185
209
  - `name`: provider name
186
- - `check({name, version})`: access control for the given package `{name, version}`.
187
- - when omitted, no check will be done.
210
+ - `scopes`: ( optional ) a list of allowed package scopes, e.g. `["@plotdb"]`.
211
+ - `@plotdb`, `plotdb` and `@plotdb/` are all accepted and normalized to `@plotdb`.
212
+ - matched before `check`, by equality: a name whose scope is not listed ( including
213
+ any unscoped name ) is rejected with 998 ( skip ), so probing random scopes does
214
+ not leave `.reg.404` files behind in the cache dir.
215
+ - `open`: ( optional ) when true, declares that serving anything reachable through the
216
+ chain is intended, and silences the "no gate" warning from `registry.route`.
217
+ - `check({name, version, scope, pkg})`: access control for the given package.
218
+ - `scope` / `pkg` are the parsed halves of `name` ( `scope` is null for an unscoped
219
+ name ), so scope comparison needs no regex. prefer `scopes` for plain scope
220
+ allowlisting.
221
+ - when both `scopes` and `check` are omitted, no check will be done -- everything the
222
+ chain can reach is served.
188
223
  - return:
189
224
  - if the given package is not allowed:
190
225
  - a Promise rejects with `{id: 403, name: 'lderror'}` (or, `lderror.reject(403)`)
@@ -265,7 +300,9 @@ A provider provides following APIs:
265
300
  the `main` symlink ( relative link, tmp + rename ). a real dir at `main`
266
301
  ( preinstalled / legacy ) is replaced only here; a symlink's target is never deleted.
267
302
  - return value: a Promise resolving `{name, version}`.
268
- - `check({name, version})`: call the `check()` function provided in constructor.
303
+ - `check({name, version})`: apply the access control gate: `scopes` first, then the
304
+ `check()` function provided in constructor ( which also receives `scope` / `pkg` ).
305
+ - `hasGate()`: whether this provider gates access at all ( `scopes`, `check` or `open` ).
269
306
  - `chain(providers)`: chain given `providers` in this provider.
270
307
  - `providers`: either another provider, or a list of other providers.
271
308
  - chained providers will be called if current provider return 404.
package/designate.js CHANGED
@@ -1,9 +1,9 @@
1
1
  // Generated by LiveScript 1.6.0
2
2
  (function(){
3
- var pthk, lderror, checkRoot, designate;
3
+ var pthk, lderror, ref$, checkRoot, checkGate, designate;
4
4
  pthk = require('pthk');
5
5
  lderror = require('lderror');
6
- checkRoot = require('./route').checkRoot;
6
+ ref$ = require('./route'), checkRoot = ref$.checkRoot, checkGate = ref$.checkGate;
7
7
  designate = function(arg$){
8
8
  var provider, root, opt;
9
9
  provider = arg$.provider, root = arg$.root, opt = arg$.opt;
@@ -11,6 +11,7 @@
11
11
  root.pub = root.pub + "/";
12
12
  }
13
13
  checkRoot(root.fs);
14
+ checkGate(provider, 'registry.designate');
14
15
  return function(req, res){
15
16
  var url, id, _o;
16
17
  url = req.originalUrl;
package/package.json CHANGED
@@ -1 +1 @@
1
- {"author":"zbryikt","name":"@plotdb/registry","license":"MIT","description":"registry","version":"0.0.13","main":"index.js","homepage":"https://github.com/plotdb/registry","repository":{"type":"git","url":"https://github.com/plotdb/registry"},"engines":{"node":">=18","npm":">=6.4.1"},"scripts":{"start":"npx server -r web -o true","server":"cd web; ../node_modules/.bin/lsc server.ls","build":"lsc -c -o dist src/index.ls src/route.ls src/provider.ls src/designate.ls src/unbox.ls src/ngxgen.ls && lsc -c -o dist/providers src/providers/index.ls src/providers/github.ls src/providers/npm.ls && cp src/config.ngx dist/config.ngx","test":"lsc test/index.ls"},"bin":{"registry-ngx":"./bin/ngxgen"},"dependencies":{"@plotdb/guides":"github:plotdb/guides#master","@plotdb/semver":"^0.0.4","@zbryikt/template":"^2.4.2","fs-extra":"^8.1.0","js-yaml":"^4.1.1","lderror":"^2.4.6","livescript":"^1.6.0","node-fetch":"^2.6.7","pthk":"^0.0.1","tar":"^7.5.21","template-text":"^0.2.0","yargs":"^17.6.0","yauzl":"^2.10.0"},"devDependencies":{"@loadingio/bootstrap.ext":"^0.0.12","@loadingio/debounce.js":"^1.0.1","@loadingio/ldquery":"^3.0.6","@plotdb/block":"^5.6.3","@plotdb/csscope":"^5.0.4","@plotdb/rescope":"^5.0.17","bootstrap":"^4.6.1","bootstrap.native":"^3.0.14","fedep":"^1.7.6","ldcover":"^3.5.6","ldloader":"^3.0.3","ldview":"^1.7.0","proxise":"^1.0.1"},"frontendDependencies":{"root":"web/static/assets/lib","modules":["@loadingio/bootstrap.ext","@loadingio/debounce.js","@loadingio/ldquery","bootstrap","bootstrap.native","@plotdb/block","@plotdb/csscope","@plotdb/rescope","@plotdb/semver","ldcover","ldloader","ldview","proxise"]}}
1
+ {"author":"zbryikt","name":"@plotdb/registry","license":"MIT","description":"registry","version":"0.0.14","main":"index.js","homepage":"https://github.com/plotdb/registry","repository":{"type":"git","url":"https://github.com/plotdb/registry"},"engines":{"node":">=18","npm":">=6.4.1"},"scripts":{"start":"npx server -r web -o true","server":"cd web; ../node_modules/.bin/lsc server.ls","build":"lsc -c -o dist src/index.ls src/route.ls src/provider.ls src/designate.ls src/unbox.ls src/ngxgen.ls && lsc -c -o dist/providers src/providers/index.ls src/providers/github.ls src/providers/npm.ls && cp src/config.ngx dist/config.ngx","test":"lsc test/index.ls"},"bin":{"registry-ngx":"./bin/ngxgen"},"dependencies":{"@plotdb/guides":"github:plotdb/guides#master","@plotdb/semver":"^0.0.4","@zbryikt/template":"^2.4.2","fs-extra":"^8.1.0","js-yaml":"^4.1.1","lderror":"^2.4.6","livescript":"^1.6.0","node-fetch":"^2.6.7","pthk":"^0.0.1","tar":"^7.5.21","template-text":"^0.2.0","yargs":"^17.6.0","yauzl":"^2.10.0"},"devDependencies":{"@loadingio/bootstrap.ext":"^0.0.12","@loadingio/debounce.js":"^1.0.1","@loadingio/ldquery":"^3.0.6","@plotdb/block":"^5.6.3","@plotdb/csscope":"^5.0.4","@plotdb/rescope":"^5.0.17","bootstrap":"^4.6.1","bootstrap.native":"^3.0.14","fedep":"^1.7.6","ldcover":"^3.5.6","ldloader":"^3.0.3","ldview":"^1.7.0","proxise":"^1.0.1"},"frontendDependencies":{"root":"web/static/assets/lib","modules":["@loadingio/bootstrap.ext","@loadingio/debounce.js","@loadingio/ldquery","bootstrap","bootstrap.native","@plotdb/block","@plotdb/csscope","@plotdb/rescope","@plotdb/semver","ldcover","ldloader","ldview","proxise"]}}
package/provider.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by LiveScript 1.6.0
2
2
  (function(){
3
- var nodeFetch, lderror, yauzl, pthk, tar, semver, fs, fetch, getVersionType, provider, ref$;
3
+ var nodeFetch, lderror, yauzl, pthk, tar, semver, fs, fetch, getVersionType, parseName, normalizeScopes, provider, ref$;
4
4
  nodeFetch = require('node-fetch');
5
5
  lderror = require('lderror');
6
6
  yauzl = require('yauzl');
@@ -22,6 +22,27 @@
22
22
  return null;
23
23
  }
24
24
  };
25
+ parseName = function(name){
26
+ var m;
27
+ if (m = /^@([0-9a-z._-]+)\/([0-9a-z._-]+)$/.exec(name + "")) {
28
+ return {
29
+ scope: "@" + m[1],
30
+ pkg: m[2]
31
+ };
32
+ } else {
33
+ return {
34
+ scope: null,
35
+ pkg: name + ""
36
+ };
37
+ }
38
+ };
39
+ normalizeScopes = function(scopes){
40
+ return (Array.isArray(scopes)
41
+ ? scopes
42
+ : [scopes]).map(function(s){
43
+ return (s + "").replace(/^@?/, '@').replace(/\/+$/, '');
44
+ });
45
+ };
25
46
  provider = function(o){
26
47
  o == null && (o = {});
27
48
  this._name = o.name || "unnamed" + (provider._idx++);
@@ -30,6 +51,8 @@
30
51
  this._fetchBundleFile = o.fetchBundleFile;
31
52
  this._fetchVersionList = o.fetchVersionList;
32
53
  this._check = o.check;
54
+ this._scopes = o.scopes != null ? normalizeScopes(o.scopes) : null;
55
+ this._open = !!o.open;
33
56
  this._opt = o.opt || {};
34
57
  return this;
35
58
  };
@@ -42,6 +65,7 @@
42
65
  return this._hash[n];
43
66
  };
44
67
  provider.versionType = getVersionType;
68
+ provider.parseName = parseName;
45
69
  provider.prototype = (ref$ = Object.create(Object.prototype), ref$.opt = function(o){
46
70
  return this._opt = o || {};
47
71
  }, ref$.fetch = function(o){
@@ -131,16 +155,24 @@
131
155
  });
132
156
  });
133
157
  }, ref$.check = function(arg$){
134
- var name, version;
158
+ var name, version, ref$, scope, pkg;
135
159
  name = arg$.name, version = arg$.version;
160
+ ref$ = parseName(name), scope = ref$.scope, pkg = ref$.pkg;
161
+ if (this._scopes && !in$(scope, this._scopes)) {
162
+ return lderror.reject(998);
163
+ }
136
164
  if (this._check) {
137
165
  return this._check({
138
166
  name: name,
139
- version: version
167
+ version: version,
168
+ scope: scope,
169
+ pkg: pkg
140
170
  });
141
171
  } else {
142
172
  return Promise.resolve();
143
173
  }
174
+ }, ref$.hasGate = function(){
175
+ return !!(this._scopes || this._check || this._open);
144
176
  }, ref$.chain = function(ps){
145
177
  return this._ps.splice.apply(this._ps, [0, 0].concat(Array.isArray(ps)
146
178
  ? ps
@@ -218,6 +250,9 @@
218
250
  version: 'latest',
219
251
  versionType: 'latest'
220
252
  }).then(function(info){
253
+ if (getVersionType(info != null ? info.version : void 8) !== 'specific') {
254
+ return lderror.reject(404);
255
+ }
221
256
  return fs.ensureDir(base).then(function(){
222
257
  return fs.writeFile(file, JSON.stringify({
223
258
  version: info.version
@@ -276,21 +311,33 @@
276
311
  base = pthk.join(root, pthk.rectify(name));
277
312
  ref$ = [pthk.join(base, 'main'), pthk.join(base, ".main." + process.pid + ".tmp")], mainPath = ref$[0], tmp = ref$[1];
278
313
  return Promise.resolve().then(function(){
279
- return fs.exists(pthk.join(base, version, '.reg.version')).then(function(isExisted){
280
- if (isExisted) {
281
- return;
314
+ return this$.check({
315
+ name: name,
316
+ version: version
317
+ })['catch'](function(e){
318
+ var ref$;
319
+ if ((ref$ = lderror.id(e)) === 403 || ref$ === 998) {
320
+ return lderror.reject(404);
321
+ } else {
322
+ return Promise.reject(e);
282
323
  }
283
- return this$.fetch({
284
- root: root,
285
- name: name,
286
- version: version,
287
- force: false,
288
- cachetime: cachetime
324
+ }).then(function(){
325
+ return fs.exists(pthk.join(base, version, '.reg.version')).then(function(isExisted){
326
+ if (isExisted) {
327
+ return;
328
+ }
329
+ return this$.fetch({
330
+ root: root,
331
+ name: name,
332
+ version: version,
333
+ force: false,
334
+ cachetime: cachetime
335
+ });
336
+ });
337
+ }).then(function(){
338
+ return fs.lstat(mainPath)['catch'](function(){
339
+ return null;
289
340
  });
290
- });
291
- }).then(function(){
292
- return fs.lstat(mainPath)['catch'](function(){
293
- return null;
294
341
  });
295
342
  }).then(function(s){
296
343
  return Promise.resolve().then(function(){
@@ -391,7 +438,9 @@
391
438
  }).then(function(vs){
392
439
  var v;
393
440
  if (v = semver.maxSatisfying(vs, version)) {
394
- return v;
441
+ return getVersionType(v) === 'specific'
442
+ ? v
443
+ : lderror.reject(404);
395
444
  }
396
445
  return lderror.reject(404);
397
446
  })['catch'](function(e){
@@ -463,6 +512,11 @@
463
512
  } else if (typeof window != 'undefined' && window !== null) {
464
513
  window.registry = provider;
465
514
  }
515
+ function in$(x, xs){
516
+ var i = -1, l = xs.length >>> 0;
517
+ while (++i < l) if (x === xs[i]) return true;
518
+ return false;
519
+ }
466
520
  function import$(obj, src){
467
521
  var own = {}.hasOwnProperty;
468
522
  for (var key in src) if (own.call(src, key)) obj[key] = src[key];
package/route.js CHANGED
@@ -1,17 +1,45 @@
1
1
  // Generated by LiveScript 1.6.0
2
2
  (function(){
3
- var pthk, lderror, fs, versionType, handle, checked, checkRoot, route;
3
+ var pthk, lderror, fs, versionType, safePath, handle, checked, checkRoot, gated, checkGate, route;
4
4
  pthk = require('pthk');
5
5
  lderror = require('lderror');
6
6
  fs = require("fs-extra");
7
7
  versionType = require('./provider').versionType;
8
+ safePath = function(p){
9
+ var ref$, prev, cur, i$, e;
10
+ ref$ = ['', (p || '') + ""], prev = ref$[0], cur = ref$[1];
11
+ for (i$ = 0; i$ < 4; ++i$) {
12
+ if (cur === prev) {
13
+ break;
14
+ }
15
+ prev = cur;
16
+ try {
17
+ cur = decodeURIComponent(cur);
18
+ } catch (e$) {
19
+ e = e$;
20
+ return false;
21
+ }
22
+ }
23
+ if (cur !== prev) {
24
+ return false;
25
+ }
26
+ if (/\0/.test(cur)) {
27
+ return false;
28
+ }
29
+ return cur.split(/[\/\\]/).every(function(seg){
30
+ return seg !== '..';
31
+ });
32
+ };
8
33
  handle = function(arg$){
9
- var provider, id, root, opt, ref$, paths, obj;
34
+ var provider, id, root, opt, ref$, paths, obj, accel;
10
35
  provider = arg$.provider, id = arg$.id, root = arg$.root, opt = arg$.opt;
11
36
  if (!(id && (id + "").trim())) {
12
37
  return lderror.reject(404);
13
38
  }
14
39
  ref$ = [{}, {}, pthk.rectify(id)], paths = ref$[0], obj = ref$[1], id = ref$[2];
40
+ accel = function(){
41
+ return pthk.join(root.internal, obj.name, obj.version + "", obj.path);
42
+ };
15
43
  return Promise.resolve().then(function(){
16
44
  var ids, e, vt, p;
17
45
  if (!/^[-_a-zA-Z0-9@./~^%]+$/.exec(id)) {
@@ -38,6 +66,9 @@
38
66
  e = e$;
39
67
  return lderror.reject(404);
40
68
  }
69
+ if (!safePath(obj.path)) {
70
+ return lderror.reject(404);
71
+ }
41
72
  if ((vt = versionType(obj.version)) === 'range' || vt === 'latest') {
42
73
  p = vt === 'range'
43
74
  ? provider.resolve(import$({
@@ -66,9 +97,19 @@
66
97
  force: false,
67
98
  cachetime: 60 * 60
68
99
  }, opt));
100
+ }).then(function(r){
101
+ if (r && r.redirect) {
102
+ return r;
103
+ } else {
104
+ return {
105
+ accel: accel()
106
+ };
107
+ }
69
108
  })['catch'](function(e){
70
109
  if (lderror.id(e) === 998) {
71
- return;
110
+ return {
111
+ accel: accel()
112
+ };
72
113
  }
73
114
  if (lderror.id(e) !== 404) {
74
115
  return Promise.reject(e);
@@ -105,6 +146,22 @@
105
146
  }
106
147
  }());
107
148
  };
149
+ gated = {};
150
+ checkGate = function(provider, where){
151
+ var name;
152
+ if (!provider || typeof provider.hasGate !== 'function') {
153
+ return true;
154
+ }
155
+ if (provider.hasGate()) {
156
+ return true;
157
+ }
158
+ if (gated[name = provider._name]) {
159
+ return false;
160
+ }
161
+ gated[name] = true;
162
+ console.warn("[@plotdb/registry] provider `" + name + "` used by " + where + " has no `scopes` / `check`:\nevery package reachable through its chain is served from this origin. restrict it, e.g.:\n myprovider = new registry.provider {scopes: ['@plotdb'], chain: [...]}\npass `open: true` to the provider if serving anything is intended.");
163
+ return false;
164
+ };
108
165
  route = function(arg$){
109
166
  var provider, root, opt;
110
167
  provider = arg$.provider, root = arg$.root, opt = arg$.opt;
@@ -112,6 +169,7 @@
112
169
  root.pub = root.pub + "/";
113
170
  }
114
171
  checkRoot(root.fs);
172
+ checkGate(provider, 'registry.route');
115
173
  return function(req, res){
116
174
  var url, id, _o;
117
175
  url = req.originalUrl;
@@ -129,7 +187,7 @@
129
187
  return res.redirect(302, r.redirect);
130
188
  }
131
189
  res.set({
132
- "X-Accel-Redirect": pthk.join(root.internal, id)
190
+ "X-Accel-Redirect": r.accel
133
191
  });
134
192
  return res.send();
135
193
  })['catch'](function(e){
@@ -144,6 +202,7 @@
144
202
  };
145
203
  };
146
204
  route.checkRoot = checkRoot;
205
+ route.checkGate = checkGate;
147
206
  module.exports = route;
148
207
  function import$(obj, src){
149
208
  var own = {}.hasOwnProperty;