Skip to content

Commit 997816e

Browse files
authored
feat(CodeSigningPlugin): auto-embed public key into native project files (#1381)
1 parent 856f1c1 commit 997816e

9 files changed

Lines changed: 1174 additions & 20 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": minor
3+
---
4+
5+
Add `publicKeyPath` and `nativeProjectPaths` options to `CodeSigningPlugin`. When `publicKeyPath` is set, the plugin automatically embeds the public key into `Info.plist` (iOS) and `strings.xml` (Android) during compilation, removing the need for manual native file setup. The `embedPublicKey` utility is also exported for standalone use.

‎packages/repack/src/plugins/CodeSigningPlugin/CodeSigningPlugin.ts‎

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ import type { Compiler as RspackCompiler } from '@rspack/core';
55
import jwt from 'jsonwebtoken';
66
import type { Compiler as WebpackCompiler } from 'webpack';
77
import { type CodeSigningPluginConfig, validateConfig } from './config.js';
8+
import { embedPublicKey } from './embedPublicKey.js';
9+
10+
function resolveProjectPath(
11+
projectRoot: string,
12+
configPath?: string
13+
): string | undefined {
14+
if (!configPath) return undefined;
15+
return path.isAbsolute(configPath)
16+
? configPath
17+
: path.resolve(projectRoot, configPath);
18+
}
819

920
export class CodeSigningPlugin {
1021
/**
@@ -55,6 +66,82 @@ export class CodeSigningPlugin {
5566
);
5667
}
5768

69+
private embedPublicKeyInNativeProjects(compiler: RspackCompiler) {
70+
if (!this.config.publicKeyPath) {
71+
return;
72+
}
73+
74+
const logger = compiler.getInfrastructureLogger('RepackCodeSigningPlugin');
75+
const projectRoot = compiler.context;
76+
77+
const publicKeyPath = resolveProjectPath(
78+
projectRoot,
79+
this.config.publicKeyPath
80+
);
81+
82+
if (!publicKeyPath) return;
83+
84+
if (!fs.existsSync(publicKeyPath)) {
85+
logger.warn(
86+
`Public key not found at ${publicKeyPath}. ` +
87+
'Skipping automatic embedding into native project files.'
88+
);
89+
return;
90+
}
91+
92+
const result = embedPublicKey({
93+
publicKeyPath,
94+
projectRoot,
95+
iosInfoPlistPath: resolveProjectPath(
96+
projectRoot,
97+
this.config.nativeProjectPaths?.ios
98+
),
99+
androidStringsXmlPath: resolveProjectPath(
100+
projectRoot,
101+
this.config.nativeProjectPaths?.android
102+
),
103+
});
104+
105+
if (result.error) {
106+
logger.warn(result.error);
107+
return;
108+
}
109+
110+
if (result.ios.modified) {
111+
logger.info(`Embedded public key in iOS Info.plist: ${result.ios.path}`);
112+
} else if (result.ios.error) {
113+
logger.warn(`Failed to embed public key in iOS: ${result.ios.error}`);
114+
} else if (result.ios.path) {
115+
logger.debug(
116+
`Public key already up-to-date in iOS Info.plist: ${result.ios.path}`
117+
);
118+
} else {
119+
logger.warn(
120+
'Could not find iOS Info.plist. Skipping auto-embedding for iOS. ' +
121+
'Use nativeProjectPaths.ios or manually add the public key to Info.plist.'
122+
);
123+
}
124+
125+
if (result.android.modified) {
126+
logger.info(
127+
`Embedded public key in Android strings.xml: ${result.android.path}`
128+
);
129+
} else if (result.android.error) {
130+
logger.warn(
131+
`Failed to embed public key in Android: ${result.android.error}`
132+
);
133+
} else if (result.android.path) {
134+
logger.debug(
135+
`Public key already up-to-date in Android strings.xml: ${result.android.path}`
136+
);
137+
} else {
138+
logger.warn(
139+
'Could not find Android strings.xml. Skipping auto-embedding for Android. ' +
140+
'Use nativeProjectPaths.android or manually add the public key to strings.xml.'
141+
);
142+
}
143+
}
144+
58145
apply(compiler: RspackCompiler): void;
59146
apply(compiler: WebpackCompiler): void;
60147

@@ -78,15 +165,31 @@ export class CodeSigningPlugin {
78165
*/
79166
const TOKEN_BUFFER_SIZE = 1280;
80167
/**
81-
* Used to denote beginning of the code-signing section of the bundle
168+
* Used to denote the beginning of the code-signing section of the bundle
82169
* alias for "Repack Code-Signing Signature Begin"
83170
*/
84171
const BEGIN_CS_MARK = '/* RCSSB */';
85172

86-
const privateKeyPath = path.isAbsolute(this.config.privateKeyPath)
87-
? this.config.privateKeyPath
88-
: path.resolve(compiler.context, this.config.privateKeyPath);
89-
const privateKey = fs.readFileSync(privateKeyPath);
173+
const privateKeyPath = resolveProjectPath(
174+
compiler.context,
175+
this.config.privateKeyPath
176+
);
177+
178+
if (!privateKeyPath) {
179+
throw new Error('[RepackCodeSigningPlugin] privateKeyPath is required.');
180+
}
181+
182+
let privateKey: Buffer;
183+
try {
184+
privateKey = fs.readFileSync(privateKeyPath);
185+
} catch (err: unknown) {
186+
const message = err instanceof Error ? err.message : String(err);
187+
throw new Error(
188+
`Failed to read private key from ${privateKeyPath}: ${message}`
189+
);
190+
}
191+
192+
this.embedPublicKeyInNativeProjects(compiler);
90193

91194
const excludedChunks = Array.isArray(this.config.excludeChunks)
92195
? this.config.excludeChunks

‎packages/repack/src/plugins/CodeSigningPlugin/config.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ export interface CodeSigningPluginConfig {
1010
privateKeyPath: string;
1111
/** Names of chunks to exclude from being signed. */
1212
excludeChunks?: string[] | RegExp | RegExp[];
13+
/**
14+
* Path to the public key file. When provided, the plugin will automatically
15+
* embed the public key into native project files (Info.plist for iOS,
16+
* strings.xml for Android) so that the runtime can verify signed bundles.
17+
*
18+
* Relative paths are resolved from the project root (compiler context).
19+
*/
20+
publicKeyPath?: string;
21+
/**
22+
* Override auto-detected paths to native project files where the public key
23+
* should be embedded. Only used when `publicKeyPath` is set.
24+
*/
25+
nativeProjectPaths?: {
26+
/** Path to iOS Info.plist. Auto-detected if not provided. */
27+
ios?: string;
28+
/** Path to Android strings.xml. Auto-detected if not provided. */
29+
android?: string;
30+
};
1331
}
1432

1533
type Schema = Parameters<typeof validate>[0];
@@ -18,7 +36,7 @@ export const optionsSchema: Schema = {
1836
type: 'object',
1937
properties: {
2038
enabled: { type: 'boolean' },
21-
privateKeyPath: { type: 'string' },
39+
privateKeyPath: { type: 'string', minLength: 1 },
2240
excludeChunks: {
2341
anyOf: [
2442
{
@@ -38,6 +56,15 @@ export const optionsSchema: Schema = {
3856
},
3957
],
4058
},
59+
publicKeyPath: { type: 'string', minLength: 1 },
60+
nativeProjectPaths: {
61+
type: 'object',
62+
properties: {
63+
ios: { type: 'string', minLength: 1 },
64+
android: { type: 'string', minLength: 1 },
65+
},
66+
additionalProperties: false,
67+
},
4168
},
4269
required: ['privateKeyPath'],
4370
additionalProperties: false,

0 commit comments

Comments
 (0)