@writedocs/generator 0.10.4 → 0.10.5
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/package.json +1 -1
- package/src/cli/build-auth.js +95 -38
package/package.json
CHANGED
package/src/cli/build-auth.js
CHANGED
|
@@ -15,64 +15,121 @@
|
|
|
15
15
|
* See docs/dev/docs/deploy.mdx for how to deploy writedocs-key-server and
|
|
16
16
|
* issue keys, and that repo's own README for the service's endpoints.
|
|
17
17
|
*/
|
|
18
|
+
import https from 'node:https';
|
|
18
19
|
import { log } from './output.js';
|
|
19
20
|
|
|
20
21
|
export const EX_TEMPFAIL = 75;
|
|
21
22
|
|
|
22
|
-
/** The authorization
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
23
|
+
/** The authorization servers every `build` asks, in this order. Two only
|
|
24
|
+
* while key validation moves to the WriteDocs platform (the first entry);
|
|
25
|
+
* the second, writedocs-key-server, goes once every key comes from the
|
|
26
|
+
* platform.
|
|
27
|
+
*
|
|
28
|
+
* Fixed in the code, with no environment override: when the URL came from
|
|
29
|
+
* WRITEDOCS_KEY_SERVER_URL, anyone could point it at a 3-line server that
|
|
30
|
+
* answers `{ "valid": true }` and build without a key. Tests and CI use a
|
|
31
|
+
* real key instead (the WRITEDOCS_API_KEY secret - see
|
|
27
32
|
* scripts/smoke-installed-cli.sh). Honest limit: the source ships with the
|
|
28
33
|
* package, so someone willing to edit it can still remove this check - it
|
|
29
34
|
* raises the bar, it isn't a lock. Real protection is what the platform
|
|
30
35
|
* does with the output. */
|
|
31
|
-
export const
|
|
36
|
+
export const KEY_SERVER_URLS = [
|
|
37
|
+
'https://api.writedocs.io',
|
|
38
|
+
'https://proxy.writechoice.io:8787',
|
|
39
|
+
];
|
|
32
40
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
/** The default transport: POSTs `body` (a JSON string) to `url` and resolves
|
|
42
|
+
* with `{ status, text }`, or rejects on a network error or once 10s have
|
|
43
|
+
* passed. node:https rather than fetch: on Windows (seen on Node 24.11),
|
|
44
|
+
* two fetch calls followed by process.exit abort Node with a libuv
|
|
45
|
+
* assertion (src\win\async.c) and exit 127 instead of 1 or 75. `agent:
|
|
46
|
+
* false` keeps no connection alive past the answer. */
|
|
47
|
+
export function httpsPostJson(url, body) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const req = https.request(
|
|
50
|
+
url,
|
|
51
|
+
{
|
|
52
|
+
method: 'POST',
|
|
53
|
+
agent: false,
|
|
54
|
+
headers: { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) },
|
|
55
|
+
signal: AbortSignal.timeout(10_000),
|
|
56
|
+
},
|
|
57
|
+
(res) => {
|
|
58
|
+
let text = '';
|
|
59
|
+
res.setEncoding('utf8');
|
|
60
|
+
res.on('data', (chunk) => (text += chunk));
|
|
61
|
+
res.on('end', () => resolve({ status: res.statusCode, text }));
|
|
62
|
+
res.on('error', reject);
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
req.on('error', reject);
|
|
66
|
+
req.end(body);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
39
69
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
70
|
+
/** Asks one server about the key. Never throws; the answer is one of
|
|
71
|
+
* 'valid' - a 2xx with `valid: true`.
|
|
72
|
+
* 'invalid' - any other 2xx, or a 4xx other than 404: the server
|
|
73
|
+
* looked at the key and said no.
|
|
74
|
+
* 'unavailable' - network error, timeout, 5xx or 404 (a server that
|
|
75
|
+
* doesn't have /v1/validate yet can't judge the key),
|
|
76
|
+
* or any other status. `reason` says which.
|
|
77
|
+
* `transport` is httpsPostJson unless a test passes a stand-in. */
|
|
78
|
+
export async function askKeyServer(serverUrl, key, transport = httpsPostJson) {
|
|
47
79
|
let res;
|
|
48
80
|
try {
|
|
49
|
-
res = await
|
|
50
|
-
method: 'POST',
|
|
51
|
-
headers: { 'content-type': 'application/json' },
|
|
52
|
-
body: JSON.stringify({ key: providedKey }),
|
|
53
|
-
signal: AbortSignal.timeout(10_000),
|
|
54
|
-
});
|
|
81
|
+
res = await transport(new URL('/v1/validate', serverUrl), JSON.stringify({ key }));
|
|
55
82
|
} catch (err) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
83
|
+
return { result: 'unavailable', reason: err.message };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (res.status >= 200 && res.status < 300) {
|
|
87
|
+
let body = null;
|
|
88
|
+
try {
|
|
89
|
+
body = JSON.parse(res.text);
|
|
90
|
+
} catch {}
|
|
91
|
+
return body?.valid === true ? { result: 'valid' } : { result: 'invalid', reason: `HTTP ${res.status}` };
|
|
92
|
+
}
|
|
93
|
+
if (res.status >= 400 && res.status < 500 && res.status !== 404) {
|
|
94
|
+
return { result: 'invalid', reason: `HTTP ${res.status}` };
|
|
61
95
|
}
|
|
96
|
+
return { result: 'unavailable', reason: `HTTP ${res.status}` };
|
|
97
|
+
}
|
|
62
98
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
99
|
+
/** `transport` is passed on to askKeyServer - tests only. */
|
|
100
|
+
export async function requireBuildKey(providedKey, transport = httpsPostJson) {
|
|
101
|
+
if (!providedKey) {
|
|
102
|
+
log.error('build requires a valid --key (or WRITEDOCS_API_KEY).');
|
|
103
|
+
process.exit(1);
|
|
66
104
|
}
|
|
67
105
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
106
|
+
// The exit code is a contract with whoever runs `build` - the WriteDocs
|
|
107
|
+
// platform retries on it, never on the message text:
|
|
108
|
+
// 1 - the key was not accepted: missing key, or no server said `valid`
|
|
109
|
+
// and at least one said no. Trying again with the same key gives
|
|
110
|
+
// the same answer.
|
|
111
|
+
// 75 - EX_TEMPFAIL (sysexits.h): no server could be asked - each one
|
|
112
|
+
// gave a network error, timeout, 5xx or 404. Still fails closed (no
|
|
113
|
+
// build happens); it only tells the caller that trying later may
|
|
114
|
+
// work.
|
|
115
|
+
// The first `valid` wins; a server that says no doesn't stop the next one
|
|
116
|
+
// from being asked, since a key may be known to only one of them.
|
|
117
|
+
const answers = [];
|
|
118
|
+
for (const serverUrl of KEY_SERVER_URLS) {
|
|
119
|
+
const answer = await askKeyServer(serverUrl, providedKey, transport);
|
|
120
|
+
if (answer.result === 'valid') return;
|
|
121
|
+
answers.push({ serverUrl, ...answer });
|
|
72
122
|
}
|
|
73
123
|
|
|
74
|
-
if (
|
|
124
|
+
if (answers.some((a) => a.result === 'invalid')) {
|
|
75
125
|
log.error('build requires a valid --key (or WRITEDOCS_API_KEY) - the server rejected this one.');
|
|
76
126
|
process.exit(1);
|
|
77
127
|
}
|
|
128
|
+
|
|
129
|
+
// Every server unavailable. Fails closed, same as a rejection - an
|
|
130
|
+
// unreachable authorization server is not treated as "no opinion, let it
|
|
131
|
+
// through".
|
|
132
|
+
log.error('Could not reach any build authorization server - try again later:');
|
|
133
|
+
for (const a of answers) log.line(` ${a.serverUrl}: ${a.reason}`);
|
|
134
|
+
process.exit(EX_TEMPFAIL);
|
|
78
135
|
}
|