@safedep/pmg 0.17.1 → 0.17.3
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 +201 -0
- package/README.md +245 -29
- package/dist/bin.cjs +3 -0
- package/package.json +34 -42
- package/.npmignore +0 -17
- package/bin/pmg.js +0 -76
- package/config.js +0 -34
- package/install.js +0 -243
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,66 +1,282 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
|
|
2
|
+
<h1>Package Manager Guard (PMG)</h1>
|
|
3
3
|
</div>
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
<p align="center">
|
|
6
|
+
<strong>Block malicious npm and pip packages before they install.</strong><br>
|
|
7
|
+
Defense in depth for the package managers you already use.
|
|
8
|
+
</p>
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
<div align="center">
|
|
11
|
+
<img src="./docs/demo/pmg-intro.gif" width="800" alt="pmg in action">
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
<br>
|
|
15
|
+
|
|
16
|
+
<div align="center">
|
|
17
|
+
|
|
18
|
+
[](https://docs.safedep.io/pmg/quickstart)
|
|
19
|
+
[](https://safedep.io)
|
|
20
|
+
[](https://discord.gg/kAGEj25dCn)
|
|
21
|
+
[](https://tldrsec.com/p/tldr-sec-316)
|
|
8
22
|
|
|
9
|
-
|
|
23
|
+
[](https://goreportcard.com/report/github.com/safedep/pmg)
|
|
24
|
+

|
|
25
|
+

|
|
26
|
+
[](https://api.securityscorecards.dev/projects/github.com/safedep/pmg)
|
|
27
|
+
[](https://github.com/safedep/pmg/actions/workflows/codeql.yml)
|
|
28
|
+
|
|
29
|
+
</div>
|
|
10
30
|
|
|
11
31
|
## Why PMG?
|
|
12
32
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
33
|
+
Developers and AI coding agents install packages every day. Each `npm install` or `pip install` executes thousands of lines of code that nobody reviews.
|
|
34
|
+
|
|
35
|
+
Recent compromises in popular ecosystems:
|
|
36
|
+
|
|
37
|
+
- [**Mini Shai-Hulud**](https://safedep.io/mini-shai-hulud-strikes-again-314-npm-packages-compromised/) - 300+ popular packages compromised
|
|
38
|
+
- [**litellm 1.82.8**](https://safedep.io/malicious-litellm-1-82-8-analysis/) - a popular AI proxy library compromised to exfiltrate credentials
|
|
39
|
+
- [**telnyx 4.87.2**](https://safedep.io/malicious-telnyx-pypi-compromise/) - a legitimate telecom SDK hijacked on PyPI
|
|
40
|
+
- [**pino-sdk-v2**](https://safedep.io/malicious-npm-package-pino-sdk-v2-env-exfiltration/) - a typosquat package disguised as the popular pino logger
|
|
41
|
+
|
|
42
|
+
**PMG is free, open source (Apache 2.0), and requires no account or API key.** It intercepts every package install and checks it against [SafeDep's free community API](https://safedep.io) for known malware **before** code executes. Install it once, and it covers every `npm install`, `pip install`, and `poetry add` after that.
|
|
43
|
+
|
|
44
|
+
## How PMG Works
|
|
45
|
+
|
|
46
|
+
PMG takes a defense in depth approach. Zero config, works across Zsh, Bash, and Fish, and each install passes through the enabled protection layers before code runs, plus an audit trail after.
|
|
47
|
+
|
|
48
|
+
<div align="center">
|
|
49
|
+
<picture>
|
|
50
|
+
<source media="(prefers-color-scheme: dark)" srcset="./docs/assets/how-pmg-works-dark.svg">
|
|
51
|
+
<img alt="PMG defense in depth: install command intercepted by PMG, passed through Layer 1 Threat Intel, Layer 2 Cooldown, Layer 3 Sandbox, then run with an audit log entry" src="./docs/assets/how-pmg-works-light.svg" width="820">
|
|
52
|
+
</picture>
|
|
53
|
+
</div>
|
|
54
|
+
|
|
55
|
+
<details>
|
|
56
|
+
<summary><strong>Layer details</strong></summary>
|
|
57
|
+
|
|
58
|
+
- **Transparent Interception** - PMG wraps `npm`, `pip`, and other package managers. Developers and AI agents use the same commands. No workflow changes.
|
|
59
|
+
- **Layer 1: Threat Intelligence** - PMG checks every package against [SafeDep's real-time threat intelligence](https://safedep.io) before install. Known-malicious packages are blocked. No key, no login required.
|
|
60
|
+
- **Layer 2: Policy (Dependency Cooldown)** - PMG blocks package versions published inside a configurable cooldown window, so recently compromised versions are skipped during the window.
|
|
61
|
+
- **Layer 3: Opt-in Sandbox** - When sandboxing is enabled and configured, PMG runs installs inside OS-native sandboxes (macOS Seatbelt, Linux Landlock by default, or Bubblewrap fallback) so install scripts have restricted system access even if a threat slips past the first two layers.
|
|
62
|
+
- **Audit Logging** - PMG logs every install (what, when, from where) for a verifiable audit trail.
|
|
63
|
+
|
|
64
|
+
</details>
|
|
65
|
+
|
|
66
|
+
## How PMG Compares
|
|
67
|
+
|
|
68
|
+
PMG is the only free, open-source, install-time package firewall that covers developers and AI agents alike and ships with sandboxing and cooldown out of the box.
|
|
69
|
+
|
|
70
|
+
| Capability | PMG | Socket | Snyk | Dependabot |
|
|
71
|
+
| --------------------------------------- | --- | ------- | ---- | ---------- |
|
|
72
|
+
| OSS / built in public | ✓ | ✗ | ✗ | ✗ |
|
|
73
|
+
| No account or API key | ✓ | ✓ | ✗ | ✗ |
|
|
74
|
+
| Install-time malicious package blocking | ✓ | ✓ | ✗ | ✗ |
|
|
75
|
+
| Dependency cooldown policy | ✓ | ✗ | ✗ | ✗ |
|
|
76
|
+
| Runtime sandboxing | ✓ | ✗ | ✗ | ✗ |
|
|
77
|
+
| Protects AI coding agents transparently | ✓ | ✗ | ✗ | ✗ |
|
|
78
|
+
| Local audit logs | ✓ | ✗ | ✗ | ✗ |
|
|
79
|
+
| Known-CVE remediation PRs | ✗ | ✗ | ✓ | ✓ |
|
|
80
|
+
|
|
81
|
+
## Quick Start
|
|
16
82
|
|
|
17
|
-
|
|
83
|
+
### 1. Install
|
|
18
84
|
|
|
19
85
|
```bash
|
|
20
|
-
|
|
86
|
+
curl -fsSL https://raw.githubusercontent.com/safedep/pmg/main/install.sh | sh
|
|
21
87
|
```
|
|
22
88
|
|
|
23
|
-
|
|
89
|
+
> See [Installation](#installation) for Homebrew, npm, and other install methods.
|
|
90
|
+
|
|
91
|
+
### 2. Setup
|
|
92
|
+
|
|
93
|
+
Wire PMG into your shell so it intercepts package managers.
|
|
24
94
|
|
|
25
95
|
```bash
|
|
26
|
-
|
|
96
|
+
pmg setup install
|
|
97
|
+
# Restart your terminal to apply changes
|
|
27
98
|
```
|
|
28
99
|
|
|
29
|
-
|
|
100
|
+
> **Tip:** Re-run `pmg setup install` after upgrading PMG to pick up new configuration options.
|
|
30
101
|
|
|
31
|
-
|
|
102
|
+
Validate your installation and verify protection is working:
|
|
32
103
|
|
|
33
104
|
```bash
|
|
34
|
-
pmg setup
|
|
105
|
+
pmg setup doctor
|
|
35
106
|
```
|
|
36
107
|
|
|
37
|
-
|
|
108
|
+
### 3. Use
|
|
109
|
+
|
|
110
|
+
See PMG blocking threats.
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
npm install --no-cache --prefer-online safedep-test-pkg@0.1.3
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
> **Note:** `safedep-test-pkg` is a benign test package flagged as malicious in SafeDep's database for
|
|
117
|
+
> testing and verification purposes.
|
|
118
|
+
|
|
119
|
+
Continue using your package managers as usual, or let your AI coding agent run them. PMG sits in the path, blocking malicious packages.
|
|
38
120
|
|
|
39
121
|
```bash
|
|
40
122
|
npm install express
|
|
41
|
-
|
|
123
|
+
# or
|
|
42
124
|
pip install requests
|
|
43
125
|
```
|
|
44
126
|
|
|
45
|
-
|
|
127
|
+
## Supported Package Managers
|
|
128
|
+
|
|
129
|
+
PMG supports the tools you already use:
|
|
130
|
+
|
|
131
|
+
| Ecosystem | Tools | Command Example |
|
|
132
|
+
| ----------- | -------- | ------------------- |
|
|
133
|
+
| **Node.js** | `npm` | `npm install <pkg>` |
|
|
134
|
+
| | `pnpm` | `pnpm add <pkg>` |
|
|
135
|
+
| | `yarn` | `yarn add <pkg>` |
|
|
136
|
+
| | `bun` | `bun add <pkg>` |
|
|
137
|
+
| | `npx` | `npx <pkg>` |
|
|
138
|
+
| | `pnpx` | `pnpx <pkg>` |
|
|
139
|
+
| **Python** | `pip` | `pip install <pkg>` |
|
|
140
|
+
| | `poetry` | `poetry add <pkg>` |
|
|
141
|
+
| | `uv` | `uv add <pkg>` |
|
|
142
|
+
|
|
143
|
+
## Installation
|
|
144
|
+
|
|
145
|
+
<details>
|
|
146
|
+
<summary><strong>Install Script (MacOS/Linux)</strong></summary>
|
|
147
|
+
|
|
148
|
+
Downloads the latest release from GitHub, verifies its SHA-256 checksum, and installs to `$HOME/.local/bin` (if on `PATH`) or `/usr/local/bin`.
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
curl -fsSL https://raw.githubusercontent.com/safedep/pmg/main/install.sh | sh
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
</details>
|
|
155
|
+
|
|
156
|
+
<details>
|
|
157
|
+
<summary><strong>Homebrew (MacOS/Linux)</strong></summary>
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
brew tap safedep/tap
|
|
161
|
+
brew install safedep/tap/pmg
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
</details>
|
|
165
|
+
|
|
166
|
+
<details>
|
|
167
|
+
<summary><strong>NPM (Cross-Platform)</strong></summary>
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
npm install -g @safedep/pmg
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
> **Note:** NPM-based installs can be fragile when Node.js is managed by version managers like [`mise`](https://mise.jdx.dev/) or [`asdf`](https://asdf-vm.com/). The global `npm` bin path changes with the active Node version, so switching versions can leave `pmg` unavailable on `PATH` (or pointing to an old install). For these setups, prefer the install script or Homebrew.
|
|
174
|
+
|
|
175
|
+
</details>
|
|
176
|
+
|
|
177
|
+
<details>
|
|
178
|
+
<summary><strong>Go (Build from Source)</strong></summary>
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
# Ensure $(go env GOPATH)/bin is in your $PATH
|
|
182
|
+
go install github.com/safedep/pmg@latest
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
</details>
|
|
186
|
+
|
|
187
|
+
<details>
|
|
188
|
+
<summary><strong>Binary Download</strong></summary>
|
|
189
|
+
|
|
190
|
+
Download the latest binary for your platform from the [Releases Page](https://github.com/safedep/pmg/releases).
|
|
191
|
+
</details>
|
|
192
|
+
|
|
193
|
+
## GitHub Actions
|
|
194
|
+
|
|
195
|
+
Protect CI workflows with one step. PMG analyzes every `npm install`,
|
|
196
|
+
`pip install`, etc. in the job.
|
|
197
|
+
|
|
198
|
+
```yaml
|
|
199
|
+
# Consider pinning third-party Actions to a full commit SHA
|
|
200
|
+
- uses: actions/setup-node@v6
|
|
201
|
+
with:
|
|
202
|
+
node-version: 24
|
|
203
|
+
- uses: safedep/pmg@v1
|
|
204
|
+
- run: npm ci
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
By default you get malware blocking and dependency cooldown. Sandbox isolation
|
|
208
|
+
is opt-in via the `sandbox` input. Tune behavior via inputs (`paranoid`,
|
|
209
|
+
`sandbox`, `cooldown-days`, ...) or point
|
|
210
|
+
`config-file` at a YAML in the repo. See
|
|
211
|
+
[docs/github-action.md](docs/github-action.md) for the full reference.
|
|
212
|
+
|
|
213
|
+
## Uninstallation
|
|
214
|
+
|
|
215
|
+
Remove shell integration:
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
pmg setup remove
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
To also remove the PMG configuration file:
|
|
46
222
|
|
|
47
223
|
```bash
|
|
48
|
-
pmg
|
|
49
|
-
pmg pnpm add react
|
|
50
|
-
pmg pip install requests
|
|
224
|
+
pmg setup remove --config-file
|
|
51
225
|
```
|
|
52
226
|
|
|
53
|
-
|
|
227
|
+
Then uninstall PMG itself:
|
|
228
|
+
|
|
229
|
+
```bash
|
|
230
|
+
# Homebrew
|
|
231
|
+
brew uninstall safedep/tap/pmg
|
|
232
|
+
|
|
233
|
+
# NPM
|
|
234
|
+
npm uninstall -g @safedep/pmg
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Trust and Security
|
|
238
|
+
|
|
239
|
+
PMG builds are reproducible and signed.
|
|
240
|
+
|
|
241
|
+
- **Attestations**: GitHub and npm attestations guarantee artifact integrity.
|
|
242
|
+
- **Verification**: You can cryptographically prove the binary matches the source code.
|
|
243
|
+
- See [Trusting PMG](docs/trust.md) for verification steps.
|
|
244
|
+
|
|
245
|
+
## User Guide
|
|
246
|
+
|
|
247
|
+
- [Configuration](docs/config.md)
|
|
248
|
+
- [Trusted Packages Configuration](docs/trusted-packages.md)
|
|
249
|
+
- [Dependency Cooldown](docs/dependency-cooldown.md)
|
|
250
|
+
- [Proxy Mode Architecture](docs/proxy-mode.md)
|
|
251
|
+
- [Sandboxing](docs/sandbox.md)
|
|
252
|
+
|
|
253
|
+
## Support
|
|
254
|
+
|
|
255
|
+
If PMG saved you from a bad package, [star this repo](https://github.com/safedep/pmg). It helps others find it.
|
|
256
|
+
|
|
257
|
+
## Star History
|
|
258
|
+
|
|
259
|
+
<a href="https://star-history.com/#safedep/pmg&Date">
|
|
260
|
+
<picture>
|
|
261
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=safedep/pmg&type=Date&theme=dark" />
|
|
262
|
+
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=safedep/pmg&type=Date" />
|
|
263
|
+
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=safedep/pmg&type=Date" />
|
|
264
|
+
</picture>
|
|
265
|
+
</a>
|
|
266
|
+
|
|
267
|
+
## Contributing
|
|
268
|
+
|
|
269
|
+
Contributions welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for build and test instructions.
|
|
54
270
|
|
|
55
|
-
|
|
56
|
-
- Linux
|
|
57
|
-
- Windows
|
|
271
|
+
Thank you to all contributors ❤️
|
|
58
272
|
|
|
59
|
-
|
|
273
|
+
<a href="https://github.com/safedep/pmg/graphs/contributors">
|
|
274
|
+
<img src="https://contrib.rocks/image?repo=safedep/pmg" alt="Contributors to PMG" />
|
|
275
|
+
</a>
|
|
60
276
|
|
|
61
|
-
##
|
|
277
|
+
## Telemetry
|
|
62
278
|
|
|
63
|
-
|
|
279
|
+
PMG collects anonymous usage data. To disable, either:
|
|
64
280
|
|
|
65
|
-
-
|
|
66
|
-
-
|
|
281
|
+
- Set `disable_telemetry: true` in your PMG config file, or
|
|
282
|
+
- Export `PMG_DISABLE_TELEMETRY=true`.
|
package/dist/bin.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
let e=require("node:module"),t=require("node:path"),n=require("node:fs"),r=require("node:child_process");const i=(0,e.createRequire)(require("url").pathToFileURL(__filename).href);function a(){return`@safedep/pmg-${process.platform}-${process.arch}`}function o(e){let r=(0,t.dirname)(i.resolve(`${e}/package.json`)),a=process.platform===`win32`?`pmg.exe`:`pmg`,o=(0,t.join)(r,`bin`,a);if(!(0,n.existsSync)(o))throw Error(`Binary not found at ${o}. The platform package "${e}" is installed but does not contain bin/${a}.`);return o}function s(){let e=a(),t;try{t=o(e)}catch(t){let n=t instanceof Error?t.message:String(t);console.error([`Failed to locate the platform binary.`,`Host: ${process.platform}/${process.arch}`,`Expected platform package: ${e}`,n,``,`Common causes:`,`- optionalDependencies were omitted during install`,`- this platform/arch is not published yet`,`- the platform package was published without the binary in bin/`].join(`
|
|
3
|
+
`)),process.exit(1)}let n=(0,r.spawn)(t,process.argv.slice(2),{stdio:`inherit`});n.on(`exit`,(e,t)=>{if(t){process.kill(process.pid,t);return}process.exit(e??1)}),n.on(`error`,e=>{console.error(`Failed to spawn the binary: ${e}`),process.exit(1)})}s();
|
package/package.json
CHANGED
|
@@ -1,58 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@safedep/pmg",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"pmg": "bin/pmg.js"
|
|
7
|
-
},
|
|
8
|
-
"scripts": {
|
|
9
|
-
"preinstall": "echo \"Installing PMG binary for your platform...\"",
|
|
10
|
-
"postinstall": "node install.js"
|
|
11
|
-
},
|
|
3
|
+
"version": "0.17.3",
|
|
4
|
+
"description": "PMG - Package Manager Guard: protect developers from malicious packages",
|
|
5
|
+
"license": "Apache-2.0",
|
|
12
6
|
"keywords": [
|
|
13
7
|
"security",
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"vulnerability",
|
|
19
|
-
"dependency-security",
|
|
20
|
-
"safedep"
|
|
8
|
+
"supply-chain",
|
|
9
|
+
"packages",
|
|
10
|
+
"safedep",
|
|
11
|
+
"pmg"
|
|
21
12
|
],
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=18"
|
|
15
|
+
},
|
|
25
16
|
"repository": {
|
|
26
17
|
"type": "git",
|
|
27
18
|
"url": "git+https://github.com/safedep/pmg.git"
|
|
28
19
|
},
|
|
20
|
+
"homepage": "https://safedep.io",
|
|
29
21
|
"bugs": {
|
|
30
22
|
"url": "https://github.com/safedep/pmg/issues"
|
|
31
23
|
},
|
|
32
|
-
"
|
|
33
|
-
"
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"type": "module",
|
|
28
|
+
"bin": {
|
|
29
|
+
"pmg": "dist/bin.cjs"
|
|
34
30
|
},
|
|
35
|
-
"os": [
|
|
36
|
-
"darwin",
|
|
37
|
-
"linux",
|
|
38
|
-
"win32"
|
|
39
|
-
],
|
|
40
|
-
"cpu": [
|
|
41
|
-
"x64",
|
|
42
|
-
"arm64",
|
|
43
|
-
"ia32"
|
|
44
|
-
],
|
|
45
31
|
"files": [
|
|
46
|
-
"
|
|
47
|
-
"install.js",
|
|
48
|
-
"config.js",
|
|
49
|
-
"test.js",
|
|
50
|
-
"README.md",
|
|
51
|
-
".npmignore"
|
|
32
|
+
"dist/**"
|
|
52
33
|
],
|
|
53
|
-
"
|
|
54
|
-
"
|
|
34
|
+
"optionalDependencies": {
|
|
35
|
+
"@safedep/pmg-linux-arm64": "0.17.3",
|
|
36
|
+
"@safedep/pmg-linux-x64": "0.17.3",
|
|
37
|
+
"@safedep/pmg-darwin-arm64": "0.17.3",
|
|
38
|
+
"@safedep/pmg-win32-x64": "0.17.3",
|
|
39
|
+
"@safedep/pmg-darwin-x64": "0.17.3"
|
|
55
40
|
},
|
|
56
|
-
"
|
|
57
|
-
|
|
58
|
-
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "25.9.1",
|
|
43
|
+
"tsdown": "0.22.0",
|
|
44
|
+
"typescript": "6.0.3"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsdown",
|
|
48
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/.npmignore
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
# Exclude the actual binary (but keep the wrapper script)
|
|
2
|
-
bin/pmg
|
|
3
|
-
bin/pmg.exe
|
|
4
|
-
|
|
5
|
-
# Exclude temp files and directories
|
|
6
|
-
temp/
|
|
7
|
-
.temp/
|
|
8
|
-
|
|
9
|
-
# Exclude development files
|
|
10
|
-
node_modules/
|
|
11
|
-
.git/
|
|
12
|
-
.gitignore
|
|
13
|
-
*.log
|
|
14
|
-
.DS_Store
|
|
15
|
-
|
|
16
|
-
# Keep only the wrapper script in bin/
|
|
17
|
-
!bin/pmg.js
|
package/bin/pmg.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const fs = require("fs");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const { spawn } = require("child_process");
|
|
6
|
-
const { ORG_NAME, PACKAGE_NAME, BINARY_NAME } = require("../config");
|
|
7
|
-
|
|
8
|
-
const BINARY_NAME_WITH_EXT =
|
|
9
|
-
process.platform === "win32" ? `${BINARY_NAME}.exe` : BINARY_NAME;
|
|
10
|
-
const BINARY_PATH = path.join(__dirname, BINARY_NAME_WITH_EXT);
|
|
11
|
-
|
|
12
|
-
function main() {
|
|
13
|
-
// Check if binary exists
|
|
14
|
-
if (!fs.existsSync(BINARY_PATH)) {
|
|
15
|
-
console.error(`❌ ${BINARY_NAME_WITH_EXT} binary not found`);
|
|
16
|
-
console.error(
|
|
17
|
-
`Try reinstalling: npm install -g ${ORG_NAME}/${PACKAGE_NAME}`,
|
|
18
|
-
);
|
|
19
|
-
process.exit(1);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Verify binary is executable
|
|
23
|
-
try {
|
|
24
|
-
fs.accessSync(BINARY_PATH, fs.constants.F_OK | fs.constants.X_OK);
|
|
25
|
-
} catch (error) {
|
|
26
|
-
console.error(`❌ ${BINARY_NAME_WITH_EXT} is not executable`);
|
|
27
|
-
console.error(
|
|
28
|
-
`Try reinstalling: npm install -g ${ORG_NAME}/${PACKAGE_NAME}`,
|
|
29
|
-
);
|
|
30
|
-
process.exit(1);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Pass all arguments to the binary
|
|
34
|
-
const args = process.argv.slice(2);
|
|
35
|
-
|
|
36
|
-
// Spawn the binary with inherited stdio for proper terminal interaction
|
|
37
|
-
const child = spawn(BINARY_PATH, args, {
|
|
38
|
-
stdio: "inherit",
|
|
39
|
-
windowsHide: false,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// Handle process termination
|
|
43
|
-
child.on("error", (error) => {
|
|
44
|
-
console.error(
|
|
45
|
-
`❌ Failed to execute ${BINARY_NAME_WITH_EXT}: ${error.message}`,
|
|
46
|
-
);
|
|
47
|
-
console.error(
|
|
48
|
-
`Try reinstalling: npm install -g ${ORG_NAME}/${PACKAGE_NAME}`,
|
|
49
|
-
);
|
|
50
|
-
process.exit(1);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// Exit with the same code as the child process
|
|
54
|
-
child.on("exit", (code, signal) => {
|
|
55
|
-
if (signal) {
|
|
56
|
-
process.kill(process.pid, signal);
|
|
57
|
-
} else {
|
|
58
|
-
process.exit(code || 0);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// Handle termination signals
|
|
63
|
-
process.on("SIGTERM", () => {
|
|
64
|
-
child.kill("SIGTERM");
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
process.on("SIGINT", () => {
|
|
68
|
-
child.kill("SIGINT");
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (require.main === module) {
|
|
73
|
-
main();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
module.exports = { main };
|
package/config.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
// Configuration for npm binary wrapper
|
|
2
|
-
|
|
3
|
-
const ORG_NAME = "@safedep";
|
|
4
|
-
const PACKAGE_NAME = "pmg";
|
|
5
|
-
const BINARY_NAME = "pmg";
|
|
6
|
-
|
|
7
|
-
// GitHub repository information for releases
|
|
8
|
-
const REPO_OWNER = "safedep";
|
|
9
|
-
const REPO_NAME = "pmg";
|
|
10
|
-
|
|
11
|
-
// GitHub releases base URL (constructed from repo info)
|
|
12
|
-
const GITHUB_RELEASES_BASE = `https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download`;
|
|
13
|
-
|
|
14
|
-
// Platform-specific binary filename patterns (GoReleaser format)
|
|
15
|
-
const BINARY_PATTERNS = {
|
|
16
|
-
"darwin-x64": `${BINARY_NAME}_Darwin_all.tar.gz`,
|
|
17
|
-
"darwin-arm64": `${BINARY_NAME}_Darwin_all.tar.gz`,
|
|
18
|
-
"linux-x64": `${BINARY_NAME}_Linux_x86_64.tar.gz`,
|
|
19
|
-
"linux-arm64": `${BINARY_NAME}_Linux_arm64.tar.gz`,
|
|
20
|
-
"linux-ia32": `${BINARY_NAME}_Linux_i386.tar.gz`,
|
|
21
|
-
"win32-x64": `${BINARY_NAME}_Windows_x86_64.zip`,
|
|
22
|
-
"win32-arm64": `${BINARY_NAME}_Windows_arm64.zip`,
|
|
23
|
-
"win32-ia32": `${BINARY_NAME}_Windows_i386.zip`,
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
module.exports = {
|
|
27
|
-
ORG_NAME,
|
|
28
|
-
PACKAGE_NAME,
|
|
29
|
-
BINARY_NAME,
|
|
30
|
-
REPO_OWNER,
|
|
31
|
-
REPO_NAME,
|
|
32
|
-
GITHUB_RELEASES_BASE,
|
|
33
|
-
BINARY_PATTERNS,
|
|
34
|
-
};
|
package/install.js
DELETED
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const fs = require("fs");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const os = require("os");
|
|
6
|
-
const https = require("https");
|
|
7
|
-
const crypto = require("crypto");
|
|
8
|
-
const { execSync } = require("child_process");
|
|
9
|
-
const {
|
|
10
|
-
BINARY_NAME,
|
|
11
|
-
REPO_OWNER,
|
|
12
|
-
REPO_NAME,
|
|
13
|
-
GITHUB_RELEASES_BASE,
|
|
14
|
-
BINARY_PATTERNS,
|
|
15
|
-
} = require("./config");
|
|
16
|
-
|
|
17
|
-
// Read version from package.json with strict validation
|
|
18
|
-
function getValidatedVersion() {
|
|
19
|
-
try {
|
|
20
|
-
const packageJson = JSON.parse(
|
|
21
|
-
fs.readFileSync(path.join(__dirname, "package.json"), "utf8"),
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const version = packageJson.version;
|
|
25
|
-
|
|
26
|
-
// Strict validation: must be valid semver (x.y.z)
|
|
27
|
-
if (!/^\d+\.\d+\.\d+$/.test(version)) {
|
|
28
|
-
throw new Error(`Invalid version format: ${version}`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return `v${version}`;
|
|
32
|
-
} catch (error) {
|
|
33
|
-
throw new Error(`Failed to read valid version: ${error.message}`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const RELEASE_VERSION = getValidatedVersion();
|
|
38
|
-
const BASE_URL = `${GITHUB_RELEASES_BASE}/${RELEASE_VERSION}`;
|
|
39
|
-
|
|
40
|
-
// Platform-specific binary URLs (constructed from config)
|
|
41
|
-
const BINARY_URLS = {};
|
|
42
|
-
Object.keys(BINARY_PATTERNS).forEach((platform) => {
|
|
43
|
-
BINARY_URLS[platform] = `${BASE_URL}/${BINARY_PATTERNS[platform]}`;
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
const CHECKSUMS_URL = `${BASE_URL}/checksums.txt`;
|
|
47
|
-
|
|
48
|
-
function getPlatformKey() {
|
|
49
|
-
const platform = process.platform;
|
|
50
|
-
const arch = process.arch;
|
|
51
|
-
return `${platform}-${arch}`;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function downloadFile(url, dest, maxRedirects = 5) {
|
|
55
|
-
return new Promise((resolve, reject) => {
|
|
56
|
-
if (maxRedirects < 0) {
|
|
57
|
-
reject(new Error("Too many redirects"));
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const file = fs.createWriteStream(dest);
|
|
62
|
-
|
|
63
|
-
https
|
|
64
|
-
.get(url, (response) => {
|
|
65
|
-
if (response.statusCode === 302 || response.statusCode === 301) {
|
|
66
|
-
file.close();
|
|
67
|
-
fs.unlink(dest, () => {});
|
|
68
|
-
return downloadFile(response.headers.location, dest, maxRedirects - 1)
|
|
69
|
-
.then(resolve)
|
|
70
|
-
.catch(reject);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (response.statusCode !== 200) {
|
|
74
|
-
file.close();
|
|
75
|
-
fs.unlink(dest, () => {});
|
|
76
|
-
reject(new Error(`Download failed: ${response.statusCode}`));
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
response.pipe(file);
|
|
81
|
-
|
|
82
|
-
file.on("finish", () => {
|
|
83
|
-
file.close();
|
|
84
|
-
resolve();
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
file.on("error", (err) => {
|
|
88
|
-
fs.unlink(dest, () => {});
|
|
89
|
-
reject(err);
|
|
90
|
-
});
|
|
91
|
-
})
|
|
92
|
-
.on("error", reject);
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function calculateChecksum(filePath) {
|
|
97
|
-
const fileBuffer = fs.readFileSync(filePath);
|
|
98
|
-
const hashSum = crypto.createHash("sha256");
|
|
99
|
-
hashSum.update(fileBuffer);
|
|
100
|
-
return hashSum.digest("hex");
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function validateChecksum(filePath, expectedChecksum) {
|
|
104
|
-
const actualChecksum = calculateChecksum(filePath);
|
|
105
|
-
return actualChecksum === expectedChecksum;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function extractArchive(archivePath, extractDir) {
|
|
109
|
-
const isZip = archivePath.endsWith(".zip");
|
|
110
|
-
|
|
111
|
-
if (isZip) {
|
|
112
|
-
if (process.platform === "win32") {
|
|
113
|
-
execSync(
|
|
114
|
-
`powershell -NoProfile -Command "Expand-Archive -Force -Path '${archivePath}' -DestinationPath '${extractDir}'"`,
|
|
115
|
-
{ stdio: "pipe" },
|
|
116
|
-
);
|
|
117
|
-
} else {
|
|
118
|
-
execSync(`unzip -o "${archivePath}" -d "${extractDir}"`, {
|
|
119
|
-
stdio: "pipe",
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
} else {
|
|
123
|
-
execSync(`tar -xzf "${archivePath}" -C "${extractDir}"`, { stdio: "pipe" });
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async function install() {
|
|
128
|
-
let tempWorkspace;
|
|
129
|
-
|
|
130
|
-
try {
|
|
131
|
-
console.log("📦 Installing PMG binary...");
|
|
132
|
-
|
|
133
|
-
// Get platform-specific URL
|
|
134
|
-
const platformKey = getPlatformKey();
|
|
135
|
-
const binaryUrl = BINARY_URLS[platformKey];
|
|
136
|
-
|
|
137
|
-
if (!binaryUrl) {
|
|
138
|
-
throw new Error(`Unsupported platform: ${platformKey}`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
console.log(`🔍 Platform: ${platformKey}`);
|
|
142
|
-
console.log(`📡 Version: ${RELEASE_VERSION}`);
|
|
143
|
-
|
|
144
|
-
// Create directories
|
|
145
|
-
const binDir = path.join(__dirname, "bin");
|
|
146
|
-
tempWorkspace = fs.mkdtempSync(path.join(os.tmpdir(), "pmg-install-"));
|
|
147
|
-
|
|
148
|
-
fs.mkdirSync(binDir, { recursive: true });
|
|
149
|
-
|
|
150
|
-
// Download binary archive
|
|
151
|
-
const archiveFilename = path.basename(binaryUrl);
|
|
152
|
-
const archivePath = path.join(tempWorkspace, archiveFilename);
|
|
153
|
-
|
|
154
|
-
console.log(`⬇️ Downloading binary...`);
|
|
155
|
-
await downloadFile(binaryUrl, archivePath);
|
|
156
|
-
|
|
157
|
-
// Download checksums
|
|
158
|
-
const checksumsPath = path.join(tempWorkspace, "checksums.txt");
|
|
159
|
-
console.log(`⬇️ Downloading checksums...`);
|
|
160
|
-
await downloadFile(CHECKSUMS_URL, checksumsPath);
|
|
161
|
-
|
|
162
|
-
// Parse checksums file
|
|
163
|
-
const checksumsContent = fs.readFileSync(checksumsPath, "utf8");
|
|
164
|
-
const checksumLines = checksumsContent.split("\n");
|
|
165
|
-
|
|
166
|
-
let expectedChecksum = null;
|
|
167
|
-
for (const line of checksumLines) {
|
|
168
|
-
if (line.includes(archiveFilename)) {
|
|
169
|
-
expectedChecksum = line.split(/\s+/)[0];
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (!expectedChecksum) {
|
|
175
|
-
throw new Error(`Checksum not found for ${archiveFilename}`);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// Validate checksum
|
|
179
|
-
console.log(`🔐 Validating checksum...`);
|
|
180
|
-
if (!validateChecksum(archivePath, expectedChecksum)) {
|
|
181
|
-
throw new Error(
|
|
182
|
-
"Checksum validation failed - binary may be corrupted or tampered",
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
console.log(`✅ Checksum validated`);
|
|
187
|
-
|
|
188
|
-
// Extract archive
|
|
189
|
-
console.log(`📂 Extracting binary...`);
|
|
190
|
-
extractArchive(archivePath, tempWorkspace);
|
|
191
|
-
|
|
192
|
-
// Find and move binary
|
|
193
|
-
const binaryName =
|
|
194
|
-
process.platform === "win32" ? `${BINARY_NAME}.exe` : BINARY_NAME;
|
|
195
|
-
const extractedBinaryPath = path.join(tempWorkspace, binaryName);
|
|
196
|
-
const finalBinaryPath = path.join(binDir, binaryName);
|
|
197
|
-
|
|
198
|
-
if (!fs.existsSync(extractedBinaryPath)) {
|
|
199
|
-
throw new Error(
|
|
200
|
-
`Binary not found at expected location: ${extractedBinaryPath}`,
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Move binary to final location (handle cross-device links)
|
|
205
|
-
try {
|
|
206
|
-
fs.renameSync(extractedBinaryPath, finalBinaryPath);
|
|
207
|
-
} catch (error) {
|
|
208
|
-
if (error.code === "EXDEV") {
|
|
209
|
-
// Cross-device link not permitted, copy and delete instead
|
|
210
|
-
fs.copyFileSync(extractedBinaryPath, finalBinaryPath);
|
|
211
|
-
fs.unlinkSync(extractedBinaryPath);
|
|
212
|
-
} else {
|
|
213
|
-
throw error;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// Make executable on Unix systems
|
|
218
|
-
if (process.platform !== "win32") {
|
|
219
|
-
fs.chmodSync(finalBinaryPath, "755");
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Clean up
|
|
223
|
-
fs.rmSync(tempWorkspace, { recursive: true, force: true });
|
|
224
|
-
|
|
225
|
-
console.log("✅ PMG binary installed successfully!");
|
|
226
|
-
} catch (error) {
|
|
227
|
-
console.error("❌ Installation failed:", error.message);
|
|
228
|
-
|
|
229
|
-
// Clean up on failure
|
|
230
|
-
try {
|
|
231
|
-
if (tempWorkspace && fs.existsSync(tempWorkspace)) {
|
|
232
|
-
fs.rmSync(tempWorkspace, { recursive: true, force: true });
|
|
233
|
-
}
|
|
234
|
-
} catch (cleanupError) {
|
|
235
|
-
console.warn("⚠️ Failed to clean up:", cleanupError.message);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
process.exit(1);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Run installation
|
|
243
|
-
install();
|