git-userhub 3.0.6 → 3.0.8

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/bin/git-user.js CHANGED
@@ -4,21 +4,44 @@ const { spawn } = require('child_process');
4
4
  const fs = require('fs');
5
5
  const path = require('path');
6
6
  const os = require('os');
7
+ const https = require('https');
8
+ const crypto = require('crypto');
9
+ const tar = require('tar');
10
+ const PKG_JSON = require('../package.json');
11
+
12
+ const REPO = 'divyo-argha/git-user';
13
+ const VERSION = `v${PKG_JSON.version}`;
14
+
15
+ // --- START PINNED HASHES ---
16
+ const PINNED_HASHES = {
17
+ "git-user_darwin_arm64.tar.gz": {
18
+ "archive": "759e6a4137bc9caed58b393f751fa4c670a3dea98ef072a6551f38d41a9fc9ce",
19
+ "binary": "9b39841ca909f8dfcffff9a856771a65a8615ffb5e872e4c1108f0fa2e0d1752"
20
+ },
21
+ "git-user_darwin_x86_64.tar.gz": {
22
+ "archive": "eda5a46ea9f440174408d33659de38b7623da28b94ee447a3dc7fa509ffced83",
23
+ "binary": "efd1e62a40659f5e9132448ae5d1c5a5f23069a5277db73641de726cd28c55f1"
24
+ },
25
+ "git-user_linux_arm64.tar.gz": {
26
+ "archive": "3e311264bedf0672eee3625396d2e42d1c72d2a263a56fe2a7de9e5c54a2e2d3",
27
+ "binary": "f177a44c4c7d0a79840a404b669ce924c0117d58db6d407880007684efea64a2"
28
+ },
29
+ "git-user_linux_x86_64.tar.gz": {
30
+ "archive": "aab7e800a2b49a265409e0fbc3c64d4282b334bca222ea975532bd92dc7869dc",
31
+ "binary": "31172516986f3020bda2d5186ef7034919c09690399f849dcc980b542ed060f8"
32
+ },
33
+ "git-user_windows_x86_64.tar.gz": {
34
+ "archive": "eee27a75cfa131053b0901d8432216881aae25fda4586258b9b357ffa3558455",
35
+ "binary": "36e2ebc6272fe828b9b6a49d2623a049d001e826cca2c6ad428f178e9ca09403"
36
+ }
37
+ };
38
+ // --- END PINNED HASHES ---
7
39
 
8
- // Detect platform and architecture
9
40
  const platform = os.platform();
10
41
  const arch = os.arch();
11
42
 
12
- const platformMap = {
13
- 'darwin': 'darwin',
14
- 'linux': 'linux',
15
- 'win32': 'windows'
16
- };
17
-
18
- const archMap = {
19
- 'x64': 'amd64',
20
- 'arm64': 'arm64'
21
- };
43
+ const platformMap = { 'darwin': 'darwin', 'linux': 'linux', 'win32': 'windows' };
44
+ const archMap = { 'x64': 'x86_64', 'arm64': 'arm64' };
22
45
 
23
46
  const osName = platformMap[platform];
24
47
  const archName = archMap[arch];
@@ -29,38 +52,106 @@ if (!osName || !archName) {
29
52
  process.exit(1);
30
53
  }
31
54
 
32
- const pkgPlatform = platform === 'win32' ? 'windows' : platform;
33
- const pkgName = `git-userhub-${pkgPlatform}-${arch}`;
34
-
35
- let binaryPath;
36
- try {
37
- // Find the sub-package directory by resolving its package.json
38
- const subPkgPath = require.resolve(`${pkgName}/package.json`);
39
- binaryPath = path.join(path.dirname(subPkgPath), 'bin', `git-user${ext}`);
40
- } catch (e) {
41
- console.error(`❌ git-user native binary not installed!`);
42
- console.error(` npm should have installed the optional dependency '${pkgName}'.`);
43
- console.error(` Please try reinstalling the package.`);
44
- process.exit(1);
55
+ const finalBinaryName = `git-user-${platform}-${arch}${ext}`;
56
+ const finalBinaryPath = path.join(__dirname, finalBinaryName);
57
+ const assetName = `git-user_${osName}_${archName}.tar.gz`;
58
+ const pinnedData = PINNED_HASHES[assetName];
59
+
60
+ function computeHashSync(filePath) {
61
+ const hash = crypto.createHash('sha256');
62
+ const buffer = fs.readFileSync(filePath);
63
+ hash.update(buffer);
64
+ return hash.digest('hex');
45
65
  }
46
66
 
47
- if (!fs.existsSync(binaryPath)) {
48
- console.error(`❌ git-user binary not found at ${binaryPath}`);
49
- console.error(` Please ensure the package was correctly installed.`);
50
- process.exit(1);
67
+ function computeHash(file) {
68
+ return new Promise((resolve, reject) => {
69
+ const hash = crypto.createHash('sha256');
70
+ const stream = fs.createReadStream(file);
71
+ stream.on('data', d => hash.update(d));
72
+ stream.on('end', () => resolve(hash.digest('hex')));
73
+ stream.on('error', reject);
74
+ });
51
75
  }
52
76
 
53
- // Forward all arguments to the bundled binary
54
- const child = spawn(binaryPath, process.argv.slice(2), {
55
- stdio: 'inherit',
56
- shell: false
57
- });
77
+ function runBinary() {
78
+ if (pinnedData) {
79
+ const currentHash = computeHashSync(finalBinaryPath);
80
+ if (currentHash !== pinnedData.binary) {
81
+ console.error(`❌ Security Error: Cached binary hash mismatch! Deleting compromised binary.`);
82
+ fs.unlinkSync(finalBinaryPath);
83
+ process.exit(1);
84
+ }
85
+ }
58
86
 
59
- child.on('exit', (code) => {
60
- process.exit(code || 0);
61
- });
87
+ const child = spawn(finalBinaryPath, process.argv.slice(2), {
88
+ stdio: 'inherit',
89
+ shell: false
90
+ });
62
91
 
63
- child.on('error', (err) => {
64
- console.error('❌ Failed to start git-user:', err.message);
65
- process.exit(1);
66
- });
92
+ child.on('exit', (code) => process.exit(code || 0));
93
+ child.on('error', (err) => {
94
+ console.error('❌ Failed to start git-user:', err.message);
95
+ process.exit(1);
96
+ });
97
+ }
98
+
99
+ if (fs.existsSync(finalBinaryPath)) {
100
+ runBinary();
101
+ return;
102
+ }
103
+
104
+ // FIRST RUN: Download binary
105
+ console.log(`[git-user] Downloading cryptographically signed binary for ${platform}-${arch}...`);
106
+
107
+ function fetchFile(url, dest) {
108
+ return new Promise((resolve, reject) => {
109
+ https.get(url, { headers: { 'User-Agent': 'node' } }, (res) => {
110
+ if (res.statusCode === 301 || res.statusCode === 302) {
111
+ return fetchFile(res.headers.location, dest).then(resolve).catch(reject);
112
+ }
113
+ if (res.statusCode !== 200) return reject(new Error(`Download Error ${res.statusCode}`));
114
+ const file = fs.createWriteStream(dest);
115
+ res.pipe(file);
116
+ file.on('finish', () => { file.close(); resolve(); });
117
+ }).on('error', reject);
118
+ });
119
+ }
120
+
121
+ async function install() {
122
+ try {
123
+ const archivePath = path.join(__dirname, assetName);
124
+ const downloadUrl = `https://github.com/${REPO}/releases/download/${VERSION}/${assetName}`;
125
+
126
+ await fetchFile(downloadUrl, archivePath);
127
+
128
+ if (pinnedData) {
129
+ const archiveHash = await computeHash(archivePath);
130
+ if (archiveHash !== pinnedData.archive) {
131
+ throw new Error("Archive checksum mismatch! Connection may be compromised.");
132
+ }
133
+ }
134
+
135
+ const binaryNameInArchive = platform === 'win32' ? 'git-user.exe' : 'git-user';
136
+ await tar.extract({
137
+ file: archivePath,
138
+ cwd: __dirname,
139
+ filter: (p) => p.replace(/^\.\//, '') === binaryNameInArchive
140
+ });
141
+
142
+ const extractedPath = path.join(__dirname, binaryNameInArchive);
143
+ fs.renameSync(extractedPath, finalBinaryPath);
144
+
145
+ if (platform !== 'win32') fs.chmodSync(finalBinaryPath, 0o755);
146
+
147
+ fs.unlinkSync(archivePath);
148
+
149
+ console.log(`[git-user] Installation and verification complete.\n`);
150
+ runBinary();
151
+ } catch (err) {
152
+ console.error(`\n❌ git-user installation failed: ${err.message}`);
153
+ process.exit(1);
154
+ }
155
+ }
156
+
157
+ install();
package/package.json CHANGED
@@ -1,23 +1,13 @@
1
1
  {
2
2
  "name": "git-userhub",
3
- "version": "3.0.6",
3
+ "version": "3.0.8",
4
4
  "description": "Switch Git accounts in one command. No config editing. No SSH key chaos.",
5
5
  "bin": {
6
6
  "git-user": "bin/git-user.js"
7
7
  },
8
8
  "scripts": {
9
9
  "test": "echo \"No tests yet\" && exit 0",
10
- "release": "node scripts/release.js"
11
- },
12
- "optionalDependencies": {
13
- "git-userhub-darwin-arm64": "3.0.6",
14
- "git-userhub-darwin-x64": "3.0.6",
15
- "git-userhub-linux-arm64": "3.0.6",
16
- "git-userhub-linux-x64": "3.0.6",
17
- "git-userhub-windows-x64": "3.0.6"
18
- },
19
- "devDependencies": {
20
- "tar": "^7.4.3"
10
+ "prepublishOnly": "node scripts/inject-hashes.js"
21
11
  },
22
12
  "keywords": [
23
13
  "git",
@@ -54,5 +44,8 @@
54
44
  ],
55
45
  "config": {
56
46
  "repo": "divyo-argha/git-user"
47
+ },
48
+ "dependencies": {
49
+ "tar": "^7.5.16"
57
50
  }
58
- }
51
+ }
@@ -1,6 +0,0 @@
1
- 228e32fdccc3b553ddcefad445a75548c9df5ca03190879d7050008128449742 git-user_darwin_arm64.tar.gz
2
- 5191fda6093bf66946a53504bb3ef0accf776d74d9b3467e36c9cd2c8d7df1a1 git-user_darwin_x86_64.tar.gz
3
- e43a1171aeedc8f3bd92ddeb38e2b50fbabe5058c5874874c6295f8ea6ca716e git-user_linux_arm64.tar.gz
4
- fd97690f902462b0f68ab930dd5e6c63ba33a1d5999851787091cf745fcbec60 git-user_linux_x86_64.tar.gz
5
- 3b5ad0b0bfaeb0f2464b123d764cd5da9db2a3f496676e5d27c2ff8fc6957f75 git-user_windows_arm64.tar.gz
6
- 2a15ecc32d0ae3d419f541477aff8687463cd3ab600807d96ed44ec50ecc7503 git-user_windows_x86_64.tar.gz
@@ -1,4 +0,0 @@
1
- # git-userhub-darwin-arm64
2
- This package contains the native binary for git-userhub on darwin arm64.
3
-
4
- This is an internal package and shouldn't be installed directly. Install `git-userhub` instead.
@@ -1,19 +0,0 @@
1
- {
2
- "name": "git-userhub-darwin-arm64",
3
- "version": "3.0.6",
4
- "description": "The darwin arm64 binary for git-userhub",
5
- "os": [
6
- "darwin"
7
- ],
8
- "cpu": [
9
- "arm64"
10
- ],
11
- "bin": {
12
- "git-user": "bin/git-user"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/divyo-argha/git-user.git"
17
- },
18
- "license": "MIT"
19
- }
@@ -1,4 +0,0 @@
1
- # git-userhub-darwin-x64
2
- This package contains the native binary for git-userhub on darwin x64.
3
-
4
- This is an internal package and shouldn't be installed directly. Install `git-userhub` instead.
@@ -1,19 +0,0 @@
1
- {
2
- "name": "git-userhub-darwin-x64",
3
- "version": "3.0.6",
4
- "description": "The darwin x64 binary for git-userhub",
5
- "os": [
6
- "darwin"
7
- ],
8
- "cpu": [
9
- "x64"
10
- ],
11
- "bin": {
12
- "git-user": "bin/git-user"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/divyo-argha/git-user.git"
17
- },
18
- "license": "MIT"
19
- }
@@ -1,4 +0,0 @@
1
- # git-userhub-linux-arm64
2
- This package contains the native binary for git-userhub on linux arm64.
3
-
4
- This is an internal package and shouldn't be installed directly. Install `git-userhub` instead.
@@ -1,19 +0,0 @@
1
- {
2
- "name": "git-userhub-linux-arm64",
3
- "version": "3.0.6",
4
- "description": "The linux arm64 binary for git-userhub",
5
- "os": [
6
- "linux"
7
- ],
8
- "cpu": [
9
- "arm64"
10
- ],
11
- "bin": {
12
- "git-user": "bin/git-user"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/divyo-argha/git-user.git"
17
- },
18
- "license": "MIT"
19
- }
@@ -1,4 +0,0 @@
1
- # git-userhub-linux-x64
2
- This package contains the native binary for git-userhub on linux x64.
3
-
4
- This is an internal package and shouldn't be installed directly. Install `git-userhub` instead.
@@ -1,19 +0,0 @@
1
- {
2
- "name": "git-userhub-linux-x64",
3
- "version": "3.0.6",
4
- "description": "The linux x64 binary for git-userhub",
5
- "os": [
6
- "linux"
7
- ],
8
- "cpu": [
9
- "x64"
10
- ],
11
- "bin": {
12
- "git-user": "bin/git-user"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/divyo-argha/git-user.git"
17
- },
18
- "license": "MIT"
19
- }
@@ -1,4 +0,0 @@
1
- # git-userhub-windows-x64
2
- This package contains the native binary for git-userhub on win32 x64.
3
-
4
- This is an internal package and shouldn't be installed directly. Install `git-userhub` instead.
@@ -1,19 +0,0 @@
1
- {
2
- "name": "git-userhub-windows-x64",
3
- "version": "3.0.6",
4
- "description": "The win32 x64 binary for git-userhub",
5
- "os": [
6
- "win32"
7
- ],
8
- "cpu": [
9
- "x64"
10
- ],
11
- "bin": {
12
- "git-user": "bin/git-user.exe"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/divyo-argha/git-user.git"
17
- },
18
- "license": "MIT"
19
- }