dotshield 0.0.0-stage → 0.1.0-beta.2

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,12 @@
1
- # Temporary Holding Version
1
+ # dotshield
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Keeps secrets away from AI coding agents (Claude Code and Cursor): a free offline scanner, and a guard that stops agents from reading or printing secrets.
4
+
5
+ ```bash
6
+ npx dotshield scan # find secrets in this folder, its git history and your agent files
7
+ npx dotshield guard install # guard Claude Code, in watch mode first
8
+ ```
9
+
10
+ This package is a small wrapper with no dependencies and no install script. On first use it downloads the `dotshield` binary for your system from the matching GitHub release, checks its SHA-256 against checksums taken from the release's signed `checksums.txt` and published in this package, and keeps it in `~/.dotshield/bin/`. Agent hooks call that binary directly.
11
+
12
+ Downloads and docs: https://github.com/dot-shield/dotshield-releases. Not affiliated with Anthropic or Cursor.
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env node
2
+ // Runs the dotshield binary for this package's version, downloading it on
3
+ // first use. The binary, not this script, is what agent hooks call: "dotshield
4
+ // guard install" writes the binary's own path into the hook settings.
5
+ 'use strict';
6
+
7
+ const { spawnSync } = require('node:child_process');
8
+ const { binaryPath } = require('../lib/install.js');
9
+
10
+ async function main() {
11
+ let bin;
12
+ try {
13
+ bin = await binaryPath();
14
+ } catch (err) {
15
+ // lib/install.js keeps URLs and environment values out of its messages.
16
+ process.stderr.write(`dotshield: ${err.message}\n`);
17
+ return 2;
18
+ }
19
+ const r = spawnSync(bin, process.argv.slice(2), { stdio: 'inherit' });
20
+ if (r.error) {
21
+ process.stderr.write(`dotshield: can't run ${bin}: ${r.error.code || r.error.message}\n`);
22
+ return 2;
23
+ }
24
+ if (r.signal) {
25
+ process.kill(process.pid, r.signal);
26
+ return 1;
27
+ }
28
+ return r.status === null ? 1 : r.status;
29
+ }
30
+
31
+ main().then((code) => {
32
+ process.exitCode = code;
33
+ });
package/checksums.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "dotshield_0.1.0-beta.2_darwin_arm64.tar.gz": {
3
+ "archive": "f79d6acd89cba118d6a720ddcc885ffc63ec2caf8b720d8575337cd0dc31be16",
4
+ "binary": "a4ac29ce3386f4714ffa5631e6196c913b1e1490ccf5850de8f4817594ae1ea7"
5
+ },
6
+ "dotshield_0.1.0-beta.2_darwin_amd64.tar.gz": {
7
+ "archive": "619e0dece81b85b013520773152460d199b6137444ee5245d6553de6d733c820",
8
+ "binary": "5ae8f57c1a664ff1bf74a47173e6c485c9758a3168a2288b664e57b0694c2efc"
9
+ },
10
+ "dotshield_0.1.0-beta.2_linux_arm64.tar.gz": {
11
+ "archive": "dc83cbbe7899a1a4d7c3598afffb6538dfb29d014e17caf5a554bcb7b3809a85",
12
+ "binary": "eccc01322eaa5b45962a50592e8dae53ef103a52eaa489d2e6d55164c9fd6e5d"
13
+ },
14
+ "dotshield_0.1.0-beta.2_linux_amd64.tar.gz": {
15
+ "archive": "92671bc93482f79e28057848260fcc802e576846d115252e3f4cfd5a315f7c25",
16
+ "binary": "6a904063c60277b4105ae8ff9730524094a6406f53cc24773642c731a747f079"
17
+ },
18
+ "dotshield_0.1.0-beta.2_windows_arm64.zip": {
19
+ "archive": "6b646e2b141e6e74c2692a619acc7520bfda56b9cf3145a2eb994d0ffd3d88ef",
20
+ "binary": "65b7ce5adec15cb2954c957a55360d0987b40920f548b78fcf1288e53c721054"
21
+ },
22
+ "dotshield_0.1.0-beta.2_windows_amd64.zip": {
23
+ "archive": "ed252103287d24c0478718f3eb0f830af29c96bea63b807b06b31d2faccd8190",
24
+ "binary": "6923e534dab51a8224f679fe3391e7371626c8a833e7aafc3e46f0b247d8bf70"
25
+ }
26
+ }
package/lib/archive.js ADDED
@@ -0,0 +1,121 @@
1
+ 'use strict';
2
+
3
+ // Reads one file out of a release archive: the .tar.gz that GoReleaser makes
4
+ // for macOS and Linux, or the .zip for Windows. Only what those archives use
5
+ // is supported. The archive's SHA-256 is checked before either runs.
6
+
7
+ const zlib = require('node:zlib');
8
+
9
+ const MAX_FILE = 200 << 20;
10
+
11
+ // fromTarGz returns the contents of the top-level file `name` in a gzipped
12
+ // tar archive, or null if it isn't there.
13
+ function fromTarGz(buf, name) {
14
+ const tar = zlib.gunzipSync(buf, { maxOutputLength: MAX_FILE + (1 << 20) });
15
+ let off = 0;
16
+ let longName = null;
17
+ while (off + 512 <= tar.length) {
18
+ const h = tar.subarray(off, off + 512);
19
+ if (h.every((b) => b === 0)) break;
20
+ const size = octal(h.subarray(124, 136));
21
+ const type = String.fromCharCode(h[156] || 0x30);
22
+ const body = off + 512;
23
+ const next = body + Math.ceil(size / 512) * 512;
24
+ if (size < 0 || body + size > tar.length) throw new Error('the archive is damaged');
25
+ if (type === 'x' || type === 'L') {
26
+ const data = tar.subarray(body, body + size);
27
+ longName = type === 'L' ? cstr(data) : paxPath(data);
28
+ } else if (type === 'g') {
29
+ // A global header holds nothing this needs.
30
+ } else {
31
+ let entry = longName;
32
+ if (entry === null) {
33
+ const prefix = cstr(h.subarray(345, 500));
34
+ const base = cstr(h.subarray(0, 100));
35
+ entry = prefix ? `${prefix}/${base}` : base;
36
+ }
37
+ longName = null;
38
+ if ((type === '0' || type === '\0') && entry.replace(/^\.\//, '') === name) {
39
+ if (size > MAX_FILE) throw new Error('the binary in the archive is too large');
40
+ return Buffer.from(tar.subarray(body, body + size));
41
+ }
42
+ }
43
+ off = next;
44
+ }
45
+ return null;
46
+ }
47
+
48
+ function octal(b) {
49
+ const s = cstr(b).trim();
50
+ if (s === '') return 0;
51
+ if (!/^[0-7]+$/.test(s)) return -1;
52
+ return parseInt(s, 8);
53
+ }
54
+
55
+ function cstr(b) {
56
+ const i = b.indexOf(0);
57
+ return b.subarray(0, i < 0 ? b.length : i).toString('utf8');
58
+ }
59
+
60
+ // paxPath returns the "path" record of a PAX extended header, or null.
61
+ function paxPath(data) {
62
+ let off = 0;
63
+ while (off < data.length) {
64
+ const sp = data.indexOf(0x20, off);
65
+ if (sp < 0) break;
66
+ const len = parseInt(data.subarray(off, sp).toString('ascii'), 10);
67
+ if (!(len > 0)) break;
68
+ const rec = data.subarray(sp + 1, off + len - 1).toString('utf8');
69
+ const eq = rec.indexOf('=');
70
+ if (eq > 0 && rec.slice(0, eq) === 'path') return rec.slice(eq + 1);
71
+ off += len;
72
+ }
73
+ return null;
74
+ }
75
+
76
+ // fromZip returns the contents of the top-level file `name` in a zip
77
+ // archive, or null if it isn't there.
78
+ function fromZip(buf, name) {
79
+ const eocd = findEOCD(buf);
80
+ const count = buf.readUInt16LE(eocd + 10);
81
+ let off = buf.readUInt32LE(eocd + 16);
82
+ for (let i = 0; i < count; i++) {
83
+ if (off + 46 > buf.length || buf.readUInt32LE(off) !== 0x02014b50) {
84
+ throw new Error('the archive is damaged');
85
+ }
86
+ const method = buf.readUInt16LE(off + 10);
87
+ const csize = buf.readUInt32LE(off + 20);
88
+ const usize = buf.readUInt32LE(off + 24);
89
+ const nlen = buf.readUInt16LE(off + 28);
90
+ const xlen = buf.readUInt16LE(off + 30);
91
+ const clen = buf.readUInt16LE(off + 32);
92
+ const local = buf.readUInt32LE(off + 42);
93
+ const entry = buf.subarray(off + 46, off + 46 + nlen).toString('utf8');
94
+ off += 46 + nlen + xlen + clen;
95
+ if (entry !== name) continue;
96
+ if (usize > MAX_FILE) throw new Error('the binary in the archive is too large');
97
+ if (local + 30 > buf.length || buf.readUInt32LE(local) !== 0x04034b50) {
98
+ throw new Error('the archive is damaged');
99
+ }
100
+ const start = local + 30 + buf.readUInt16LE(local + 26) + buf.readUInt16LE(local + 28);
101
+ const data = buf.subarray(start, start + csize);
102
+ if (data.length !== csize) throw new Error('the archive is damaged');
103
+ let out;
104
+ if (method === 0) out = Buffer.from(data);
105
+ else if (method === 8) out = zlib.inflateRawSync(data, { maxOutputLength: MAX_FILE });
106
+ else throw new Error('the archive uses an unsupported compression method');
107
+ if (out.length !== usize) throw new Error('the archive is damaged');
108
+ return out;
109
+ }
110
+ return null;
111
+ }
112
+
113
+ function findEOCD(buf) {
114
+ const min = Math.max(0, buf.length - 22 - 0xffff);
115
+ for (let i = buf.length - 22; i >= min; i--) {
116
+ if (buf.readUInt32LE(i) === 0x06054b50) return i;
117
+ }
118
+ throw new Error('the archive is damaged');
119
+ }
120
+
121
+ module.exports = { fromTarGz, fromZip };
package/lib/install.js ADDED
@@ -0,0 +1,184 @@
1
+ 'use strict';
2
+
3
+ // Finds, or downloads once, the dotshield binary for this package's version.
4
+ //
5
+ // - Where: <state folder>/bin/v<version>/dotshield, where the state folder is
6
+ // $DOTSHIELD_HOME or ~/.dotshield, the folder the binary itself uses. The
7
+ // path stays the same across npx runs, so hooks that "dotshield guard
8
+ // install" writes keep working.
9
+ // - What: the release archive for this OS and CPU from the GitHub release of
10
+ // the same version, in the public dot-shield/dotshield-releases repository. checksums.json holds, for each archive, its SHA-256 and
11
+ // the SHA-256 of the binary inside it. The publish job writes it from the
12
+ // release's cosign-verified checksums.txt and the archives themselves. A
13
+ // package without checksums (a development copy) never downloads.
14
+ // - Every run checks the stored binary against its checksum before running
15
+ // it, and replaces it if it doesn't match.
16
+ // - No install script: nothing runs at npm install time.
17
+ //
18
+ // Messages never hold a URL or a value from the environment: a mirror URL
19
+ // can carry a token.
20
+
21
+ const crypto = require('node:crypto');
22
+ const fs = require('node:fs');
23
+ const os = require('node:os');
24
+ const path = require('node:path');
25
+ const { fromTarGz, fromZip } = require('./archive.js');
26
+
27
+ const pkg = require('../package.json');
28
+ // The public downloads repository. The source repository is private, and a
29
+ // private repository's release files can't be downloaded without signing in.
30
+ const RELEASES = 'https://github.com/dot-shield/dotshield-releases/releases/download/';
31
+ const MAX_ARCHIVE = 100 << 20;
32
+ const TIMEOUT_MS = 120_000;
33
+
34
+ const OS = { darwin: 'darwin', linux: 'linux', win32: 'windows' };
35
+ const ARCH = { x64: 'amd64', arm64: 'arm64' };
36
+
37
+ // target names the release archive and the binary inside it.
38
+ function target(platform, arch, version) {
39
+ const o = OS[platform];
40
+ const a = ARCH[arch];
41
+ if (!o || !a) {
42
+ throw new Error(`no release build for ${platform} ${arch}; see https://github.com/dot-shield/dotshield-releases`);
43
+ }
44
+ const win = o === 'windows';
45
+ return {
46
+ archive: `dotshield_${version}_${o}_${a}.${win ? 'zip' : 'tar.gz'}`,
47
+ binary: win ? 'dotshield.exe' : 'dotshield',
48
+ };
49
+ }
50
+
51
+ function stateDir(env, home) {
52
+ if (env.DOTSHIELD_HOME) return env.DOTSHIELD_HOME;
53
+ if (!home) throw new Error('no home folder; set DOTSHIELD_HOME');
54
+ return path.join(home, '.dotshield');
55
+ }
56
+
57
+ // downloadBase is the release folder for a version. DOTSHIELD_DOWNLOAD_BASE
58
+ // points at a mirror; the checksums are checked all the same. A mirror URL
59
+ // may not carry credentials, a query or a fragment.
60
+ function downloadBase(env, version) {
61
+ const base = env.DOTSHIELD_DOWNLOAD_BASE || `${RELEASES}v${version}/`;
62
+ let u;
63
+ try {
64
+ u = new URL(base);
65
+ } catch {
66
+ throw new Error('DOTSHIELD_DOWNLOAD_BASE is not a URL');
67
+ }
68
+ const local = u.hostname === '127.0.0.1' || u.hostname === 'localhost';
69
+ if (u.protocol !== 'https:' && !(u.protocol === 'http:' && local)) {
70
+ throw new Error('DOTSHIELD_DOWNLOAD_BASE must be an https URL');
71
+ }
72
+ if (u.username || u.password || u.search || u.hash) {
73
+ throw new Error('DOTSHIELD_DOWNLOAD_BASE must not hold a user name, password, query or fragment');
74
+ }
75
+ return base.endsWith('/') ? base : `${base}/`;
76
+ }
77
+
78
+ async function download(url, name, fetchFn) {
79
+ const host = new URL(url).host;
80
+ let res;
81
+ try {
82
+ res = await fetchFn(url, { redirect: 'follow', signal: AbortSignal.timeout(TIMEOUT_MS) });
83
+ } catch (err) {
84
+ const why = err && err.name === 'TimeoutError' ? 'timed out' : (err && err.cause && err.cause.code) || 'network error';
85
+ throw new Error(`can't download ${name} from ${host}: ${why}`);
86
+ }
87
+ if (!res.ok) throw new Error(`can't download ${name} from ${host}: HTTP ${res.status}`);
88
+ if (Number(res.headers.get('content-length') || 0) > MAX_ARCHIVE) {
89
+ throw new Error(`${name} from ${host} is too large`);
90
+ }
91
+ const chunks = [];
92
+ let n = 0;
93
+ try {
94
+ for await (const chunk of res.body) {
95
+ n += chunk.length;
96
+ if (n > MAX_ARCHIVE) throw new Error('too large');
97
+ chunks.push(Buffer.from(chunk));
98
+ }
99
+ } catch (err) {
100
+ if (res.body && res.body.cancel) res.body.cancel().catch(() => {});
101
+ throw new Error(err.message === 'too large' ? `${name} from ${host} is too large` : `can't download ${name} from ${host}: the transfer failed`);
102
+ }
103
+ return Buffer.concat(chunks);
104
+ }
105
+
106
+ const sha256 = (b) => crypto.createHash('sha256').update(b).digest('hex');
107
+
108
+ // trusted reports whether the file at p is the binary with checksum want: a
109
+ // regular file, not a link, that only its owner (this user) may write.
110
+ function trusted(p, want) {
111
+ let st;
112
+ try {
113
+ st = fs.lstatSync(p);
114
+ } catch {
115
+ return false;
116
+ }
117
+ if (!st.isFile()) return false;
118
+ if (process.platform !== 'win32') {
119
+ if (typeof process.getuid === 'function' && st.uid !== process.getuid()) return false;
120
+ if (st.mode & 0o022) return false;
121
+ }
122
+ try {
123
+ return sha256(fs.readFileSync(p)) === want;
124
+ } catch {
125
+ return false;
126
+ }
127
+ }
128
+
129
+ // binaryPath returns the path of a verified binary, downloading it if needed.
130
+ async function binaryPath(opts = {}) {
131
+ const env = opts.env || process.env;
132
+ const log = opts.log || ((m) => process.stderr.write(m));
133
+ if (env.DOTSHIELD_BINARY) {
134
+ if (!path.isAbsolute(env.DOTSHIELD_BINARY)) throw new Error('DOTSHIELD_BINARY must be an absolute path');
135
+ log('dotshield: using the binary that DOTSHIELD_BINARY names, unchecked\n');
136
+ return env.DOTSHIELD_BINARY;
137
+ }
138
+ const version = opts.version || pkg.version;
139
+ const checksums = opts.checksums || require('../checksums.json');
140
+ const t = target(opts.platform || process.platform, opts.arch || process.arch, version);
141
+ const dir = path.join(stateDir(env, opts.home === undefined ? os.homedir() : opts.home), 'bin', `v${version}`);
142
+ const bin = path.join(dir, t.binary);
143
+
144
+ const want = checksums[t.archive];
145
+ if (!want || !/^[0-9a-f]{64}$/.test(want.archive || '') || !/^[0-9a-f]{64}$/.test(want.binary || '')) {
146
+ throw new Error(`this package has no checksum for ${t.archive}, so it won't download it. ` +
147
+ 'A development copy needs DOTSHIELD_BINARY set to the absolute path of a dotshield binary you built.');
148
+ }
149
+ if (trusted(bin, want.binary)) return bin;
150
+
151
+ const url = downloadBase(env, version) + t.archive;
152
+ log(`dotshield: downloading v${version} (${t.archive}) to ${dir}\n`);
153
+ const archive = await download(url, t.archive, opts.fetch || fetch);
154
+ if (sha256(archive) !== want.archive) {
155
+ throw new Error(`${t.archive} doesn't match its checksum, so it was not used`);
156
+ }
157
+ const data = t.archive.endsWith('.zip') ? fromZip(archive, t.binary) : fromTarGz(archive, t.binary);
158
+ if (!data) throw new Error(`${t.archive} holds no ${t.binary}`);
159
+ if (sha256(data) !== want.binary) {
160
+ throw new Error(`the ${t.binary} in ${t.archive} doesn't match its checksum, so it was not used`);
161
+ }
162
+
163
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
164
+ const tmp = path.join(dir, `.${t.binary}.${process.pid}.${crypto.randomBytes(4).toString('hex')}`);
165
+ try {
166
+ const fd = fs.openSync(tmp, 'wx', 0o755);
167
+ try {
168
+ fs.writeSync(fd, data);
169
+ fs.fsyncSync(fd);
170
+ } finally {
171
+ fs.closeSync(fd);
172
+ }
173
+ // A file that failed the check above is replaced, not run.
174
+ fs.renameSync(tmp, bin);
175
+ } catch (err) {
176
+ fs.rmSync(tmp, { force: true });
177
+ // Another run may have put a good copy there first.
178
+ if (!trusted(bin, want.binary)) throw new Error(`can't write ${bin}: ${err.code || 'write failed'}`);
179
+ }
180
+ if (!trusted(bin, want.binary)) throw new Error(`${bin} doesn't match its checksum after writing it`);
181
+ return bin;
182
+ }
183
+
184
+ module.exports = { binaryPath, target, downloadBase };
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
2
  "name": "dotshield",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-beta.2",
4
+ "description": "Keeps secrets away from AI coding agents. This package runs the signed dotshield binary.",
5
+ "license": "Apache-2.0",
6
+ "homepage": "https://github.com/dot-shield/dotshield-releases",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/dot-shield/dotshield.git",
10
+ "directory": "packages/npm"
11
+ },
12
+ "bin": {
13
+ "dotshield": "bin/dotshield.js"
14
+ },
15
+ "files": [
16
+ "bin/",
17
+ "lib/",
18
+ "checksums.json",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
22
+ "engines": {
23
+ "node": ">=18"
24
+ },
25
+ "scripts": {
26
+ "test": "node --test \"test/*.test.js\""
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ }
31
+ }