-
Notifications
You must be signed in to change notification settings - Fork 6
/
artifact_authentication.test.ts
378 lines (361 loc) · 12.4 KB
/
artifact_authentication.test.ts
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import fs from 'fs';
import path from 'path';
import { dirSync } from 'tmp';
import { assert, err, ok } from '@votingworks/basics';
import { mockOf } from '@votingworks/test-utils';
import {
CastVoteRecordExportMetadata,
CVR,
safeParseJson,
} from '@votingworks/types';
import { getTestFilePath } from '../test/utils';
import {
ArtifactToExport,
ArtifactToImport,
authenticateArtifactUsingSignatureFile,
prepareSignatureFile,
} from './artifact_authentication';
import { ArtifactAuthenticationConfig } from './config';
jest.mock('@votingworks/types', (): typeof import('@votingworks/types') => ({
...jest.requireActual('@votingworks/types'),
safeParseJson: jest.fn(),
}));
/**
* The root hash for the mock cast vote records created in the beforeEach block
*/
const expectedCastVoteRecordRootHash =
'2b0e230f18ffbb4f40c00930b456fcc3a3b3c279c099a67d404a1bb03020cb3b';
const cvrId1 = 'a1234567-0000-0000-0000-000000000000';
const cvrId2 = 'a2345678-0000-0000-0000-000000000000';
const cvrId3 = 'a3456789-0000-0000-0000-000000000000';
let tempDirectoryPath: string;
let castVoteRecords: {
artifactToExport: ArtifactToExport;
artifactToImport: ArtifactToImport;
};
let electionPackage: {
artifactToExport: ArtifactToExport;
artifactToImport: ArtifactToImport;
};
beforeEach(() => {
// Avoid having to prepare a complete CVR.CastVoteRecordReport object for
// CastVoteRecordExportMetadata
mockOf(safeParseJson).mockImplementation((value) => ok(JSON.parse(value)));
tempDirectoryPath = dirSync().name;
// Prepare mock cast vote records
const castVoteRecordExportDirectoryPath = path.join(
tempDirectoryPath,
'cast-vote-record-export'
);
fs.mkdirSync(castVoteRecordExportDirectoryPath);
fs.mkdirSync(path.join(castVoteRecordExportDirectoryPath, cvrId1));
fs.mkdirSync(path.join(castVoteRecordExportDirectoryPath, cvrId2));
for (const { directoryName, fileName, fileContents } of [
{ directoryName: cvrId1, fileName: '1', fileContents: '1a' },
{ directoryName: cvrId1, fileName: '2', fileContents: '2a' },
{ directoryName: cvrId2, fileName: '1', fileContents: '1b' },
{ directoryName: cvrId2, fileName: '2', fileContents: '2b' },
]) {
fs.writeFileSync(
path.join(castVoteRecordExportDirectoryPath, directoryName, fileName),
fileContents
);
}
const castVoteRecordExportMetadata: CastVoteRecordExportMetadata = {
arePollsClosed: true,
castVoteRecordReportMetadata: {} as unknown as CVR.CastVoteRecordReport,
castVoteRecordRootHash: expectedCastVoteRecordRootHash,
};
fs.writeFileSync(
path.join(castVoteRecordExportDirectoryPath, 'metadata.json'),
JSON.stringify(castVoteRecordExportMetadata)
);
castVoteRecords = {
artifactToExport: {
type: 'cast_vote_records',
context: 'export',
directoryName: path.basename(castVoteRecordExportDirectoryPath),
metadataFileContents: JSON.stringify(castVoteRecordExportMetadata),
},
artifactToImport: {
type: 'cast_vote_records',
context: 'import',
directoryPath: castVoteRecordExportDirectoryPath,
},
};
// Prepare mock election package
const electionPackagePath = path.join(
tempDirectoryPath,
'election-package.zip'
);
fs.writeFileSync(electionPackagePath, 'abcd');
electionPackage = {
artifactToExport: {
type: 'election_package',
filePath: electionPackagePath,
},
artifactToImport: {
type: 'election_package',
filePath: electionPackagePath,
},
};
});
afterEach(() => {
fs.rmSync(tempDirectoryPath, { recursive: true });
});
const vxAdminTestConfig: ArtifactAuthenticationConfig = {
signingMachineCertPath: getTestFilePath({
fileType: 'vx-admin-cert-authority-cert.pem',
}),
signingMachinePrivateKey: {
source: 'file',
path: getTestFilePath({ fileType: 'vx-admin-private-key.pem' }),
},
vxCertAuthorityCertPath: getTestFilePath({
fileType: 'vx-cert-authority-cert.pem',
}),
};
const vxScanTestConfig: ArtifactAuthenticationConfig = {
signingMachineCertPath: getTestFilePath({
fileType: 'vx-scan-cert.pem',
}),
signingMachinePrivateKey: {
source: 'file',
path: getTestFilePath({ fileType: 'vx-scan-private-key.pem' }),
},
vxCertAuthorityCertPath: getTestFilePath({
fileType: 'vx-cert-authority-cert.pem',
}),
};
/**
* Defining artifacts in test.each configs directly results in compile-time errors due to variables
* being used before they're assigned to in the beforeEach block, so we use functions to defer
* variable access.
*/
type ArtifactGenerator = () => {
artifactToExport: ArtifactToExport;
artifactToImport: ArtifactToImport;
};
test.each<{
description: string;
artifactGenerator: ArtifactGenerator;
exportingMachineConfig: ArtifactAuthenticationConfig;
importingMachineConfig: ArtifactAuthenticationConfig;
}>([
{
description: 'cast vote records',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
},
{
description: 'election package',
artifactGenerator: () => electionPackage,
exportingMachineConfig: vxAdminTestConfig,
importingMachineConfig: vxScanTestConfig,
},
])(
'Preparing signature file and authenticating artifact using signature file - $description',
async ({
artifactGenerator,
exportingMachineConfig,
importingMachineConfig,
}) => {
const { artifactToExport, artifactToImport } = artifactGenerator();
const signatureFile = await prepareSignatureFile(
artifactToExport,
exportingMachineConfig
);
fs.writeFileSync(
path.join(tempDirectoryPath, signatureFile.fileName),
signatureFile.fileContents
);
expect(
await authenticateArtifactUsingSignatureFile(
artifactToImport,
importingMachineConfig
)
).toEqual(ok());
}
);
test.each<{
description: string;
artifactGenerator: ArtifactGenerator;
exportingMachineConfig: ArtifactAuthenticationConfig;
importingMachineConfig: ArtifactAuthenticationConfig;
tamperFn: () => void;
}>([
{
description: 'cast vote records, altered metadata file',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
tamperFn: () => {
assert(castVoteRecords.artifactToImport.type === 'cast_vote_records');
const { directoryPath } = castVoteRecords.artifactToImport;
const metadataFilePath = path.join(directoryPath, 'metadata.json');
const metadataFileContents = fs.readFileSync(metadataFilePath, 'utf-8');
const metadataFileContentsAltered = JSON.stringify({
...JSON.parse(metadataFileContents),
castVoteRecordRootHash: expectedCastVoteRecordRootHash.replace(
'a',
'b'
),
});
fs.writeFileSync(metadataFilePath, metadataFileContentsAltered);
},
},
{
description: 'cast vote records, removed metadata file',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
tamperFn: () => {
assert(castVoteRecords.artifactToImport.type === 'cast_vote_records');
const { directoryPath } = castVoteRecords.artifactToImport;
fs.rmSync(path.join(directoryPath, 'metadata.json'));
},
},
{
description: 'cast vote records, altered cast vote record file',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
tamperFn: () => {
assert(castVoteRecords.artifactToImport.type === 'cast_vote_records');
const { directoryPath } = castVoteRecords.artifactToImport;
fs.appendFileSync(path.join(directoryPath, cvrId1, '1'), '!');
},
},
{
description: 'cast vote records, added cast vote record file',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
tamperFn: () => {
assert(castVoteRecords.artifactToImport.type === 'cast_vote_records');
const { directoryPath } = castVoteRecords.artifactToImport;
fs.writeFileSync(path.join(directoryPath, cvrId1, '3'), '');
},
},
{
description: 'cast vote records, removed cast vote record file',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
tamperFn: () => {
assert(castVoteRecords.artifactToImport.type === 'cast_vote_records');
const { directoryPath } = castVoteRecords.artifactToImport;
fs.rmSync(path.join(directoryPath, cvrId1, '1'));
},
},
{
description:
'cast vote records, renamed cast vote record file (renamed such that the alphabetical order is unchanged)',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
tamperFn: () => {
assert(castVoteRecords.artifactToImport.type === 'cast_vote_records');
const { directoryPath } = castVoteRecords.artifactToImport;
fs.renameSync(
path.join(directoryPath, cvrId1, '1'),
path.join(directoryPath, cvrId1, '1-renamed')
);
},
},
{
description: 'cast vote records, added cast vote record directory',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
tamperFn: () => {
assert(castVoteRecords.artifactToImport.type === 'cast_vote_records');
const { directoryPath } = castVoteRecords.artifactToImport;
fs.mkdirSync(path.join(directoryPath, cvrId3));
},
},
{
description: 'cast vote records, removed cast vote record directory',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
tamperFn: () => {
assert(castVoteRecords.artifactToImport.type === 'cast_vote_records');
const { directoryPath } = castVoteRecords.artifactToImport;
fs.rmSync(path.join(directoryPath, cvrId2), { recursive: true });
},
},
{
description:
'cast vote records, renamed cast vote record directory (renamed such that the alphabetical order is unchanged)',
artifactGenerator: () => castVoteRecords,
exportingMachineConfig: vxScanTestConfig,
importingMachineConfig: vxAdminTestConfig,
tamperFn: () => {
assert(castVoteRecords.artifactToImport.type === 'cast_vote_records');
const { directoryPath } = castVoteRecords.artifactToImport;
// cp and cpSync are experimental so not recommended for use in production but fine for use
// in tests
fs.cpSync(
path.join(directoryPath, cvrId2),
path.join(directoryPath, cvrId3),
{ recursive: true }
);
fs.rmSync(path.join(directoryPath, cvrId2), { recursive: true });
},
},
{
description: 'election package',
artifactGenerator: () => electionPackage,
exportingMachineConfig: vxAdminTestConfig,
importingMachineConfig: vxScanTestConfig,
tamperFn: () => {
assert(electionPackage.artifactToImport.type === 'election_package');
const { filePath } = electionPackage.artifactToImport;
fs.appendFileSync(filePath, '!');
},
},
])(
'Detecting that an artifact has been tampered with - $description',
async ({
artifactGenerator,
exportingMachineConfig,
importingMachineConfig,
tamperFn,
}) => {
const { artifactToExport, artifactToImport } = artifactGenerator();
const signatureFile = await prepareSignatureFile(
artifactToExport,
exportingMachineConfig
);
fs.writeFileSync(
path.join(tempDirectoryPath, signatureFile.fileName),
signatureFile.fileContents
);
tamperFn();
expect(
await authenticateArtifactUsingSignatureFile(
artifactToImport,
importingMachineConfig
)
).toEqual(err(expect.any(Error)));
}
);
test('Error parsing cast vote record export metadata file', async () => {
mockOf(safeParseJson).mockImplementation(() => err(new Error('Whoa!')));
const signatureFile = await prepareSignatureFile(
castVoteRecords.artifactToExport,
vxScanTestConfig
);
fs.writeFileSync(
path.join(tempDirectoryPath, signatureFile.fileName),
signatureFile.fileContents
);
expect(
await authenticateArtifactUsingSignatureFile(
castVoteRecords.artifactToImport,
vxAdminTestConfig
)
).toEqual(err(expect.any(Error)));
});