Skip to content

Commit

Permalink
Percent-encode URL path when reporting spam
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn-Signal committed Jan 19, 2023
1 parent 6ddb12c commit 7f0ed25
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
20 changes: 19 additions & 1 deletion ts/test-both/util/url_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import { assert } from 'chai';
import { size } from '../../util/iterables';

import { maybeParseUrl, setUrlSearchParams } from '../../util/url';
import {
maybeParseUrl,
setUrlSearchParams,
urlPathFromComponents,
} from '../../util/url';

describe('URL utilities', () => {
describe('maybeParseUrl', () => {
Expand Down Expand Up @@ -84,4 +88,18 @@ describe('URL utilities', () => {
assert.strictEqual(newUrl.search, '?foo=bar');
});
});

describe('urlPathFromComponents', () => {
it('returns / if no components are provided', () => {
assert.strictEqual(urlPathFromComponents([]), '/');
});

it('joins components, percent-encoding them and removing empty components', () => {
const components = ['foo', '', '~', 'bar / baz qúx'];
assert.strictEqual(
urlPathFromComponents(components),
'/foo/~/bar%20%2F%20baz%20q%C3%BAx'
);
});
});
});
4 changes: 2 additions & 2 deletions ts/textsecure/WebAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import type {
} from './Types.d';
import { handleStatusCode, translateError } from './Utils';
import * as log from '../logging/log';
import { maybeParseUrl } from '../util/url';
import { maybeParseUrl, urlPathFromComponents } from '../util/url';

// Note: this will break some code that expects to be able to use err.response when a
// web request fails, because it will force it to text. But it is very useful for
Expand Down Expand Up @@ -1780,7 +1780,7 @@ export function initialize({
await _ajax({
call: 'reportMessage',
httpType: 'POST',
urlParameters: `/${senderUuid}/${serverGuid}`,
urlParameters: urlPathFromComponents([senderUuid, serverGuid]),
responseType: 'bytes',
});
}
Expand Down
6 changes: 6 additions & 0 deletions ts/util/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ function cloneUrl(url: Readonly<URL>): URL {
function stringifySearchParamValue(value: unknown): string {
return value == null ? '' : String(value);
}

export function urlPathFromComponents(
components: ReadonlyArray<string>
): string {
return `/${components.filter(Boolean).map(encodeURIComponent).join('/')}`;
}

0 comments on commit 7f0ed25

Please sign in to comment.