Skip to content

Instantly share code, notes, and snippets.

@stephancasas
Created June 4, 2023 06:02
Show Gist options
  • Save stephancasas/e153ee91506e8faba748992d350e6277 to your computer and use it in GitHub Desktop.
Save stephancasas/e153ee91506e8faba748992d350e6277 to your computer and use it in GitHub Desktop.
Add root trust to Xcode iOS Simulator
#!/usr/bin/env osascript -l JavaScript
const CERT_PATH = '/tmp/sim-cert-install.pem';
// NOTE: This is intended to be used as an Alfred Workflow.
// If you don't have Alfred, just replace the value of
// `argv` with the URL for the site whose root trust
// needs to be added.
function run([argv = '']) {
if (!argv) {
return;
}
// Extract the canonical name and org from the HTTPS trace.
const { organization, canonicalName } = App.doShellScript(
`exec 2>&1 && curl -v ${argv}`,
).match(
/issuer:\sO=(?<organization>[^;]+);\sCN=(?<canonicalName>[^;]+);/i,
).groups;
// Locate the cert using its canonical name.
App.doShellScript(
`security find-certificate -c "${canonicalName}" -p > ${CERT_PATH}`,
);
// Did we find the cert? If not, play the noise.
//
// I'd have done this differently, but the Obj-C bridge kept screwing with
// the string encoding and upsetting the cert store. An inline variable
// for the shell script would work, but I'm not in the mood to write it.
//
if (!!App.doShellScript(`cat ${CERT_PATH}`).match(/not\sbe\sfound/i)) {
App.doShellScript(`afplay /System/Library/Sounds/Basso.aiff`);
return;
}
// Add the cert to the booted simulator device.
App.doShellScript(
[
`exec 2>&1`,
`xcrun simctl keychain booted add-root-cert '${CERT_PATH}'`,
`afplay /System/Library/Sounds/Funk.aiff`,
].join(' && '),
);
return 0;
}
const App = Application.currentApplication();
App.includeStandardAdditions = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment