@cdxoo/npm-lockdown-proxy 0.0.1

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 (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -0
  3. package/package.json +28 -0
  4. package/proxy.js +126 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 cdxOo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # @cdxoo/npm-lockdown-proxy
2
+
3
+ A minimal npm registry proxy that blocks any package (or version) not on a whitelist.
4
+
5
+ ## AI Disclosure
6
+
7
+ This stuff was vibe coded with claude (pronounced "KLORT!!")
8
+
9
+ ## Run
10
+
11
+ ```sh
12
+ node proxy.js
13
+ ```
14
+
15
+ | Env var | Default | Description |
16
+ |---|---|---|
17
+ | `PORT` | `4873` | Port to listen on |
18
+ | `WHITELIST` | `whitelist.json` | Path to whitelist file |
19
+
20
+ ## Use
21
+
22
+ ```sh
23
+ npm install <pkg> --registry http://localhost:4873
24
+ # or set it globally
25
+ npm config set registry http://localhost:4873
26
+ ```
27
+
28
+ ## Whitelist format
29
+
30
+ `whitelist.json` is an object. The value controls which versions are allowed:
31
+
32
+ ```json
33
+ {
34
+ "express": "*",
35
+ "lodash": "4.17.21",
36
+ "@types/node": ["18.19.9", "20.11.5"]
37
+ }
38
+ ```
39
+
40
+ | Value | Meaning |
41
+ |---|---|
42
+ | `"*"` | Any version |
43
+ | `"1.2.3"` | Exact version only |
44
+ | `["1.2.3", "4.5.6"]` | Any of these exact versions |
45
+
46
+ ## Behaviour
47
+
48
+ - Package not in whitelist -> `404` (npm sees it as non-existent)
49
+ - Package in whitelist, version not allowed -> `404` on the tarball download
50
+ - Applies to **all** packages including transitive dependencies
51
+ - `/-/` endpoints (ping, search) are always passed through
52
+
53
+ ## Reload whitelist without restart
54
+
55
+ ```sh
56
+ kill -HUP <pid>
57
+ ```
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@cdxoo/npm-lockdown-proxy",
3
+ "version": "0.0.1",
4
+ "description": "Minimal npm registry proxy with package/version whitelisting",
5
+ "bin": {
6
+ "npm-lockdown-proxy": "./proxy.js"
7
+ },
8
+ "files": [
9
+ "proxy.js"
10
+ ],
11
+ "engines": {
12
+ "node": ">=18"
13
+ },
14
+ "author": "Jan Schwalbe",
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/cdxOo/npm-lockdown-proxy.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/cdxOo/npm-lockdown-proxy/issues"
22
+ },
23
+ "homepage": "https://github.com/cdxOo/npm-lockdown-proxy#readme",
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "dependencies": {}
28
+ }
package/proxy.js ADDED
@@ -0,0 +1,126 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const http = require('http');
5
+ const https = require('https');
6
+ const fs = require('fs');
7
+ const path = require('path');
8
+
9
+ const UPSTREAM = 'https://registry.npmjs.org';
10
+ const PORT = process.env.PORT || 4873;
11
+ const WHITELIST_FILE = path.resolve(process.env.WHITELIST || 'whitelist.json');
12
+
13
+ function loadWhitelist() {
14
+ let raw;
15
+ try {
16
+ raw = JSON.parse(fs.readFileSync(WHITELIST_FILE, 'utf8'));
17
+ } catch {
18
+ console.warn(`WARNING: could not load whitelist from '${WHITELIST_FILE}' — all packages will be blocked`);
19
+ console.warn(` Set the WHITELIST env var or create a whitelist.json in the working directory.`);
20
+ return {};
21
+ }
22
+ // Normalise each entry to an array of allowed versions, or '*' for any.
23
+ const wl = {};
24
+ for (const [pkg, versions] of Object.entries(raw)) {
25
+ if (versions === '*') {
26
+ wl[pkg] = '*';
27
+ } else if (Array.isArray(versions)) {
28
+ wl[pkg] = versions;
29
+ } else {
30
+ wl[pkg] = [versions];
31
+ }
32
+ }
33
+ return wl;
34
+ }
35
+
36
+ let whitelist = loadWhitelist();
37
+
38
+ process.on('SIGHUP', () => {
39
+ whitelist = loadWhitelist();
40
+ console.log('whitelist reloaded');
41
+ });
42
+
43
+ // Parse the request pathname into { pkg, version }.
44
+ // pkg - package name (scoped or plain), null for npm-internal paths
45
+ // version - string if this is a tarball request, otherwise null
46
+ function parseRequest(pathname) {
47
+ if (pathname.startsWith('/-/')) return { pkg: null, version: null };
48
+
49
+ const parts = pathname.slice(1).split('/'); // drop leading /
50
+ let pkg, rest;
51
+
52
+ if (parts[0].startsWith('@')) {
53
+ if (parts.length < 2) return { pkg: null, version: null };
54
+ pkg = `${parts[0]}/${parts[1]}`;
55
+ rest = parts.slice(2);
56
+ } else {
57
+ pkg = parts[0] || null;
58
+ rest = parts.slice(1);
59
+ }
60
+
61
+ // Tarball path: /pkg/-/pkg-1.2.3.tgz or /@scope/pkg/-/pkg-1.2.3.tgz
62
+ // rest would be ['-', 'pkg-1.2.3.tgz'] at this point
63
+ let version = null;
64
+ if (rest[0] === '-' && rest[1]?.endsWith('.tgz')) {
65
+ const filename = rest[1];
66
+ const basename = pkg.includes('/') ? pkg.split('/')[1] : pkg;
67
+ const prefix = `${basename}-`;
68
+ if (filename.startsWith(prefix)) {
69
+ version = filename.slice(prefix.length, -4); // strip prefix and .tgz
70
+ }
71
+ }
72
+
73
+ return { pkg, version };
74
+ }
75
+
76
+ function deny(res, msg) {
77
+ const body = JSON.stringify({ error: msg });
78
+ res.writeHead(404, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) });
79
+ res.end(body);
80
+ }
81
+
82
+ const server = http.createServer((req, res) => {
83
+ const { pkg, version } = parseRequest(req.url.split('?')[0]);
84
+
85
+ if (pkg !== null) {
86
+ if (!Object.keys(whitelist).includes(pkg)) {
87
+ console.log(`BLOCKED ${req.method} ${req.url} - '${pkg}' not whitelisted`);
88
+ return deny(res, `Package '${pkg}' is not on the whitelist`);
89
+ }
90
+
91
+ const allowed = whitelist[pkg];
92
+ if (version !== null && allowed !== '*' && !allowed.includes(version)) {
93
+ console.log(`BLOCKED ${req.method} ${req.url} - '${pkg}@${version}' not an allowed version`);
94
+ return deny(res, `Version '${version}' of '${pkg}' is not on the whitelist (allowed: ${allowed.join(', ')})`);
95
+ }
96
+ }
97
+
98
+ console.log(`ALLOW ${req.method} ${req.url}`);
99
+
100
+ const url = new URL(req.url, UPSTREAM);
101
+ const options = {
102
+ hostname: url.hostname,
103
+ port: url.port || 443,
104
+ path: url.pathname + url.search,
105
+ method: req.method,
106
+ headers: { ...req.headers, host: url.hostname },
107
+ };
108
+
109
+ const proxy = https.request(options, (upstream) => {
110
+ res.writeHead(upstream.statusCode, upstream.headers);
111
+ upstream.pipe(res);
112
+ });
113
+
114
+ proxy.on('error', (err) => {
115
+ console.error(err.message);
116
+ res.writeHead(502);
117
+ res.end('Bad Gateway');
118
+ });
119
+
120
+ req.pipe(proxy);
121
+ });
122
+
123
+ server.listen(PORT, () => {
124
+ console.log(`npm proxy -> ${UPSTREAM} on http://localhost:${PORT}`);
125
+ console.log(`whitelist ${WHITELIST_FILE} (${Object.keys(whitelist).length} packages)`);
126
+ });