Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add ssl pinning copy logic #6312

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions cli/src/tasks/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ export async function copy(
usesLiveUpdates = true;
}

let usesSSLPinning = false;
if (
allPlugins.filter(plugin => plugin.id === '@ionic-enterprise/ssl-pinning')
.length > 0
) {
usesSSLPinning = true;
}

if (platformName === config.ios.name) {
if (usesCapacitorPortals) {
await copyFederatedWebDirs(config, await config.ios.webDirAbs);
Expand All @@ -118,6 +126,13 @@ export async function copy(
config.ios.nativeTargetDirAbs,
);
}
if (usesSSLPinning && config.app.extConfig?.plugins?.SSLPinning?.certs) {
await copySSLCert(
config.app.extConfig.plugins.SSLPinning?.certs as unknown as string[],
config.app.rootDir,
await config.ios.webDirAbs,
);
}
await copyCapacitorConfig(config, config.ios.nativeTargetDirAbs);
const cordovaPlugins = await getCordovaPlugins(config, platformName);
await handleCordovaPluginsJS(cordovaPlugins, config, platformName);
Expand Down Expand Up @@ -145,6 +160,13 @@ export async function copy(
config.android.assetsDirAbs,
);
}
if (usesSSLPinning && config.app.extConfig?.plugins?.SSLPinning?.certs) {
await copySSLCert(
config.app.extConfig.plugins.SSLPinning?.certs as unknown as string[],
config.app.rootDir,
config.android.assetsDirAbs,
);
}
await copyCapacitorConfig(config, config.android.assetsDirAbs);
const cordovaPlugins = await getCordovaPlugins(config, platformName);
await handleCordovaPluginsJS(cordovaPlugins, config, platformName);
Expand Down Expand Up @@ -281,3 +303,45 @@ async function copySecureLiveUpdatesKey(
},
);
}

async function copySSLCert(
sslCertPaths: string[],
rootDir: string,
targetDir: string,
) {
const validCertPaths: string[] = [];
for (const sslCertPath of sslCertPaths) {
const certAbsFromPath = join(rootDir, sslCertPath);
if (!/^.+\.(cer)$/.test(certAbsFromPath)) {
logger.warn(
`Cannot copy file from ${c.strong(certAbsFromPath)}\n` +
`The file is not a .cer SSL Certificate file.`,
);

return;
}
if (!(await pathExists(certAbsFromPath))) {
logger.warn(
`Cannot copy SSL Certificate file from ${c.strong(certAbsFromPath)}\n` +
`SSL Certificate does not exist at specified path.`,
);

return;
}
validCertPaths.push(certAbsFromPath);
}
const certsDirAbsToPath = join(targetDir, 'certs');
const certsDirRelToDir = relative(rootDir, targetDir);
await runTask(
`Copying SSL Certificates from to ${certsDirRelToDir}`,
async () => {
const promises: Promise<void>[] = [];
for (const certPath of validCertPaths) {
promises.push(
fsCopy(certPath, join(certsDirAbsToPath, basename(certPath))),
);
}
return Promise.all(promises);
},
);
}