1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Support optional MSC3860 redirects (#4007)

* Support optional MSC3860 redirects

See `allow_redirect` across the media endpoints: https://spec.matrix.org/v1.9/client-server-api/#client-behaviour-7

* Update the tests

* Appease the linter

* Add test to appease SonarCloud

* Only add `allow_redirect` if the parameter is specified rather than defaulting to `false`

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Travis Ralston
2024-01-24 03:30:51 -07:00
committed by GitHub
parent c4d32a3292
commit ab217bdc35
4 changed files with 45 additions and 1 deletions

View File

@ -37,6 +37,21 @@ describe("ContentRepo", function () {
); );
}); });
it("should allow redirects when requested on download URLs", function () {
const mxcUri = "mxc://server.name/resourceid";
expect(getHttpUriForMxc(baseUrl, mxcUri, undefined, undefined, undefined, false, true)).toEqual(
baseUrl + "/_matrix/media/v3/download/server.name/resourceid?allow_redirect=true",
);
});
it("should allow redirects when requested on thumbnail URLs", function () {
const mxcUri = "mxc://server.name/resourceid";
expect(getHttpUriForMxc(baseUrl, mxcUri, 32, 32, "scale", false, true)).toEqual(
baseUrl +
"/_matrix/media/v3/thumbnail/server.name/resourceid?width=32&height=32&method=scale&allow_redirect=true",
);
});
it("should return the empty string for null input", function () { it("should return the empty string for null input", function () {
expect(getHttpUriForMxc(null as any, "")).toEqual(""); expect(getHttpUriForMxc(null as any, "")).toEqual("");
}); });

View File

@ -55,6 +55,7 @@ import {
RuleId, RuleId,
IPushRule, IPushRule,
ConditionKind, ConditionKind,
getHttpUriForMxc,
} from "../../src"; } from "../../src";
import { supportsMatrixCall } from "../../src/webrtc/call"; import { supportsMatrixCall } from "../../src/webrtc/call";
import { makeBeaconEvent } from "../test-utils/beacon"; import { makeBeaconEvent } from "../test-utils/beacon";
@ -369,6 +370,21 @@ describe("MatrixClient", function () {
client.stopClient(); client.stopClient();
}); });
describe("mxcUrlToHttp", () => {
it("should call getHttpUriForMxc", () => {
const mxc = "mxc://server/example";
expect(client.mxcUrlToHttp(mxc)).toBe(getHttpUriForMxc(client.baseUrl, mxc));
expect(client.mxcUrlToHttp(mxc, 32)).toBe(getHttpUriForMxc(client.baseUrl, mxc, 32));
expect(client.mxcUrlToHttp(mxc, 32, 46)).toBe(getHttpUriForMxc(client.baseUrl, mxc, 32, 46));
expect(client.mxcUrlToHttp(mxc, 32, 46, "scale")).toBe(
getHttpUriForMxc(client.baseUrl, mxc, 32, 46, "scale"),
);
expect(client.mxcUrlToHttp(mxc, 32, 46, "scale", false, true)).toBe(
getHttpUriForMxc(client.baseUrl, mxc, 32, 46, "scale", false, true),
);
});
});
describe("timestampToEvent", () => { describe("timestampToEvent", () => {
const roomId = "!room:server.org"; const roomId = "!room:server.org";
const eventId = "$eventId:example.org"; const eventId = "$eventId:example.org";

View File

@ -5805,6 +5805,9 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @param allowDirectLinks - If true, return any non-mxc URLs * @param allowDirectLinks - If true, return any non-mxc URLs
* directly. Fetching such URLs will leak information about the user to * directly. Fetching such URLs will leak information about the user to
* anyone they share a room with. If false, will return null for such URLs. * anyone they share a room with. If false, will return null for such URLs.
* @param allowRedirects - If true, the caller supports the URL being 307 or
* 308 redirected to another resource upon request. If false, redirects
* are not expected.
* @returns the avatar URL or null. * @returns the avatar URL or null.
*/ */
public mxcUrlToHttp( public mxcUrlToHttp(
@ -5813,8 +5816,9 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
height?: number, height?: number,
resizeMethod?: string, resizeMethod?: string,
allowDirectLinks?: boolean, allowDirectLinks?: boolean,
allowRedirects?: boolean,
): string | null { ): string | null {
return getHttpUriForMxc(this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks); return getHttpUriForMxc(this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks, allowRedirects);
} }
/** /**

View File

@ -28,6 +28,9 @@ import { encodeParams } from "./utils";
* directly. Fetching such URLs will leak information about the user to * directly. Fetching such URLs will leak information about the user to
* anyone they share a room with. If false, will return the emptry string * anyone they share a room with. If false, will return the emptry string
* for such URLs. * for such URLs.
* @param allowRedirects - If true, the caller supports the URL being 307 or
* 308 redirected to another resource upon request. If false, redirects
* are not expected.
* @returns The complete URL to the content. * @returns The complete URL to the content.
*/ */
export function getHttpUriForMxc( export function getHttpUriForMxc(
@ -37,6 +40,7 @@ export function getHttpUriForMxc(
height?: number, height?: number,
resizeMethod?: string, resizeMethod?: string,
allowDirectLinks = false, allowDirectLinks = false,
allowRedirects?: boolean,
): string { ): string {
if (typeof mxc !== "string" || !mxc) { if (typeof mxc !== "string" || !mxc) {
return ""; return "";
@ -67,6 +71,11 @@ export function getHttpUriForMxc(
prefix = "/_matrix/media/v3/thumbnail/"; prefix = "/_matrix/media/v3/thumbnail/";
} }
if (typeof allowRedirects === "boolean") {
// We add this after, so we don't convert everything to a thumbnail request.
params["allow_redirect"] = JSON.stringify(allowRedirects);
}
const fragmentOffset = serverAndMediaId.indexOf("#"); const fragmentOffset = serverAndMediaId.indexOf("#");
let fragment = ""; let fragment = "";
if (fragmentOffset >= 0) { if (fragmentOffset >= 0) {