paylab-cli 0.0.0-stage → 0.1.0
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 +49 -2
- package/bin/sepay.js +316 -0
- package/package.json +42 -4
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 Derivative
|
|
95
|
+
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 2026 The paylab-cli authors
|
|
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,3 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# paylab-cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A command-line tool for building and testing a payment integration: log in, watch transactions,
|
|
4
|
+
register webhooks and stream them to `localhost`, and simulate all of it in sandbox.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install --save-dev paylab-cli@0.1.0
|
|
10
|
+
npx paylab-cli version
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**Always pin the version.** `paylab-cli@latest` and a bare `npx paylab-cli` resolve to whatever is current at
|
|
14
|
+
that moment, which is the opposite of what a lockfile is for.
|
|
15
|
+
|
|
16
|
+
In CI, install from the lockfile and leave install scripts off:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm ci --ignore-scripts
|
|
20
|
+
npx paylab-cli version
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
This package ships no install scripts at all, so `--ignore-scripts` costs you nothing here.
|
|
24
|
+
|
|
25
|
+
## How it works
|
|
26
|
+
|
|
27
|
+
`paylab-cli` contains no binary. It declares six optional dependencies — one per platform — and npm
|
|
28
|
+
installs only the one matching your machine:
|
|
29
|
+
|
|
30
|
+
| Platform | Package |
|
|
31
|
+
|---|---|
|
|
32
|
+
| macOS Intel | `@paylab/cli-darwin-x64` |
|
|
33
|
+
| macOS Apple silicon | `@paylab/cli-darwin-arm64` |
|
|
34
|
+
| Linux x86-64 | `@paylab/cli-linux-x64` |
|
|
35
|
+
| Linux arm64 | `@paylab/cli-linux-arm64` |
|
|
36
|
+
| Windows x86-64 | `@paylab/cli-win32-x64` |
|
|
37
|
+
| Windows arm64 | `@paylab/cli-win32-arm64` |
|
|
38
|
+
|
|
39
|
+
Before running the binary, `paylab-cli` checks its SHA-256 against a table built into this package, and
|
|
40
|
+
refuses to run on a mismatch. That table is derived, inside the release pipeline, from a
|
|
41
|
+
cosign-signed `checksums.txt`.
|
|
42
|
+
|
|
43
|
+
## Known limitations
|
|
44
|
+
|
|
45
|
+
1. **No shell completions.** Registering them needs an install script, and this package deliberately
|
|
46
|
+
has none.
|
|
47
|
+
2. **`npm install --omit=optional` (or `--no-optional`) breaks it.** The binary lives in an optional
|
|
48
|
+
dependency, so skipping those skips the binary. The package's error message says so.
|
|
49
|
+
|
|
50
|
+
Apache-2.0
|
package/bin/sepay.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
// The gatekeeper of the npm channel.
|
|
5
|
+
//
|
|
6
|
+
// Three things, in order: find the platform package matching this machine, check the
|
|
7
|
+
// binary's SHA-256 against the table baked into this package's own package.json, and
|
|
8
|
+
// only then exec it.
|
|
9
|
+
//
|
|
10
|
+
// Two deliberate departures from esbuild's launcher. Do not "fix" them back:
|
|
11
|
+
//
|
|
12
|
+
// 1. esbuild compares checksums in install.js, a script that runs at install time.
|
|
13
|
+
// This package runs no install script at all, so the comparison moves to run
|
|
14
|
+
// time, i.e. here. The reason: a customer who installs with --ignore-scripts
|
|
15
|
+
// must actually be protected by that flag.
|
|
16
|
+
//
|
|
17
|
+
// 2. esbuild has ESBUILD_BINARY_PATH to point at a different binary. There is no
|
|
18
|
+
// equivalent here and none may be added: one such variable disables the entire
|
|
19
|
+
// checksum chain in a single line. Same reason install.sh refuses a BASE_URL
|
|
20
|
+
// override.
|
|
21
|
+
//
|
|
22
|
+
// No third-party dependency. Each one is another entry to track for vulnerabilities
|
|
23
|
+
// (PCI-DSS 6.3.2) and another way in.
|
|
24
|
+
|
|
25
|
+
const { spawn } = require("child_process");
|
|
26
|
+
const crypto = require("crypto");
|
|
27
|
+
const fs = require("fs");
|
|
28
|
+
const os = require("os");
|
|
29
|
+
const path = require("path");
|
|
30
|
+
|
|
31
|
+
const pkg = require("../package.json");
|
|
32
|
+
|
|
33
|
+
const SUPPORTED = [
|
|
34
|
+
"darwin-x64",
|
|
35
|
+
"darwin-arm64",
|
|
36
|
+
"linux-x64",
|
|
37
|
+
"linux-arm64",
|
|
38
|
+
"win32-x64",
|
|
39
|
+
"win32-arm64",
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
const HOMEPAGE = "https://github.com/hoanghuy-sepay/paylab-cli";
|
|
43
|
+
|
|
44
|
+
// Message style matches install.sh: English, no colour, lowercase right after
|
|
45
|
+
// "Error:", detail lines indented two spaces, everything on stderr.
|
|
46
|
+
function fail(message) {
|
|
47
|
+
const details = Array.prototype.slice.call(arguments, 1);
|
|
48
|
+
let out = "Error: " + message + "\n";
|
|
49
|
+
for (let i = 0; i < details.length; i++) {
|
|
50
|
+
out += " " + details[i] + "\n";
|
|
51
|
+
}
|
|
52
|
+
// A write to a pipe is asynchronous on POSIX, and process.exit() below discards
|
|
53
|
+
// whatever is still queued, so this has to be a synchronous write or the customer
|
|
54
|
+
// gets a bare exit 1 with no reason. `npx sepay` in any CI pipes stderr.
|
|
55
|
+
try {
|
|
56
|
+
fs.writeSync(2, out);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
process.stderr.write(out);
|
|
59
|
+
}
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function sha256(file) {
|
|
64
|
+
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Cache of checksum results.
|
|
68
|
+
//
|
|
69
|
+
// The binary is ~13 MB, so hashing costs roughly 50 ms, and a CLI gets called inside
|
|
70
|
+
// shell loops.
|
|
71
|
+
//
|
|
72
|
+
// NOT os.tmpdir(): on a multi-user machine everyone can write there, so a planted
|
|
73
|
+
// cache file turns "verified" into something an attacker asserts about themselves.
|
|
74
|
+
// The cache lives in the user's own directory, AND is only trusted when the binary
|
|
75
|
+
// belongs to that same user — a global install (root-owned binary) always re-hashes,
|
|
76
|
+
// because there the cache sits on the weaker side of the binary. Windows has no
|
|
77
|
+
// usable uid here, so it does not cache at all.
|
|
78
|
+
|
|
79
|
+
function cacheFile() {
|
|
80
|
+
const base = process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache");
|
|
81
|
+
return path.join(base, "sepay", "verified.json");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function cacheKey(st, expected) {
|
|
85
|
+
// ctime is required, not redundant: no system call sets it, so every write to the
|
|
86
|
+
// file pushes it forward — including a binary overwritten in place at the same size
|
|
87
|
+
// with `touch -r` restoring mtime. Drop ctime and that is exactly the path you open,
|
|
88
|
+
// and the cache will assert a swapped binary has been verified.
|
|
89
|
+
return [st.dev, st.ino, st.size, Math.floor(st.mtimeMs), Math.floor(st.ctimeMs), expected].join(":");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function ownedByUs(st) {
|
|
93
|
+
return typeof process.getuid === "function" && st.uid === process.getuid();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function alreadyVerified(binPath, expected) {
|
|
97
|
+
if (process.platform === "win32") return false;
|
|
98
|
+
try {
|
|
99
|
+
const st = fs.statSync(binPath);
|
|
100
|
+
if (!ownedByUs(st)) return false;
|
|
101
|
+
const file = cacheFile();
|
|
102
|
+
const meta = fs.lstatSync(file);
|
|
103
|
+
if (!meta.isFile() || !ownedByUs(meta)) return false;
|
|
104
|
+
const data = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
105
|
+
return data[binPath] === cacheKey(st, expected);
|
|
106
|
+
} catch (e) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// `st` MUST be the file state read BEFORE hashing, not re-read here. Re-reading after
|
|
112
|
+
// hashing records the pair (NEW state, OLD hash): anyone who swaps the file between
|
|
113
|
+
// those two operations gets a permanent certificate from the cache, and every later
|
|
114
|
+
// run skips hashing. A momentary race becomes a standing hole.
|
|
115
|
+
function rememberVerified(binPath, expected, st) {
|
|
116
|
+
if (process.platform === "win32") return;
|
|
117
|
+
try {
|
|
118
|
+
if (!ownedByUs(st)) return;
|
|
119
|
+
const file = cacheFile();
|
|
120
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
121
|
+
let data = {};
|
|
122
|
+
try {
|
|
123
|
+
const meta = fs.lstatSync(file);
|
|
124
|
+
if (meta.isFile() && ownedByUs(meta)) {
|
|
125
|
+
data = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
126
|
+
}
|
|
127
|
+
} catch (e) {
|
|
128
|
+
data = {};
|
|
129
|
+
}
|
|
130
|
+
data[binPath] = cacheKey(st, expected);
|
|
131
|
+
// Write to a temp file and rename. A direct write follows a symlink, so a symlink
|
|
132
|
+
// planted at the cache path would have its target overwritten; and `mode` only
|
|
133
|
+
// applies when CREATING a file, so a pre-existing world-readable file keeps its
|
|
134
|
+
// permissions. Renaming replaces the inode, and both problems disappear.
|
|
135
|
+
const temp = file + "." + process.pid + ".tmp";
|
|
136
|
+
fs.writeFileSync(temp, JSON.stringify(data), { mode: 0o600 });
|
|
137
|
+
fs.renameSync(temp, file);
|
|
138
|
+
} catch (e) {
|
|
139
|
+
// A broken cache only makes the command slower. It must never make it fail.
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function main() {
|
|
144
|
+
const key = process.platform + "-" + process.arch;
|
|
145
|
+
|
|
146
|
+
if (SUPPORTED.indexOf(key) === -1) {
|
|
147
|
+
fail(
|
|
148
|
+
"unsupported platform: " + process.platform + " " + process.arch + ".",
|
|
149
|
+
"Supported: darwin/x64, darwin/arm64, linux/x64, linux/arm64, win32/x64, win32/arm64.",
|
|
150
|
+
"Install with the shell script instead: " + HOMEPAGE
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// The scope comes from what this wrapper declares, not from a literal repeated here.
|
|
155
|
+
// One source of truth, and a build under a different namespace needs no change.
|
|
156
|
+
const suffix = "/cli-" + key;
|
|
157
|
+
const matches = Object.keys(pkg.optionalDependencies || {}).filter(function (n) {
|
|
158
|
+
return n.slice(-suffix.length) === suffix;
|
|
159
|
+
});
|
|
160
|
+
if (matches.length !== 1) {
|
|
161
|
+
fail(
|
|
162
|
+
"this package declares " + matches.length + " platform dependencies for " + key + ", expected 1.",
|
|
163
|
+
"It was not built by the release pipeline."
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
const platformPkg = matches[0];
|
|
167
|
+
const binName = process.platform === "win32" ? "sepay.exe" : "sepay";
|
|
168
|
+
|
|
169
|
+
let pkgDir;
|
|
170
|
+
try {
|
|
171
|
+
pkgDir = path.dirname(require.resolve(platformPkg + "/package.json"));
|
|
172
|
+
} catch (e) {
|
|
173
|
+
// Two causes, not one. A message naming only the first sends the user round a
|
|
174
|
+
// reinstall loop that cannot succeed when the real problem is that the package is
|
|
175
|
+
// not on the registry.
|
|
176
|
+
fail(
|
|
177
|
+
"the sepay binary for " + process.platform + "/" + process.arch + " is not installed.",
|
|
178
|
+
"Expected package: " + platformPkg + "@" + pkg.version,
|
|
179
|
+
"Usually optional dependencies were skipped (npm install --no-optional or",
|
|
180
|
+
"--omit=optional). Reinstall with them enabled.",
|
|
181
|
+
"If they were not skipped, that package is not on the registry. Check with:",
|
|
182
|
+
" npm view " + platformPkg + "@" + pkg.version
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Resolve every symlink FIRST, then check the path is inside the package. Checking
|
|
187
|
+
// before resolving checks nothing: a symlink named bin/sepay pointing at /tmp/evil
|
|
188
|
+
// is still "inside the package" to a string comparison.
|
|
189
|
+
let realDir;
|
|
190
|
+
let binPath;
|
|
191
|
+
const expectedPath = function () {
|
|
192
|
+
return path.join(realDir, "bin", binName);
|
|
193
|
+
};
|
|
194
|
+
try {
|
|
195
|
+
realDir = fs.realpathSync(pkgDir);
|
|
196
|
+
binPath = fs.realpathSync(expectedPath());
|
|
197
|
+
} catch (e) {
|
|
198
|
+
fail(
|
|
199
|
+
"the sepay binary is missing from " + platformPkg + ".",
|
|
200
|
+
"Expected file: bin/" + binName,
|
|
201
|
+
"Reinstall the package."
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
if (binPath !== expectedPath()) {
|
|
205
|
+
fail(
|
|
206
|
+
"the sepay binary resolves outside its package.",
|
|
207
|
+
"Expected: " + expectedPath(),
|
|
208
|
+
"Actual: " + binPath,
|
|
209
|
+
"Do not run it. Reinstall the package."
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// A platform package left over from an earlier version is an ordinary install
|
|
214
|
+
// artefact, not a sign of tampering. Uncaught here it falls through to the checksum
|
|
215
|
+
// comparison and the user is told "do not run the binary" — which reads as a
|
|
216
|
+
// security incident.
|
|
217
|
+
try {
|
|
218
|
+
const platformVersion = require(path.join(realDir, "package.json")).version;
|
|
219
|
+
if (platformVersion !== pkg.version) {
|
|
220
|
+
fail(
|
|
221
|
+
"the installed " + platformPkg + " is version " + platformVersion + ", not " + pkg.version + ".",
|
|
222
|
+
"This happens when a previous version is left behind by an upgrade.",
|
|
223
|
+
"Remove node_modules and the lockfile entry, then reinstall."
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
} catch (e) {
|
|
227
|
+
// unreadable — let the checksum comparison below deal with it
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// No checksum table means STOP, not run anyway. A wrapper built without this table
|
|
231
|
+
// is a wrapper that can verify nothing — it has to fail loudly, not silently.
|
|
232
|
+
const table = (pkg.sepay && pkg.sepay.binaryHashes) || {};
|
|
233
|
+
const expected = table[key];
|
|
234
|
+
if (typeof expected !== "string" || !/^[a-f0-9]{64}$/.test(expected)) {
|
|
235
|
+
fail(
|
|
236
|
+
"no checksum is recorded for " + key + " in this package.",
|
|
237
|
+
"This package was not built by the release pipeline.",
|
|
238
|
+
"Do not run the binary. Reinstall from a published version."
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (!alreadyVerified(binPath, expected)) {
|
|
243
|
+
let stBefore = null;
|
|
244
|
+
try {
|
|
245
|
+
stBefore = fs.statSync(binPath);
|
|
246
|
+
} catch (e) {
|
|
247
|
+
stBefore = null; // unreadable: only the cache is lost
|
|
248
|
+
}
|
|
249
|
+
const actual = sha256(binPath);
|
|
250
|
+
if (actual !== expected) {
|
|
251
|
+
fail(
|
|
252
|
+
"checksum verification failed for the sepay binary.",
|
|
253
|
+
"Expected: " + expected,
|
|
254
|
+
"Actual: " + actual,
|
|
255
|
+
"Reinstall the package. If this persists, do not run the binary."
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
if (stBefore) rememberVerified(binPath, expected, stBefore);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const env = Object.assign({}, process.env);
|
|
262
|
+
delete env.NODE_OPTIONS; // NODE_OPTIONS=--require=/tmp/evil.js injects code into the
|
|
263
|
+
delete env.NODE_PATH; // child process if that process is also Node.
|
|
264
|
+
|
|
265
|
+
const child = spawn(binPath, process.argv.slice(2), {
|
|
266
|
+
stdio: "inherit", // share the terminal + keyboard with the user
|
|
267
|
+
shell: false, // explicit: with a shell, --ref "a & calc" becomes two commands
|
|
268
|
+
env: env,
|
|
269
|
+
windowsHide: true,
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// On Windows `child.kill(sig)` is TerminateProcess, not a signal: the binary never
|
|
273
|
+
// runs its own cleanup. A real Ctrl-C in a console is delivered by Windows to the
|
|
274
|
+
// whole process group, so that path still shuts down cleanly — only the
|
|
275
|
+
// child.kill/taskkill route from another program does not. Case 10 measures the real
|
|
276
|
+
// Ctrl-C path; nobody has measured the other one.
|
|
277
|
+
const forward = function (sig) {
|
|
278
|
+
return function () {
|
|
279
|
+
try {
|
|
280
|
+
child.kill(sig);
|
|
281
|
+
} catch (e) {
|
|
282
|
+
// child already gone
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
process.on("SIGINT", forward("SIGINT"));
|
|
287
|
+
process.on("SIGTERM", forward("SIGTERM"));
|
|
288
|
+
|
|
289
|
+
child.on("error", function (err) {
|
|
290
|
+
fail(
|
|
291
|
+
"could not start the sepay binary.",
|
|
292
|
+
err.code === "EACCES"
|
|
293
|
+
? "The file is not executable. Reinstall the package."
|
|
294
|
+
: String(err.message),
|
|
295
|
+
"Path: " + binPath
|
|
296
|
+
);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Pass the exit code through. Getting this wrong means a customer's CI reports
|
|
300
|
+
// success while the command failed — with a payments CLI that is the worst kind of
|
|
301
|
+
// bug. The exit-code table in `sepay --help` is the contract this preserves.
|
|
302
|
+
child.on("exit", function (code, signal) {
|
|
303
|
+
if (signal) {
|
|
304
|
+
const num = os.constants.signals[signal];
|
|
305
|
+
process.exit(num ? 128 + num : 1);
|
|
306
|
+
}
|
|
307
|
+
process.exit(code === null ? 1 : code);
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Never let Node print a raw stack trace at a customer.
|
|
312
|
+
try {
|
|
313
|
+
main();
|
|
314
|
+
} catch (e) {
|
|
315
|
+
fail("sepay could not start.", String((e && e.message) || e), "Reinstall the package.");
|
|
316
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "paylab-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Command-line tool for building and testing a payment integration",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cli",
|
|
7
|
+
"payments",
|
|
8
|
+
"webhook"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/hoanghuy-sepay/paylab-cli",
|
|
11
|
+
"bugs": "https://github.com/hoanghuy-sepay/paylab-cli/issues",
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/hoanghuy-sepay/paylab-cli.git"
|
|
16
|
+
},
|
|
17
|
+
"bin": {
|
|
18
|
+
"sepay": "bin/sepay.js"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"bin"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=18.0.0"
|
|
25
|
+
},
|
|
26
|
+
"optionalDependencies": {
|
|
27
|
+
"@paylab/cli-linux-arm64": "0.1.0",
|
|
28
|
+
"@paylab/cli-linux-x64": "0.1.0",
|
|
29
|
+
"@paylab/cli-darwin-x64": "0.1.0",
|
|
30
|
+
"@paylab/cli-darwin-arm64": "0.1.0",
|
|
31
|
+
"@paylab/cli-win32-x64": "0.1.0",
|
|
32
|
+
"@paylab/cli-win32-arm64": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"sepay": {
|
|
35
|
+
"binaryHashes": {
|
|
36
|
+
"linux-arm64": "a4462b96cf602711d1e7488be2a3579a051807fae717eb296faff1abe177f598",
|
|
37
|
+
"linux-x64": "100eb70cc1ff94280af3a00090647cc608ad6e21aaf71a00e26e93ca65a4757e",
|
|
38
|
+
"darwin-x64": "c35eb8e38c0486e4555c289e10d8d538fc536144a3b75fde159e337150b248b7",
|
|
39
|
+
"darwin-arm64": "b1786a410011b653d23db2cfd0f8587f86f4fba3f1edfaa5f87d4b15ea7b50b1",
|
|
40
|
+
"win32-x64": "0ec689cf0c39828cbdf193282d76bfb4afa29f67e50ce2b5279c0ef5ea2b392d",
|
|
41
|
+
"win32-arm64": "c560a1535fc2639146b7006598fb0393c7095d955170ad5769e67a12deb5d385"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|