From 4d06660cb597e980db17bd103fa50c89743ed6df Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Thu, 31 Mar 2022 16:05:04 +0100 Subject: [PATCH 1/4] Concatentate URL properly --- src/provisioning/api.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/provisioning/api.ts b/src/provisioning/api.ts index 63e45709..c9a29cbf 100644 --- a/src/provisioning/api.ts +++ b/src/provisioning/api.ts @@ -349,7 +349,8 @@ export class ProvisioningApi { // Now do the token exchange try { - const response = await axios.get<{sub: string}>(`${url}/_matrix/federation/v1/openid/userinfo`, { + const requestUrl = new URL("/_matrix/federation/v1/openid/userinfo", url); + const response = await axios.get<{sub: string}>(requestUrl.toString(), { params: { access_token: openIdToken, }, From d9db33cabe5a22875d7ab4526cc06794fa3a2a92 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Tue, 5 Apr 2022 09:37:27 +0100 Subject: [PATCH 2/4] Tweak supported methods for requests --- src/provisioning/api.ts | 3 ++- src/provisioning/request.ts | 51 ++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/provisioning/api.ts b/src/provisioning/api.ts index c9a29cbf..11855b2d 100644 --- a/src/provisioning/api.ts +++ b/src/provisioning/api.ts @@ -11,6 +11,7 @@ import IPCIDR from "ip-cidr"; import { isIP } from "net"; import { promises as dns } from "dns"; import ratelimiter, { RateLimitInfo, Options as RatelimitOptions, AugmentedRequest } from "express-rate-limit"; +import { Methods } from "./request"; // Borrowed from // https://github.com/matrix-org/synapse/blob/91221b696156e9f1f9deecd425ae58af03ebb5d3/docs/sample_config.yaml#L215 @@ -196,7 +197,7 @@ export class ProvisioningApi { } public addRoute( - method: "get"|"post"|"delete"|"put", + method: Methods, path: string, handler: (req: ProvisioningRequest, res: Response, next?: NextFunction) => void|Promise, fnName?: string): void { diff --git a/src/provisioning/request.ts b/src/provisioning/request.ts index a4b92666..778bcde5 100644 --- a/src/provisioning/request.ts +++ b/src/provisioning/request.ts @@ -1,16 +1,51 @@ import Logging, { LogWrapper } from "../components/logging"; import crypto from "crypto"; import { ThinRequest } from ".."; +import { Request } from "express"; +import { ParsedQs } from "qs"; + +// Methods supported by a express.Router +export type Methods = 'all' | +'get' | +'post' | +'put' | +'delete' | +'patch' | +'options' | +'head' | +'checkout' | +'connect' | +'copy' | +'lock' | +'merge' | +'mkactivity' | +'mkcol' | +'move' | +'m-search' | +'notify' | +'propfind' | +'proppatch' | +'purge' | +'report' | +'search' | +'subscribe' | +'trace' | +'unlock' | +'unsubscribe'; export class ProvisioningRequest< - Body = Record, Params = Record - > implements ThinRequest { + // These types are taken from express.Request + Params = {[key: string]: string}, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ResBody = any, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ReqBody = any, + ReqQuery = ParsedQs> implements ThinRequest { public readonly log: LogWrapper; public readonly id: string; - constructor( - private expressReq: {body: Body, params: Params, path?: string}, + public readonly expressReq: Request, public readonly userId: string|null, public readonly requestSource: "widget"|"provisioner", public readonly widgetToken?: string, @@ -21,18 +56,22 @@ export class ProvisioningRequest< this.log = Logging.get( `ProvisionRequest ${[this.id, fnName].filter(n => !!n).join(" ")}` ); - this.log.info(`New request from ${userId} via ${requestSource}`); + this.log.debug(`Request ${userId} (${requestSource}) ${this.fnName}`); } public getId(): string { return this.id; } - get body(): Body { + get body(): ReqBody { return this.expressReq.body; } get params(): Params { return this.expressReq.params; } + + get query(): ReqQuery { + return this.expressReq.query; + } } From 35b2e29c55f08696e412d5c0907a40263c828db5 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Tue, 5 Apr 2022 09:45:53 +0100 Subject: [PATCH 3/4] add note --- changelog.d/397.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/397.bugfix diff --git a/changelog.d/397.bugfix b/changelog.d/397.bugfix new file mode 100644 index 00000000..55eb011b --- /dev/null +++ b/changelog.d/397.bugfix @@ -0,0 +1 @@ +Fix an issue where the provisioner API's `/v1/exchange_openid` route would sometimes fail. \ No newline at end of file From 71c1524fdf978eaba7c5288df531dd8332de7be5 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Tue, 5 Apr 2022 16:52:17 +0100 Subject: [PATCH 4/4] Fix getWellKnown failing to handle non-JSON payloads --- src/utils/matrix-host-resolver.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils/matrix-host-resolver.ts b/src/utils/matrix-host-resolver.ts index 24027533..cb7333a7 100644 --- a/src/utils/matrix-host-resolver.ts +++ b/src/utils/matrix-host-resolver.ts @@ -105,7 +105,17 @@ export class MatrixHostResolver { if (wellKnown.status !== 200) { throw Error('Well known request returned non-200'); } - const mServer = wellKnown.data["m.server"]; + let data: MatrixServerWellKnown; + if (typeof wellKnown.data === "object") { + data = wellKnown.data; + } + else if (typeof wellKnown.data === "string") { + data = JSON.parse(wellKnown.data); + } + else { + throw Error('Invalid datatype for well-known response'); + } + const mServer = data["m.server"]; if (typeof mServer !== "string") { throw Error("Missing 'm.server' in well-known response"); }