Skip to content

Commit b519f68

Browse files
committed
pryv: host names may hold an underscore; username refusal is a PryvError; numeric token kept
1 parent 92de972 commit b519f68

4 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ reads it (security).
2727
the authority only; an endpoint with an `@` after the authority, more than one `@`, a backslash, white space or a
2828
control character throws `Invalid apiEndpoint: ambiguous URL`. The endpoint is now read against a strict grammar:
2929
`http` or `https` only, a token of URL user-part characters without `:` or `%` (a `user:password@` part is refused),
30-
a host name, IPv4 or bracketed IPv6 address with an optional port, a path without query or fragment; and
30+
an ASCII host name (a Unicode name is given in its `xn--` form), IPv4 or bracketed IPv6 address with an optional
31+
port, a path without query or fragment; and
3132
`new URL()` must parse it to the same host. A value that is not a string (a `URL` object), or a string with
3233
surrounding white space or a trailing newline, now throws instead of being accepted. `utils.buildAPIEndpoint` refuses
3334
a token outside that grammar and checks that what it builds reads back the same; `Service.buildAPIEndpoint` (and so

‎components/pryv/src/Service.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class Service {
172172
// or `:`) would move the endpoint, and the password or token sent to it,
173173
// to another host or path.
174174
if (typeof username !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9-]*$/.test(username)) {
175-
throw new Error('Invalid username: it cannot be placed in an apiEndpoint');
175+
throw new PryvError('Invalid username: it cannot be placed in an apiEndpoint');
176176
}
177177
const endpoint = serviceInfo.api.replace('{username}', username);
178178
return utils.buildAPIEndpoint({ endpoint, token });

‎components/pryv/src/utils.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const regexSchemeAuthorityRest = /^(https?):\/\/([^/?#]*)(.*)$/is;
99
// A token: letters, digits and the URL user-part characters that need no
1010
// encoding and end nothing (no `:`, `@`, `/`, `?`, `#`, `%`, backslash).
1111
const regexToken = /^[A-Za-z0-9._~!$&'()*+,;=-]+$/;
12-
// A host: a name or an IPv4 address, or an IPv6 address in brackets, with an
13-
// optional port.
14-
const regexHost = /^(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?\.?)(?::[0-9]{1,5})?$/;
12+
// A host: a name (ASCII; `_` as container names use) or an IPv4 address, or
13+
// an IPv6 address in brackets, with an optional port.
14+
const regexHost = /^(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9_](?:[A-Za-z0-9_.-]*[A-Za-z0-9_])?\.?)(?::[0-9]{1,5})?$/;
1515
// A path: `/` and the characters a URL path holds without ending or
1616
// changing it (no `@`, `?`, `#`, backslash, white space).
1717
const regexPath = /^(?:\/[A-Za-z0-9._~!$&'()*+,;=:%/-]*)?$/;
@@ -151,7 +151,7 @@ const utils = module.exports = {
151151
const built = utils._buildAPIEndpoint(tokenAndAPI);
152152
// What is built reads back as the same endpoint and token.
153153
const back = utils.extractTokenAndAPIEndpoint(built);
154-
if ((back.token ?? null) !== (tokenAndAPI.token || null)) {
154+
if ((back.token ?? null) !== (tokenAndAPI.token ? String(tokenAndAPI.token) : null)) {
155155
throw new Error('Invalid apiEndpoint: ambiguous URL');
156156
}
157157
return built;

‎components/pryv/test/utils.endpoint.test.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe('[UTLP] apiEndpoint parsing', function () {
8282
expect(() => extract(e), e).to.throw(/invalid URL format/);
8383
}
8484
expect(extract('https://tok@[::1]:3000/')).to.eql({ endpoint: 'https://[::1]:3000/', token: 'tok' });
85+
expect(extract('http://tok@pryv_core:3000/u/')).to.eql({ endpoint: 'http://pryv_core:3000/u/', token: 'tok' });
8586
expect(extract('HTTPS://tok@A.test/a/').token).to.equal('tok');
8687
});
8788

0 commit comments

Comments
 (0)