Skip to content

Commit 26efa49

Browse files
committed
validate submissionSource and raise if not valid
1 parent 52656dc commit 26efa49

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/util/submit-addon.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ export async function signAddon({
423423
const stats = await fsPromises.stat(xpiPath);
424424

425425
if (!stats.isFile()) {
426-
throw new Error(`not a file: ${xpiPath}`);
426+
throw new Error('not a file');
427427
}
428428
} catch (statError) {
429429
throw new Error(`error with ${xpiPath}: ${statError}`);
@@ -450,12 +450,20 @@ export async function signAddon({
450450
channel,
451451
savedUploadUuidPath,
452452
);
453+
const patchData = {};
453454
// if we have a source file we need to upload we patch after the create
454-
const patchData = {
455-
version: submissionSource
456-
? { source: client.fileFromSync(submissionSource) }
457-
: undefined,
458-
};
455+
if (submissionSource) {
456+
try {
457+
const stats2 = await fsPromises.stat(submissionSource);
458+
459+
if (!stats2.isFile()) {
460+
throw new Error('not a file');
461+
}
462+
} catch (statError) {
463+
throw new Error(`error with ${submissionSource}: ${statError}`);
464+
}
465+
patchData.version = { source: client.fileFromSync(submissionSource) };
466+
}
459467

460468
// We specifically need to know if `id` has not been passed as a parameter because
461469
// it's the indication that a new add-on should be created, rather than a new version.

tests/unit/test-util/test.submit-addon.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ describe('util.submit-addon', () => {
5757
beforeEach(() => {
5858
statStub = sinon
5959
.stub(fsPromises, 'stat')
60+
.onFirstCall()
6061
.resolves({ isFile: () => true });
62+
statStub.callThrough();
6163
getPreviousUuidOrUploadXpiStub = sinon
6264
.stub(Client.prototype, 'getPreviousUuidOrUploadXpi')
6365
.resolves(uploadUuid);
@@ -197,6 +199,7 @@ describe('util.submit-addon', () => {
197199

198200
it('includes source data to be patched if submissionSource defined for new addon', async () => {
199201
const submissionSource = 'path/to/source/zip';
202+
statStub.onSecondCall().resolves({ isFile: () => true });
200203
await signAddon({
201204
...signAddonDefaults,
202205
submissionSource,
@@ -214,6 +217,7 @@ describe('util.submit-addon', () => {
214217

215218
it('includes source data to be patched if submissionSource defined for new version', async () => {
216219
const submissionSource = 'path/to/source/zip';
220+
statStub.onSecondCall().resolves({ isFile: () => true });
217221
const id = '@thisID';
218222
await signAddon({
219223
...signAddonDefaults,
@@ -230,6 +234,34 @@ describe('util.submit-addon', () => {
230234
{ version: { source: fakeFileFromSync } },
231235
);
232236
});
237+
238+
it('throws error if submissionSource is not found', async () => {
239+
const submissionSource = 'path/to/source/zip';
240+
const signAddonPromise = signAddon({
241+
...signAddonDefaults,
242+
submissionSource,
243+
});
244+
await assert.isRejected(
245+
signAddonPromise,
246+
`error with ${submissionSource}: ` +
247+
'Error: ENOENT: no such file or directory',
248+
);
249+
});
250+
251+
it('throws error if submissionSource is a directory', async () => {
252+
await withTempDir(async (tmpDir) => {
253+
const submissionSource = path.join(tmpDir.path(), 'someDirectory');
254+
await fsPromises.mkdir(submissionSource);
255+
const signAddonPromise = signAddon({
256+
...signAddonDefaults,
257+
submissionSource,
258+
});
259+
await assert.isRejected(
260+
signAddonPromise,
261+
`error with ${submissionSource}: ` + 'Error: not a file',
262+
);
263+
});
264+
});
233265
});
234266

235267
describe('Client', () => {

0 commit comments

Comments
 (0)