Skip to content

Commit 71e19d2

Browse files
authored
add trailing slash to amo-base-url & enforce within submit-addon Client (#2621)
fixes #2579
1 parent 00250d0 commit 71e19d2

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

src/program.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type ExecuteOptions = {
4848
globalEnv?: string | void,
4949
};
5050

51+
export const AMO_BASE_URL = 'https://addons.mozilla.org/api/v5/';
52+
5153
/*
5254
* The command line program.
5355
*/
@@ -568,7 +570,7 @@ Example: $0 --help run.
568570
'amo-base-url': {
569571
describe:
570572
'Signing API URL prefix - only used with `use-submission-api`',
571-
default: 'https://addons.mozilla.org/api/v5',
573+
default: AMO_BASE_URL,
572574
demandOption: true,
573575
type: 'string',
574576
},

src/util/submit-addon.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ export default class Client {
9090
userAgentString,
9191
}: ClientConstructorParams) {
9292
this.apiAuth = apiAuth;
93+
if (!baseUrl.pathname.endsWith('/')) {
94+
baseUrl = new URL(baseUrl.href);
95+
baseUrl.pathname += '/';
96+
}
9397
this.apiUrl = new URL('addons/', baseUrl);
9498
this.validationCheckInterval = validationCheckInterval;
9599
this.validationCheckTimeout = validationCheckTimeout;

tests/unit/test-cmd/test.sign.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { assert } from 'chai';
99
import * as sinon from 'sinon';
1010

1111
import { UsageError, WebExtError } from '../../../src/errors.js';
12+
import { AMO_BASE_URL } from '../../../src/program.js';
1213
import { getManifestId } from '../../../src/util/manifest.js';
1314
import { saveIdToFile } from '../../../src/util/submit-addon.js';
1415
import { withTempDir } from '../../../src/util/temp-dir.js';
@@ -23,7 +24,7 @@ import type { ExtensionManifestApplications } from '../../../src/util/manifest';
2324
describe('sign', () => {
2425
function getStubs() {
2526
const signingConfig = {
26-
amoBaseUrl: 'http://not-the-real-amo.com/api/v5',
27+
amoBaseUrl: AMO_BASE_URL,
2728
apiKey: 'AMO JWT issuer',
2829
apiSecret: 'AMO JWT secret',
2930
apiUrlPrefix: 'http://not-the-real-amo.com/api/v4',

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { afterEach, before, beforeEach, describe, it } from 'mocha';
88
import * as sinon from 'sinon';
99
import { File, FormData, Response } from 'node-fetch';
1010

11+
import { AMO_BASE_URL } from '../../../src/program.js';
1112
import Client, {
1213
JwtApiAuth,
1314
saveIdToFile,
@@ -69,7 +70,7 @@ describe('util.submit-addon', () => {
6970
const signAddonDefaults = {
7071
apiKey: 'some-key',
7172
apiSecret: 'ffff',
72-
amoBaseUrl: 'https://some.url/api/v5',
73+
amoBaseUrl: AMO_BASE_URL,
7374
timeout: 1,
7475
downloadDir: '/some-dir/',
7576
xpiPath: '/some.xpi',
@@ -81,7 +82,7 @@ describe('util.submit-addon', () => {
8182
it('creates Client with parameters', async () => {
8283
const apiKey = 'fooKey';
8384
const apiSecret = '4321';
84-
const amoBaseUrl = 'https://foo.host/api/v5';
85+
const amoBaseUrl = AMO_BASE_URL;
8586
const baseUrl = new URL(amoBaseUrl);
8687
const downloadDir = '/foo';
8788
const clientSpy = sinon.spy(Client);
@@ -186,7 +187,7 @@ describe('util.submit-addon', () => {
186187
});
187188

188189
describe('Client', () => {
189-
const baseUrl = new URL('http://not-a-real-amo-api.com/api/v5');
190+
const baseUrl = new URL(AMO_BASE_URL);
190191

191192
const apiAuth = new JwtApiAuth({
192193
apiKey: 'fake-api-key',
@@ -241,6 +242,22 @@ describe('util.submit-addon', () => {
241242
getAuthHeaderSpy.resetHistory();
242243
});
243244

245+
it('adds a missing trailing slash to baseUrl before setting apiUrl', () => {
246+
const noSlashBaseUrl = new URL('http://url.without/trailing/slash');
247+
const client = new Client({ ...clientDefaults, baseUrl: noSlashBaseUrl });
248+
assert.equal(
249+
client.apiUrl.href,
250+
new URL(`${noSlashBaseUrl.href}/addons/`).href
251+
);
252+
});
253+
254+
it('drops extra characters on baseUrl before setting apiUrl', () => {
255+
const cleanUrl = 'http://url.with/extra';
256+
const extraBaseUrl = new URL(`${cleanUrl}?#`);
257+
const client = new Client({ ...clientDefaults, baseUrl: extraBaseUrl });
258+
assert.equal(client.apiUrl.href, new URL(`${cleanUrl}/addons/`).href);
259+
});
260+
244261
describe('doUploadSubmit', () => {
245262
it('submits the xpi', async () => {
246263
const client = new Client(clientDefaults);

0 commit comments

Comments
 (0)