Skip to content

Commit 1aa2584

Browse files
throw Missing protected data when getValue is invoked in a context without protected data
1 parent 6e7ab77 commit 1aa2584

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

packages/dataprotector-deserializer/src/index.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,31 @@ type Mode = 'optimistic' | 'legacy' | 'borsh';
4040
* - `protectedDataPath`: overrides the dataset path, by default use the standard dataset path provided in the iExec worker runtime
4141
*/
4242
class IExecDataProtectorDeserializer {
43-
private protectedDataPath: string;
43+
private protectedDataPath?: string;
4444

4545
private mode: Mode;
4646

47-
private zipPromise: Promise<JSZip>;
47+
private zipPromise?: Promise<JSZip>;
4848

4949
constructor(options?: { mode?: Mode; protectedDataPath?: string }) {
5050
this.mode = options?.mode || 'optimistic';
51-
this.protectedDataPath =
52-
options?.protectedDataPath ||
53-
join(process.env.IEXEC_IN, process.env.IEXEC_DATASET_FILENAME);
54-
this.zipPromise = readFile(this.protectedDataPath).then((buffer) => {
55-
return new JSZip().loadAsync(buffer);
56-
});
57-
this.zipPromise.catch(() => {
58-
/* prevents unhandled promise rejection */
59-
});
51+
if (options?.protectedDataPath) {
52+
this.protectedDataPath = options?.protectedDataPath;
53+
} else if (process.env.IEXEC_DATASET_FILENAME && process.env.IEXEC_IN) {
54+
this.protectedDataPath = join(
55+
process.env.IEXEC_IN,
56+
process.env.IEXEC_DATASET_FILENAME
57+
);
58+
}
59+
60+
if (this.protectedDataPath) {
61+
this.zipPromise = readFile(this.protectedDataPath).then((buffer) => {
62+
return new JSZip().loadAsync(buffer);
63+
});
64+
this.zipPromise.catch(() => {
65+
/* prevents unhandled promise rejection */
66+
});
67+
}
6068
}
6169

6270
/**
@@ -93,6 +101,9 @@ class IExecDataProtectorDeserializer {
93101
| StringSchemaFilter<T>
94102
| BinarySchemaFilter<T>
95103
> {
104+
if (this.zipPromise === undefined) {
105+
throw Error('Missing protected data');
106+
}
96107
const zip = await this.zipPromise.catch(() => {
97108
throw Error('Failed to load protected data');
98109
});

packages/dataprotector-deserializer/tests/index.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ import { writeFile } from 'fs/promises';
22
import {
33
createZipFromObject as legacyCreateZipFromObject,
44
extractDataSchema as legacyExtractDataSchema,
5-
} from '@iexec/dataprotector/dist/utils/data.js'; // run `prepare-test-deps` script before running this test file
6-
import { describe, it, beforeAll, expect } from '@jest/globals';
5+
} from '@iexec/dataprotector/dist/utils/data.js'; // run `test:prepare` script before running this test file
6+
import { describe, it, beforeAll, expect, beforeEach } from '@jest/globals';
77
import {
88
createZipFromObject,
99
extractDataSchema,
1010
} from '../../sdk/dist/src/utils/data.js';
1111
import { IExecDataProtectorDeserializer } from '../src/index.js';
1212

1313
describe('IExecDataProtectorDeserializer', () => {
14+
beforeEach(() => {
15+
// reset env
16+
delete process.env.IEXEC_IN;
17+
delete process.env.IEXEC_DATASET_FILENAME;
18+
});
1419
describe('constructor', () => {
1520
it('set default protectedDataPath with iexec envs', () => {
1621
process.env.IEXEC_IN = 'iexec_in';
@@ -27,6 +32,15 @@ describe('IExecDataProtectorDeserializer', () => {
2732
expect(protectedDataDeserializer['mode']).toBe('optimistic');
2833
});
2934
});
35+
describe('when used without protected data', () => {
36+
it('getValue() fails to load the data', async () => {
37+
// process.env.IEXEC_IN = 'iexec_in';
38+
const protectedDataDeserializer = new IExecDataProtectorDeserializer();
39+
await expect(
40+
protectedDataDeserializer.getValue('foo', 'string')
41+
).rejects.toThrow(Error('Missing protected data'));
42+
});
43+
});
3044
describe('with a file that is not a protected data', () => {
3145
it('getValue() fails to load the data', async () => {
3246
const protectedDataDeserializer = new IExecDataProtectorDeserializer({

0 commit comments

Comments
 (0)