@routr/common 2.31.3 → 2.31.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
1
  import { SipURI } from "./types";
2
2
  /**
3
3
  * The caller identity Routr rewrites toward a Trunk travels in its own
4
- * Record-Route as parameters, so the dialog's later requests can be mapped back
5
- * without state. Shared by the processor, which writes and reads them, and the
6
- * trace sanitizer, which must rewrite inside them.
4
+ * Record-Route, as parameters or in the URI user part, so the dialog's later
5
+ * requests can be mapped back without state. Shared by the processor, which
6
+ * writes and reads them, and the trace sanitizer, which must rewrite inside them.
7
7
  */
8
8
  /**
9
9
  * The Record-Route parameter names: [r]ewritten [f]rom or [c]allee (To),
@@ -22,6 +22,7 @@ export declare const IDENTITY_PARAMETER_NAMES: string[];
22
22
  export interface Identity {
23
23
  user?: string;
24
24
  host: string;
25
+ port?: number;
25
26
  userParam?: string;
26
27
  }
27
28
  /**
@@ -33,9 +34,20 @@ export interface Identity {
33
34
  */
34
35
  export declare const encodeIdentity: (uri: SipURI) => string;
35
36
  /**
36
- * Decodes an identity parameter value.
37
+ * Decodes an identity parameter value, user@host[:port][;user=...].
37
38
  *
38
39
  * @param {string} value - The base64url value
39
40
  * @return {Identity | undefined} The identity, or undefined when it is not one
40
41
  */
41
42
  export declare const decodeIdentity: (value?: string) => Identity | undefined;
43
+ /**
44
+ * Reads the identity mapping from a Route URI user part of the form
45
+ * rfo.<b64>.rfn.<b64>.rco.<b64>.rcn.<b64>: each pair optional, in that order.
46
+ * The URI is what a user agent keeps of a Record-Route entry (RFC 3261 section
47
+ * 12.1.2), and "." is not in the base64url alphabet.
48
+ *
49
+ * @param {string} user - The URI user part
50
+ * @return {Record<string, string> | undefined} The base64url values by
51
+ * parameter name, or undefined when the user part is not of this form
52
+ */
53
+ export declare const decodeIdentityUser: (user?: string) => Record<string, string> | undefined;
package/dist/identity.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decodeIdentity = exports.encodeIdentity = exports.IDENTITY_PARAMETER_NAMES = exports.IDENTITY_PARAMETERS = void 0;
3
+ exports.decodeIdentityUser = exports.decodeIdentity = exports.encodeIdentity = exports.IDENTITY_PARAMETER_NAMES = exports.IDENTITY_PARAMETERS = void 0;
4
4
  /**
5
5
  * The caller identity Routr rewrites toward a Trunk travels in its own
6
- * Record-Route as parameters, so the dialog's later requests can be mapped back
7
- * without state. Shared by the processor, which writes and reads them, and the
8
- * trace sanitizer, which must rewrite inside them.
6
+ * Record-Route, as parameters or in the URI user part, so the dialog's later
7
+ * requests can be mapped back without state. Shared by the processor, which
8
+ * writes and reads them, and the trace sanitizer, which must rewrite inside them.
9
9
  */
10
10
  /**
11
11
  * The Record-Route parameter names: [r]ewritten [f]rom or [c]allee (To),
@@ -33,12 +33,13 @@ const encodeIdentity = (uri) => {
33
33
  };
34
34
  exports.encodeIdentity = encodeIdentity;
35
35
  /**
36
- * Decodes an identity parameter value.
36
+ * Decodes an identity parameter value, user@host[:port][;user=...].
37
37
  *
38
38
  * @param {string} value - The base64url value
39
39
  * @return {Identity | undefined} The identity, or undefined when it is not one
40
40
  */
41
41
  const decodeIdentity = (value) => {
42
+ var _a;
42
43
  if (!value)
43
44
  return undefined;
44
45
  const decoded = Buffer.from(value, "base64url").toString("utf8");
@@ -47,10 +48,32 @@ const decodeIdentity = (value) => {
47
48
  if (at < 0)
48
49
  return undefined;
49
50
  const user = address.slice(0, at);
50
- return {
51
- user: user || undefined,
52
- host: address.slice(at + 1),
53
- userParam: userParam || undefined
54
- };
51
+ const hostPort = address.slice(at + 1);
52
+ // An IPv6 host keeps its brackets, so its port follows the last "]:"
53
+ const [, host = hostPort, port] = (_a = hostPort.match(/^(\[[^\]]*\]|[^:]*)(?::(\d+))?$/)) !== null && _a !== void 0 ? _a : [];
54
+ return Object.assign(Object.assign({ user: user || undefined, host }, (port ? { port: Number(port) } : {})), { userParam: userParam || undefined });
55
55
  };
56
56
  exports.decodeIdentity = decodeIdentity;
57
+ /**
58
+ * Reads the identity mapping from a Route URI user part of the form
59
+ * rfo.<b64>.rfn.<b64>.rco.<b64>.rcn.<b64>: each pair optional, in that order.
60
+ * The URI is what a user agent keeps of a Record-Route entry (RFC 3261 section
61
+ * 12.1.2), and "." is not in the base64url alphabet.
62
+ *
63
+ * @param {string} user - The URI user part
64
+ * @return {Record<string, string> | undefined} The base64url values by
65
+ * parameter name, or undefined when the user part is not of this form
66
+ */
67
+ const decodeIdentityUser = (user) => {
68
+ const parts = (user !== null && user !== void 0 ? user : "").split(".");
69
+ if (parts.length < 2 || parts.length % 2)
70
+ return undefined;
71
+ const pairs = parts
72
+ .filter((_, i) => i % 2 === 0)
73
+ .map((name, i) => [name, parts[i * 2 + 1]]);
74
+ const order = pairs.map(([name]) => exports.IDENTITY_PARAMETER_NAMES.indexOf(name));
75
+ const valid = order.every((o, i) => o >= 0 && (i === 0 || o > order[i - 1])) &&
76
+ pairs.every(([, value]) => /^[A-Za-z0-9_-]+$/.test(value));
77
+ return valid ? Object.fromEntries(pairs) : undefined;
78
+ };
79
+ exports.decodeIdentityUser = decodeIdentityUser;
@@ -99,7 +99,29 @@ function applyRewrites(record, rewrites) {
99
99
  // (";rfo=...;rfn=..." in a hop's diff); base64url has no quote, so they are
100
100
  // safe to match in the JSON text
101
101
  const encodedInText = new RegExp(`\\b(${ENCODED_IDENTITY_PARAMETERS.join("|")})=([A-Za-z0-9_-]+)`, "g");
102
- return JSON.parse(rewrite(text.replace(encodedInText, (_m, name, value) => `${name}=${reencode(value, rewrite)}`)));
102
+ // And in a Route or Record-Route URI user part kept as text
103
+ // ("<sip:rfo....rcn...@host>")
104
+ const names = ENCODED_IDENTITY_PARAMETERS.join("|");
105
+ const encodedUserInText = new RegExp(`\\b((?:${names})\\.[A-Za-z0-9_-]+(?:\\.(?:${names})\\.[A-Za-z0-9_-]+)*)@`, "g");
106
+ return JSON.parse(rewrite(text
107
+ .replace(encodedInText, (_m, name, value) => `${name}=${reencode(value, rewrite)}`)
108
+ .replace(encodedUserInText, (_m, user) => `${reencodeUser(user, rewrite)}@`)));
109
+ }
110
+ /**
111
+ * Rewrites the identities inside a URI user part that holds Routr's identity
112
+ * mapping (rfo.<b64>.rfn.<b64>...); any other user part is returned as is.
113
+ *
114
+ * @param {string} user - The URI user part
115
+ * @param {Function} rewrite - The literal rewrites
116
+ * @return {string} The rewritten user part
117
+ */
118
+ function reencodeUser(user, rewrite) {
119
+ const values = (0, identity_1.decodeIdentityUser)(user);
120
+ if (!values)
121
+ return user;
122
+ return Object.entries(values)
123
+ .map(([name, value]) => `${name}.${reencode(value, rewrite)}`)
124
+ .join(".");
103
125
  }
104
126
  /**
105
127
  * Decodes a base64url identity parameter, rewrites it and encodes it again.
@@ -123,7 +145,8 @@ function reencode(value, rewrite) {
123
145
  // survive sanitizing; they are decoded, rewritten and encoded again.
124
146
  const ENCODED_IDENTITY_PARAMETERS = identity_1.IDENTITY_PARAMETER_NAMES;
125
147
  /**
126
- * Rewrites the identity parameters wherever they are a field of the record.
148
+ * Rewrites the identity parameters wherever they are a field of the record, or
149
+ * a URI user part.
127
150
  *
128
151
  * @param {T} value - Any part of the record
129
152
  * @param {Function} rewrite - The literal rewrites
@@ -139,6 +162,10 @@ function rewriteEncodedIdentities(value, rewrite) {
139
162
  if (ENCODED_IDENTITY_PARAMETERS.includes(k) && typeof v === "string") {
140
163
  return [k, reencode(v, rewrite)];
141
164
  }
165
+ // A Route or Record-Route URI whose user part holds the mapping
166
+ if (k === "user" && typeof v === "string") {
167
+ return [k, reencodeUser(v, rewrite)];
168
+ }
142
169
  return [k, rewriteEncodedIdentities(v, rewrite)];
143
170
  }));
144
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/common",
3
- "version": "2.31.3",
3
+ "version": "2.31.4",
4
4
  "description": "Common package",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -61,5 +61,5 @@
61
61
  "devDependencies": {
62
62
  "@types/js-yaml": "^4.0.5"
63
63
  },
64
- "gitHead": "7178a8d1ad402c3a00b31b55c8b63ecd2527c975"
64
+ "gitHead": "11c1ca25e5892aeea6f5b836b35cf9f90f020279"
65
65
  }