forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathafter-sign.js
More file actions
183 lines (163 loc) · 5.98 KB
/
after-sign.js
File metadata and controls
183 lines (163 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* eslint-disable @typescript-eslint/no-require-imports */
/**
* electron-builder afterSign hook — code signing for macOS.
*
* When a real Developer ID certificate is available (CSC_LINK or CSC_NAME env
* vars are set), electron-builder handles signing automatically. This hook only
* runs a strict verification to confirm the signature is intact.
*
* When no certificate is available (local dev builds), falls back to ad-hoc
* signing so that electron-updater's ShipIt process can still validate the
* code signature.
*
* Ad-hoc signing order (inside-out):
* 1. All native binaries (.node, .dylib, .so)
* 2. All Frameworks (*.framework)
* 3. All Helper apps (*.app inside Frameworks/)
* 4. The main .app bundle
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
/**
* Ad-hoc sign a single path. Failures are logged but non-fatal to avoid
* breaking builds on edge-case binaries (e.g. debug symbols).
*/
function codesign(targetPath) {
try {
execSync(`codesign --force --sign - "${targetPath}"`, {
stdio: 'pipe',
timeout: 30000,
});
} catch (err) {
console.warn(`[afterSign] Failed to sign ${targetPath}: ${err.message}`);
}
}
/**
* Recursively collect all files matching the given extensions.
*/
function collectFiles(dir, extensions) {
const results = [];
if (!fs.existsSync(dir)) return results;
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
// Don't descend into .app or .framework bundles — they are signed as a unit
if (entry.name.endsWith('.app') || entry.name.endsWith('.framework')) {
continue;
}
results.push(...collectFiles(fullPath, extensions));
} else if (entry.isFile()) {
const ext = path.extname(entry.name);
if (extensions.includes(ext)) {
results.push(fullPath);
}
}
}
return results;
}
/**
* Collect bundle directories (.app, .framework) at a given depth.
*/
function collectBundles(dir, extension) {
const results = [];
if (!fs.existsSync(dir)) return results;
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory() && entry.name.endsWith(extension)) {
results.push(path.join(dir, entry.name));
}
}
return results;
}
module.exports = async function afterSign(context) {
const platform = context.packager.platform.name;
if (platform !== 'mac') return;
const appOutDir = context.appOutDir;
const appName = context.packager.appInfo.productFilename;
const appPath = path.join(appOutDir, `${appName}.app`);
if (!fs.existsSync(appPath)) {
console.warn(`[afterSign] macOS app not found at ${appPath}, skipping`);
return;
}
// ── Detect real (non-ad-hoc) code signature ───────────────────────────
// Check env vars first (CI path), then probe the actual signature on the
// .app bundle (covers the case where electron-builder auto-discovered a
// Developer ID certificate from the local Keychain).
let hasRealSignature = !!(process.env.CSC_LINK || process.env.CSC_NAME);
if (!hasRealSignature) {
try {
const info = execSync(`codesign -d --verbose=2 "${appPath}" 2>&1`, {
stdio: 'pipe',
timeout: 15000,
encoding: 'utf-8',
});
if (/Authority=Developer ID Application/.test(info)) {
hasRealSignature = true;
}
} catch {
// codesign -d fails if the bundle is unsigned — that's fine
}
}
if (hasRealSignature) {
console.log('[afterSign] Real code signing certificate detected (CSC_LINK/CSC_NAME set or Developer ID signature found).');
console.log('[afterSign] Skipping ad-hoc signing to preserve Developer ID signature.');
try {
execSync(`codesign --verify --deep --strict --verbose=4 "${appPath}"`, {
stdio: 'pipe',
timeout: 60000,
});
console.log('[afterSign] Developer ID signature verification passed.');
} catch (err) {
console.error('[afterSign] WARNING: Developer ID signature verification FAILED:', err.stderr?.toString() || err.message);
}
return;
}
// ── No certificate — ad-hoc signing fallback ─────────────────────────
console.log(`[afterSign] Ad-hoc signing ${appPath} (individual component signing)...`);
const contentsPath = path.join(appPath, 'Contents');
const frameworksPath = path.join(contentsPath, 'Frameworks');
let signed = 0;
// Step 1: Sign all native binaries (.node, .dylib, .so)
const nativeBinaries = collectFiles(contentsPath, ['.node', '.dylib', '.so']);
for (const bin of nativeBinaries) {
codesign(bin);
signed++;
}
if (nativeBinaries.length > 0) {
console.log(`[afterSign] Signed ${nativeBinaries.length} native binaries (.node/.dylib/.so)`);
}
// Step 2: Sign all Frameworks
const frameworks = collectBundles(frameworksPath, '.framework');
for (const fw of frameworks) {
codesign(fw);
signed++;
}
if (frameworks.length > 0) {
console.log(`[afterSign] Signed ${frameworks.length} frameworks`);
}
// Step 3: Sign all Helper apps
const helperApps = collectBundles(frameworksPath, '.app');
for (const helper of helperApps) {
codesign(helper);
signed++;
}
if (helperApps.length > 0) {
console.log(`[afterSign] Signed ${helperApps.length} helper apps`);
}
// Step 4: Sign the main app bundle
codesign(appPath);
signed++;
console.log(`[afterSign] Ad-hoc signing complete — ${signed} components signed`);
// Verify
try {
execSync(`codesign --verify --deep --strict "${appPath}"`, {
stdio: 'pipe',
timeout: 30000,
});
console.log('[afterSign] Signature verification passed (--deep --strict)');
} catch (err) {
console.error('[afterSign] WARNING: Signature verification FAILED:', err.stderr?.toString() || err.message);
}
};