-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmacos_verify_code_signing_spec.rb
More file actions
239 lines (193 loc) · 9.77 KB
/
Copy pathmacos_verify_code_signing_spec.rb
File metadata and controls
239 lines (193 loc) · 9.77 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# frozen_string_literal: true
require 'spec_helper'
describe Fastlane::Actions::MacosVerifyCodeSigningAction do
let(:authority) { 'Developer ID Application: Automattic, Inc. (ABCDE12345)' }
# Runs the action against artifacts that exist on disk, so that the existence
# check doesn't get in the way of asserting on the commands the action runs.
#
def with_artifacts(*names)
in_tmp_dir do |tmp_dir|
paths = names.map do |name|
path = File.join(tmp_dir, name)
# A `.app` is a directory and a `.dmg` a file, but the action only calls
# `File.exist?`, so either satisfies it.
FileUtils.mkdir_p(path)
path
end
yield(paths)
end
end
def expect_codesign_verify_deep(path, exitstatus: 0, output: '')
expect_shell_command('codesign', '--verify', '--deep', '--strict', '--verbose=2', path, exitstatus: exitstatus, output: output)
end
def expect_codesign_verify(path, exitstatus: 0, output: '')
expect_shell_command('codesign', '--verify', '--strict', '--verbose=2', path, exitstatus: exitstatus, output: output)
end
def expect_codesign_display(path, authority:)
expect_shell_command('codesign', '--display', '--verbose=2', path, output: "Executable=#{path}\nAuthority=#{authority}\n")
end
def expect_gatekeeper_assess_execute(path, exitstatus: 0)
expect_shell_command('spctl', '--assess', '--type', 'execute', '--verbose=2', path, exitstatus: exitstatus)
end
def expect_gatekeeper_assess_open(path, exitstatus: 0)
expect_shell_command('spctl', '--assess', '--type', 'open', '--context', 'context:primary-signature', '--verbose=2', path, exitstatus: exitstatus)
end
def expect_stapler_validate(path, exitstatus: 0)
expect_shell_command('xcrun', 'stapler', 'validate', path, exitstatus: exitstatus)
end
before do
allow_fastlane_action_sh
end
describe 'app bundles' do
it 'verifies the signature, Gatekeeper acceptance and stapled ticket' do
with_artifacts('Test.app') do |(path)|
expect_codesign_verify_deep(path)
expect_gatekeeper_assess_execute(path)
expect_stapler_validate(path)
run_described_fastlane_action(artifact_path: path)
end
end
it 'skips the notarization checks when `verify_notarization` is `false`' do
with_artifacts('Test.app') do |(path)|
expect_codesign_verify_deep(path)
expect(Open3).not_to receive(:popen2e).with('spctl', any_args)
expect(Open3).not_to receive(:popen2e).with('xcrun', any_args)
run_described_fastlane_action(artifact_path: path, verify_notarization: false)
end
end
it 'passes when signed by the expected authority' do
with_artifacts('Test.app') do |(path)|
expect_codesign_verify_deep(path)
expect_codesign_display(path, authority: authority)
expect_gatekeeper_assess_execute(path)
expect_stapler_validate(path)
run_described_fastlane_action(artifact_path: path, expected_authority: authority)
end
end
it 'fails when signed by a different authority' do
with_artifacts('Test.app') do |(path)|
expect_codesign_verify_deep(path)
expect_codesign_display(path, authority: 'Apple Development: Someone Else (ZZZZZ99999)')
expect { run_described_fastlane_action(artifact_path: path, expected_authority: authority) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /is not signed by '#{Regexp.escape(authority)}'/)
end
end
it 'fails when the signature is not valid' do
with_artifacts('Test.app') do |(path)|
expect_codesign_verify_deep(path, exitstatus: 1, output: "#{path}: invalid signature (code or signature have been modified)\n")
expect { run_described_fastlane_action(artifact_path: path) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /The code signature of .*Test\.app is not valid/)
end
end
# Unlike a disk image, an app bundle without a signature is always a failure.
it 'fails when it is not signed at all' do
with_artifacts('Test.app') do |(path)|
expect_codesign_verify_deep(path, exitstatus: 1, output: "#{path}: code object is not signed at all\n")
expect { run_described_fastlane_action(artifact_path: path) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.app is not signed at all/)
end
end
it 'fails when Gatekeeper rejects it' do
with_artifacts('Test.app') do |(path)|
expect_codesign_verify_deep(path)
expect_gatekeeper_assess_execute(path, exitstatus: 3)
expect { run_described_fastlane_action(artifact_path: path) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.app was rejected by Gatekeeper/)
end
end
it 'fails when it has no notarization ticket stapled' do
with_artifacts('Test.app') do |(path)|
expect_codesign_verify_deep(path)
expect_gatekeeper_assess_execute(path)
expect_stapler_validate(path, exitstatus: 65)
expect { run_described_fastlane_action(artifact_path: path) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.app has no notarization ticket stapled to it/)
end
end
end
describe 'disk images' do
# `electron-builder` signs the app inside the image but not the image itself,
# which is what our own `.dmg` artifacts look like.
it 'checks only the stapled ticket when the image is not signed' do
with_artifacts('Test.dmg') do |(path)|
expect_codesign_verify(path, exitstatus: 1, output: "#{path}: code object is not signed at all\n")
expect_stapler_validate(path)
expect(Open3).not_to receive(:popen2e).with('spctl', any_args)
expect(FastlaneCore::UI).to receive(:important).with(/is not signed — skipping its signature checks/)
run_described_fastlane_action(artifact_path: path)
end
end
it 'does not assert the authority of an unsigned image' do
with_artifacts('Test.dmg') do |(path)|
expect_codesign_verify(path, exitstatus: 1, output: "#{path}: code object is not signed at all\n")
expect_stapler_validate(path)
expect(Open3).not_to receive(:popen2e).with('codesign', '--display', any_args)
run_described_fastlane_action(artifact_path: path, expected_authority: authority)
end
end
it 'checks Gatekeeper and the authority when the image is signed' do
with_artifacts('Test.dmg') do |(path)|
expect_codesign_verify(path)
expect_codesign_display(path, authority: authority)
expect_gatekeeper_assess_open(path)
expect_stapler_validate(path)
run_described_fastlane_action(artifact_path: path, expected_authority: authority)
end
end
it 'fails when the image carries a broken signature' do
with_artifacts('Test.dmg') do |(path)|
expect_codesign_verify(path, exitstatus: 1, output: "#{path}: invalid signature (code or signature have been modified)\n")
expect { run_described_fastlane_action(artifact_path: path) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /The code signature of .*Test\.dmg is not valid/)
end
end
it 'fails when it has no notarization ticket stapled' do
with_artifacts('Test.dmg') do |(path)|
expect_codesign_verify(path, exitstatus: 1, output: "#{path}: code object is not signed at all\n")
expect_stapler_validate(path, exitstatus: 65)
expect { run_described_fastlane_action(artifact_path: path) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.dmg has no notarization ticket stapled to it/)
end
end
it 'skips every check but the signature when `verify_notarization` is `false`' do
with_artifacts('Test.dmg') do |(path)|
expect_codesign_verify(path)
expect(Open3).not_to receive(:popen2e).with('spctl', any_args)
expect(Open3).not_to receive(:popen2e).with('xcrun', any_args)
run_described_fastlane_action(artifact_path: path, verify_notarization: false)
end
end
end
it 'verifies every artifact when given a list of paths' do
with_artifacts('One.app', 'Two.dmg') do |(app_path, dmg_path)|
expect_codesign_verify_deep(app_path)
expect_gatekeeper_assess_execute(app_path)
expect_stapler_validate(app_path)
expect_codesign_verify(dmg_path, exitstatus: 1, output: "#{dmg_path}: code object is not signed at all\n")
expect_stapler_validate(dmg_path)
run_described_fastlane_action(artifact_path: [app_path, dmg_path])
end
end
it 'fails on an artifact it does not know how to verify' do
with_artifacts('Test.zip') do |(path)|
expect { run_described_fastlane_action(artifact_path: path) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /Don't know how to verify .*Test\.zip/)
end
end
it 'fails when there is no artifact at the given path' do
expect { run_described_fastlane_action(artifact_path: '/path/to/Missing.app') }
.to raise_error(FastlaneCore::Interface::FastlaneError, %r{There is no artifact at /path/to/Missing\.app})
end
it 'fails when given an empty list of paths' do
expect { run_described_fastlane_action(artifact_path: []) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /`artifact_path` is empty/)
end
it 'fails when `artifact_path` is neither a String nor an Array' do
expect { run_described_fastlane_action(artifact_path: 42) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /'artifact_path' value must be either `Array` or `comma-separated String`!/)
end
it 'fails when `artifact_path` is an Array of something other than Strings' do
expect { run_described_fastlane_action(artifact_path: [42]) }
.to raise_error(FastlaneCore::Interface::FastlaneError, /`artifact_path` must be a String or an Array of Strings/)
end
end