lich-cli 0.0.0-stage → 0.1.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.
package/dist/guard.cjs ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ // The Network_Guard. The Side_Controller loads this file with --require, so it is in place before
3
+ // the Worker and the package load. On Node 22 and 24 the permission model does not cover the
4
+ // network, so this file stops the network APIs it knows about: the network is blocked by a guard.
5
+ // The guard is in-process JavaScript, so it is not a hard boundary. Each attempt is logged as
6
+ // well as refused, because a package can catch the error and the Worker must still show the attempt.
7
+ const dgram = require("node:dgram");
8
+ const dns = require("node:dns");
9
+ const dnsPromises = require("node:dns/promises");
10
+ const nodeModule = require("node:module");
11
+ const net = require("node:net");
12
+ let attempts = [];
13
+ // The Worker calls this after every call from the same module instance, so each call's outcome
14
+ // holds only the attempts made while it ran.
15
+ function takeAttempts() {
16
+ const taken = attempts;
17
+ attempts = [];
18
+ return taken;
19
+ }
20
+ function refuse(api, describe, args) {
21
+ let target = "";
22
+ try {
23
+ // A target may come from a hostile value, and the Worker's detail must stay on one line.
24
+ target = describe(args).replace(/[\r\n]+/g, " ");
25
+ }
26
+ catch {
27
+ // A getter in the arguments threw. The attempt is still logged and still refused by the guard.
28
+ }
29
+ attempts.push({ api, target });
30
+ throw Object.assign(new Error(`Network blocked by a guard in the checker: ${api} ${target}`), {
31
+ code: "ERR_LICH_NETWORK_BLOCKED",
32
+ });
33
+ }
34
+ function replace(holder, key, api, describe) {
35
+ const slots = holder;
36
+ // Skips a function that this Node version does not have, such as a newer record type.
37
+ if (typeof slots[key] !== "function")
38
+ return;
39
+ slots[key] = function guarded(...args) {
40
+ return refuse(api, describe, args);
41
+ };
42
+ }
43
+ // Reads only strings and numbers, so no package value's toString runs inside the guard.
44
+ function text(value) {
45
+ return typeof value === "string" || typeof value === "number" ? String(value) : "";
46
+ }
47
+ const firstArgument = (args) => text(args[0]);
48
+ const serviceTarget = (args) => `${text(args[0])}:${text(args[1])}`;
49
+ const connectTarget = (args) => {
50
+ // net.connect, and so every HTTP, HTTPS and TLS client, passes one array of parsed arguments.
51
+ const parsed = Array.isArray(args[0]) ? args[0] : args;
52
+ const [first, second] = parsed;
53
+ if (typeof first === "object" && first !== null) {
54
+ const options = first;
55
+ // Node takes a pipe only for a non-empty path. The HTTP agent passes `path: null` for TCP.
56
+ if (typeof options.path === "string" && options.path !== "")
57
+ return options.path;
58
+ return `${text(options.host) || "localhost"}:${text(options.port)}`;
59
+ }
60
+ // A string that is not a port number is a Unix socket or named pipe path.
61
+ if (typeof first === "string" && !/^\d+$/.test(first))
62
+ return first;
63
+ return `${text(second) || "localhost"}:${text(first)}`;
64
+ };
65
+ const socketTarget = (args) => {
66
+ const first = args[0];
67
+ if (typeof first === "object" && first !== null) {
68
+ return text(first.type);
69
+ }
70
+ return text(first);
71
+ };
72
+ // `reverse` and `lookupService` are DNS queries too, so the guard covers them with the
73
+ // `resolve*` functions. The list is read from the module, so a record type that a later Node adds is covered.
74
+ function guardDns(holder, prefix) {
75
+ for (const key of Object.keys(holder)) {
76
+ if (key === "lookup" || key === "reverse" || key.startsWith("resolve")) {
77
+ replace(holder, key, `${prefix}.${key}`, firstArgument);
78
+ }
79
+ else if (key === "lookupService") {
80
+ replace(holder, key, `${prefix}.${key}`, serviceTarget);
81
+ }
82
+ }
83
+ }
84
+ // The own methods of a Resolver prototype are its queries. The shared base class holds
85
+ // getServers, setServers, setLocalAddress and cancel, which send nothing, so they stay.
86
+ function guardResolver(prototype, prefix) {
87
+ for (const key of Object.getOwnPropertyNames(prototype)) {
88
+ if (key !== "constructor")
89
+ replace(prototype, key, `${prefix}.${key}`, firstArgument);
90
+ }
91
+ }
92
+ // TLS sockets inherit this method, and fetch opens its sockets through net, so this one
93
+ // replacement covers HTTP, HTTPS, TLS and fetch.
94
+ replace(net.Socket.prototype, "connect", "net.Socket.connect", connectTarget);
95
+ guardDns(dns, "dns");
96
+ guardDns(dnsPromises, "dns.promises");
97
+ guardResolver(dns.Resolver.prototype, "dns.Resolver");
98
+ guardResolver(dnsPromises.Resolver.prototype, "dns.promises.Resolver");
99
+ replace(dgram, "createSocket", "dgram.createSocket", socketTarget);
100
+ // A package loaded with import reads the ESM view of each built-in module, which Node only
101
+ // refreshes from module.exports on request.
102
+ nodeModule.syncBuiltinESMExports();
103
+ module.exports = { takeAttempts };
104
+ //# sourceMappingURL=guard.cjs.map