Skip to content

Commit

Permalink
add trailing slash to amo-base-url & enforce within submit-addon Clie…
Browse files Browse the repository at this point in the history
…nt (#2621)

fixes #2579
  • Loading branch information
eviljeff committed Jan 24, 2023
1 parent 00250d0 commit 71e19d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type ExecuteOptions = {
globalEnv?: string | void,
};

export const AMO_BASE_URL = 'https://addons.mozilla.org/api/v5/';

/*
* The command line program.
*/
Expand Down Expand Up @@ -568,7 +570,7 @@ Example: $0 --help run.
'amo-base-url': {
describe:
'Signing API URL prefix - only used with `use-submission-api`',
default: 'https://addons.mozilla.org/api/v5',
default: AMO_BASE_URL,
demandOption: true,
type: 'string',
},
Expand Down
4 changes: 4 additions & 0 deletions src/util/submit-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export default class Client {
userAgentString,
}: ClientConstructorParams) {
this.apiAuth = apiAuth;
if (!baseUrl.pathname.endsWith('/')) {
baseUrl = new URL(baseUrl.href);
baseUrl.pathname += '/';
}
this.apiUrl = new URL('addons/', baseUrl);
this.validationCheckInterval = validationCheckInterval;
this.validationCheckTimeout = validationCheckTimeout;
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test-cmd/test.sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { assert } from 'chai';
import * as sinon from 'sinon';

import { UsageError, WebExtError } from '../../../src/errors.js';
import { AMO_BASE_URL } from '../../../src/program.js';
import { getManifestId } from '../../../src/util/manifest.js';
import { saveIdToFile } from '../../../src/util/submit-addon.js';
import { withTempDir } from '../../../src/util/temp-dir.js';
Expand All @@ -23,7 +24,7 @@ import type { ExtensionManifestApplications } from '../../../src/util/manifest';
describe('sign', () => {
function getStubs() {
const signingConfig = {
amoBaseUrl: 'http://not-the-real-amo.com/api/v5',
amoBaseUrl: AMO_BASE_URL,
apiKey: 'AMO JWT issuer',
apiSecret: 'AMO JWT secret',
apiUrlPrefix: 'http://not-the-real-amo.com/api/v4',
Expand Down
23 changes: 20 additions & 3 deletions tests/unit/test-util/test.submit-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { afterEach, before, beforeEach, describe, it } from 'mocha';
import * as sinon from 'sinon';
import { File, FormData, Response } from 'node-fetch';

import { AMO_BASE_URL } from '../../../src/program.js';
import Client, {
JwtApiAuth,
saveIdToFile,
Expand Down Expand Up @@ -69,7 +70,7 @@ describe('util.submit-addon', () => {
const signAddonDefaults = {
apiKey: 'some-key',
apiSecret: 'ffff',
amoBaseUrl: 'https://some.url/api/v5',
amoBaseUrl: AMO_BASE_URL,
timeout: 1,
downloadDir: '/some-dir/',
xpiPath: '/some.xpi',
Expand All @@ -81,7 +82,7 @@ describe('util.submit-addon', () => {
it('creates Client with parameters', async () => {
const apiKey = 'fooKey';
const apiSecret = '4321';
const amoBaseUrl = 'https://foo.host/api/v5';
const amoBaseUrl = AMO_BASE_URL;
const baseUrl = new URL(amoBaseUrl);
const downloadDir = '/foo';
const clientSpy = sinon.spy(Client);
Expand Down Expand Up @@ -186,7 +187,7 @@ describe('util.submit-addon', () => {
});

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

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

it('adds a missing trailing slash to baseUrl before setting apiUrl', () => {
const noSlashBaseUrl = new URL('http://url.without/trailing/slash');
const client = new Client({ ...clientDefaults, baseUrl: noSlashBaseUrl });
assert.equal(
client.apiUrl.href,
new URL(`${noSlashBaseUrl.href}/addons/`).href
);
});

it('drops extra characters on baseUrl before setting apiUrl', () => {
const cleanUrl = 'http://url.with/extra';
const extraBaseUrl = new URL(`${cleanUrl}?#`);
const client = new Client({ ...clientDefaults, baseUrl: extraBaseUrl });
assert.equal(client.apiUrl.href, new URL(`${cleanUrl}/addons/`).href);
});

describe('doUploadSubmit', () => {
it('submits the xpi', async () => {
const client = new Client(clientDefaults);
Expand Down

0 comments on commit 71e19d2

Please sign in to comment.