|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe Fastlane::Actions::MacosVerifyCodeSigningAction do |
| 6 | + let(:authority) { 'Developer ID Application: Automattic, Inc. (ABCDE12345)' } |
| 7 | + |
| 8 | + # Runs the action against artifacts that exist on disk, so that the existence |
| 9 | + # check doesn't get in the way of asserting on the commands the action runs. |
| 10 | + # |
| 11 | + def with_artifacts(*names) |
| 12 | + in_tmp_dir do |tmp_dir| |
| 13 | + paths = names.map do |name| |
| 14 | + path = File.join(tmp_dir, name) |
| 15 | + # A `.app` is a directory and a `.dmg` a file, but the action only calls |
| 16 | + # `File.exist?`, so either satisfies it. |
| 17 | + FileUtils.mkdir_p(path) |
| 18 | + path |
| 19 | + end |
| 20 | + yield(paths) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + def expect_codesign_verify_deep(path, exitstatus: 0, output: '') |
| 25 | + expect_shell_command('codesign', '--verify', '--deep', '--strict', '--verbose=2', path, exitstatus: exitstatus, output: output) |
| 26 | + end |
| 27 | + |
| 28 | + def expect_codesign_verify(path, exitstatus: 0, output: '') |
| 29 | + expect_shell_command('codesign', '--verify', '--strict', '--verbose=2', path, exitstatus: exitstatus, output: output) |
| 30 | + end |
| 31 | + |
| 32 | + def expect_codesign_display(path, authority:) |
| 33 | + expect_shell_command('codesign', '--display', '--verbose=2', path, output: "Executable=#{path}\nAuthority=#{authority}\n") |
| 34 | + end |
| 35 | + |
| 36 | + def expect_gatekeeper_assess_execute(path, exitstatus: 0) |
| 37 | + expect_shell_command('spctl', '--assess', '--type', 'execute', '--verbose=2', path, exitstatus: exitstatus) |
| 38 | + end |
| 39 | + |
| 40 | + def expect_gatekeeper_assess_open(path, exitstatus: 0) |
| 41 | + expect_shell_command('spctl', '--assess', '--type', 'open', '--context', 'context:primary-signature', '--verbose=2', path, exitstatus: exitstatus) |
| 42 | + end |
| 43 | + |
| 44 | + def expect_stapler_validate(path, exitstatus: 0) |
| 45 | + expect_shell_command('xcrun', 'stapler', 'validate', path, exitstatus: exitstatus) |
| 46 | + end |
| 47 | + |
| 48 | + before do |
| 49 | + allow_fastlane_action_sh |
| 50 | + end |
| 51 | + |
| 52 | + describe 'app bundles' do |
| 53 | + it 'verifies the signature, Gatekeeper acceptance and stapled ticket' do |
| 54 | + with_artifacts('Test.app') do |(path)| |
| 55 | + expect_codesign_verify_deep(path) |
| 56 | + expect_gatekeeper_assess_execute(path) |
| 57 | + expect_stapler_validate(path) |
| 58 | + |
| 59 | + run_described_fastlane_action(artifact_path: path) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + it 'skips the notarization checks when `verify_notarization` is `false`' do |
| 64 | + with_artifacts('Test.app') do |(path)| |
| 65 | + expect_codesign_verify_deep(path) |
| 66 | + expect(Open3).not_to receive(:popen2e).with('spctl', any_args) |
| 67 | + expect(Open3).not_to receive(:popen2e).with('xcrun', any_args) |
| 68 | + |
| 69 | + run_described_fastlane_action(artifact_path: path, verify_notarization: false) |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + it 'passes when signed by the expected authority' do |
| 74 | + with_artifacts('Test.app') do |(path)| |
| 75 | + expect_codesign_verify_deep(path) |
| 76 | + expect_codesign_display(path, authority: authority) |
| 77 | + expect_gatekeeper_assess_execute(path) |
| 78 | + expect_stapler_validate(path) |
| 79 | + |
| 80 | + run_described_fastlane_action(artifact_path: path, expected_authority: authority) |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + it 'fails when signed by a different authority' do |
| 85 | + with_artifacts('Test.app') do |(path)| |
| 86 | + expect_codesign_verify_deep(path) |
| 87 | + expect_codesign_display(path, authority: 'Apple Development: Someone Else (ZZZZZ99999)') |
| 88 | + |
| 89 | + expect { run_described_fastlane_action(artifact_path: path, expected_authority: authority) } |
| 90 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /is not signed by '#{Regexp.escape(authority)}'/) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + it 'fails when the signature is not valid' do |
| 95 | + with_artifacts('Test.app') do |(path)| |
| 96 | + expect_codesign_verify_deep(path, exitstatus: 1, output: "#{path}: invalid signature (code or signature have been modified)\n") |
| 97 | + |
| 98 | + expect { run_described_fastlane_action(artifact_path: path) } |
| 99 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /The code signature of .*Test\.app is not valid/) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + # Unlike a disk image, an app bundle without a signature is always a failure. |
| 104 | + it 'fails when it is not signed at all' do |
| 105 | + with_artifacts('Test.app') do |(path)| |
| 106 | + expect_codesign_verify_deep(path, exitstatus: 1, output: "#{path}: code object is not signed at all\n") |
| 107 | + |
| 108 | + expect { run_described_fastlane_action(artifact_path: path) } |
| 109 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.app is not signed at all/) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + it 'fails when Gatekeeper rejects it' do |
| 114 | + with_artifacts('Test.app') do |(path)| |
| 115 | + expect_codesign_verify_deep(path) |
| 116 | + expect_gatekeeper_assess_execute(path, exitstatus: 3) |
| 117 | + |
| 118 | + expect { run_described_fastlane_action(artifact_path: path) } |
| 119 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.app was rejected by Gatekeeper/) |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + it 'fails when it has no notarization ticket stapled' do |
| 124 | + with_artifacts('Test.app') do |(path)| |
| 125 | + expect_codesign_verify_deep(path) |
| 126 | + expect_gatekeeper_assess_execute(path) |
| 127 | + expect_stapler_validate(path, exitstatus: 65) |
| 128 | + |
| 129 | + expect { run_described_fastlane_action(artifact_path: path) } |
| 130 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.app has no notarization ticket stapled to it/) |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + describe 'disk images' do |
| 136 | + # `electron-builder` signs the app inside the image but not the image itself, |
| 137 | + # which is what our own `.dmg` artifacts look like. |
| 138 | + it 'checks only the stapled ticket when the image is not signed' do |
| 139 | + with_artifacts('Test.dmg') do |(path)| |
| 140 | + expect_codesign_verify(path, exitstatus: 1, output: "#{path}: code object is not signed at all\n") |
| 141 | + expect_stapler_validate(path) |
| 142 | + expect(Open3).not_to receive(:popen2e).with('spctl', any_args) |
| 143 | + expect(FastlaneCore::UI).to receive(:important).with(/is not signed — skipping its signature checks/) |
| 144 | + |
| 145 | + run_described_fastlane_action(artifact_path: path) |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + it 'does not assert the authority of an unsigned image' do |
| 150 | + with_artifacts('Test.dmg') do |(path)| |
| 151 | + expect_codesign_verify(path, exitstatus: 1, output: "#{path}: code object is not signed at all\n") |
| 152 | + expect_stapler_validate(path) |
| 153 | + expect(Open3).not_to receive(:popen2e).with('codesign', '--display', any_args) |
| 154 | + |
| 155 | + run_described_fastlane_action(artifact_path: path, expected_authority: authority) |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + it 'checks Gatekeeper and the authority when the image is signed' do |
| 160 | + with_artifacts('Test.dmg') do |(path)| |
| 161 | + expect_codesign_verify(path) |
| 162 | + expect_codesign_display(path, authority: authority) |
| 163 | + expect_gatekeeper_assess_open(path) |
| 164 | + expect_stapler_validate(path) |
| 165 | + |
| 166 | + run_described_fastlane_action(artifact_path: path, expected_authority: authority) |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | + it 'fails when the image carries a broken signature' do |
| 171 | + with_artifacts('Test.dmg') do |(path)| |
| 172 | + expect_codesign_verify(path, exitstatus: 1, output: "#{path}: invalid signature (code or signature have been modified)\n") |
| 173 | + |
| 174 | + expect { run_described_fastlane_action(artifact_path: path) } |
| 175 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /The code signature of .*Test\.dmg is not valid/) |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + it 'fails when it has no notarization ticket stapled' do |
| 180 | + with_artifacts('Test.dmg') do |(path)| |
| 181 | + expect_codesign_verify(path, exitstatus: 1, output: "#{path}: code object is not signed at all\n") |
| 182 | + expect_stapler_validate(path, exitstatus: 65) |
| 183 | + |
| 184 | + expect { run_described_fastlane_action(artifact_path: path) } |
| 185 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.dmg has no notarization ticket stapled to it/) |
| 186 | + end |
| 187 | + end |
| 188 | + |
| 189 | + it 'skips every check but the signature when `verify_notarization` is `false`' do |
| 190 | + with_artifacts('Test.dmg') do |(path)| |
| 191 | + expect_codesign_verify(path) |
| 192 | + expect(Open3).not_to receive(:popen2e).with('spctl', any_args) |
| 193 | + expect(Open3).not_to receive(:popen2e).with('xcrun', any_args) |
| 194 | + |
| 195 | + run_described_fastlane_action(artifact_path: path, verify_notarization: false) |
| 196 | + end |
| 197 | + end |
| 198 | + end |
| 199 | + |
| 200 | + it 'verifies every artifact when given a list of paths' do |
| 201 | + with_artifacts('One.app', 'Two.dmg') do |(app_path, dmg_path)| |
| 202 | + expect_codesign_verify_deep(app_path) |
| 203 | + expect_gatekeeper_assess_execute(app_path) |
| 204 | + expect_stapler_validate(app_path) |
| 205 | + |
| 206 | + expect_codesign_verify(dmg_path, exitstatus: 1, output: "#{dmg_path}: code object is not signed at all\n") |
| 207 | + expect_stapler_validate(dmg_path) |
| 208 | + |
| 209 | + run_described_fastlane_action(artifact_path: [app_path, dmg_path]) |
| 210 | + end |
| 211 | + end |
| 212 | + |
| 213 | + it 'fails on an artifact it does not know how to verify' do |
| 214 | + with_artifacts('Test.zip') do |(path)| |
| 215 | + expect { run_described_fastlane_action(artifact_path: path) } |
| 216 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /Don't know how to verify .*Test\.zip/) |
| 217 | + end |
| 218 | + end |
| 219 | + |
| 220 | + it 'fails when there is no artifact at the given path' do |
| 221 | + expect { run_described_fastlane_action(artifact_path: '/path/to/Missing.app') } |
| 222 | + .to raise_error(FastlaneCore::Interface::FastlaneError, %r{There is no artifact at /path/to/Missing\.app}) |
| 223 | + end |
| 224 | + |
| 225 | + it 'fails when given an empty list of paths' do |
| 226 | + expect { run_described_fastlane_action(artifact_path: []) } |
| 227 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /`artifact_path` is empty/) |
| 228 | + end |
| 229 | + |
| 230 | + it 'fails when `artifact_path` is neither a String nor an Array' do |
| 231 | + expect { run_described_fastlane_action(artifact_path: 42) } |
| 232 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /'artifact_path' value must be either `Array` or `comma-separated String`!/) |
| 233 | + end |
| 234 | + |
| 235 | + it 'fails when `artifact_path` is an Array of something other than Strings' do |
| 236 | + expect { run_described_fastlane_action(artifact_path: [42]) } |
| 237 | + .to raise_error(FastlaneCore::Interface::FastlaneError, /`artifact_path` must be a String or an Array of Strings/) |
| 238 | + end |
| 239 | +end |
0 commit comments