Skip to content

Commit 9769642

Browse files
ooiuuiisteipete
andauthored
fix: reject unsafe Mattermost API path segments (#98390)
* Harden Mattermost API paths * fix(mattermost): guard direct media file paths * refactor(mattermost): streamline API path validation --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent a83ed13 commit 9769642

4 files changed

Lines changed: 93 additions & 8 deletions

File tree

‎extensions/mattermost/src/mattermost/client.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,45 @@ describe("createMattermostClient", () => {
368368
);
369369
});
370370

371+
it("rejects relative API path segments before fetch", async () => {
372+
const { client, calls } = createTestClient();
373+
374+
await expect(client.request("/posts/../users/me")).rejects.toThrow(
375+
"Mattermost API path must not contain unsafe path segments",
376+
);
377+
378+
expect(calls).toEqual([]);
379+
});
380+
381+
it("rejects encoded relative API path segments before fetch", async () => {
382+
const { client, calls } = createTestClient();
383+
384+
await expect(client.request("/posts/%2e%2e/users/me")).rejects.toThrow(
385+
"Mattermost API path must not contain unsafe path segments",
386+
);
387+
388+
expect(calls).toEqual([]);
389+
});
390+
391+
it("rejects URL-normalized relative API path bypasses before fetch", async () => {
392+
const { client, calls } = createTestClient();
393+
394+
for (const path of [
395+
"/posts/..?x=1",
396+
"/posts/%2e%2e?x=1",
397+
"/posts\\..\\users/me",
398+
"/posts/.\n./users/me",
399+
"/posts/.%0a./users/me",
400+
"/posts/%2e%2e%2fusers%80%2f..%2fme",
401+
]) {
402+
await expect(client.request(path)).rejects.toThrow(
403+
"Mattermost API path must not contain unsafe path segments",
404+
);
405+
}
406+
407+
expect(calls).toEqual([]);
408+
});
409+
371410
it("sends Authorization header with Bearer token", async () => {
372411
const { mockFetch, calls } = createMockFetch({ body: { id: "u1" } });
373412
const client = createMattermostClient({

‎extensions/mattermost/src/mattermost/client.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,23 @@ export function normalizeMattermostBaseUrl(raw?: string | null): string | undefi
9191
return withoutTrailing.replace(/\/api\/v4$/i, "");
9292
}
9393

94-
function buildMattermostApiUrl(baseUrl: string, path: string): string {
94+
export function buildMattermostApiUrl(baseUrl: string, path: string): string {
9595
const normalized = normalizeMattermostBaseUrl(baseUrl);
9696
if (!normalized) {
9797
throw new Error("Mattermost baseUrl is required");
9898
}
99+
const pathname = (path.split(/[?#]/, 1)[0] ?? "").replace(/[\t\r\n]/g, "").replace(/\\/g, "/");
100+
for (const segment of pathname.split("/")) {
101+
let decoded: string;
102+
try {
103+
decoded = decodeURIComponent(segment).replace(/[\t\r\n]/g, "");
104+
} catch {
105+
throw new Error("Mattermost API path must not contain unsafe path segments");
106+
}
107+
if (decoded.split(/[\\/]/).some((part) => part === "." || part === "..")) {
108+
throw new Error("Mattermost API path must not contain unsafe path segments");
109+
}
110+
}
99111
const suffix = path.startsWith("/") ? path : `/${path}`;
100112
return `${normalized}/api/v4${suffix}`;
101113
}

‎extensions/mattermost/src/mattermost/monitor-resources.test.ts‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ const sendMattermostTyping = vi.hoisted(() => vi.fn());
77
const updateMattermostPost = vi.hoisted(() => vi.fn());
88
const buildButtonProps = vi.hoisted(() => vi.fn());
99

10-
vi.mock("./client.js", () => ({
11-
fetchMattermostChannel,
12-
fetchMattermostUser,
13-
sendMattermostTyping,
14-
updateMattermostPost,
15-
}));
10+
vi.mock("./client.js", async (importOriginal) => {
11+
const actual = await importOriginal<typeof import("./client.js")>();
12+
return {
13+
...actual,
14+
fetchMattermostChannel,
15+
fetchMattermostUser,
16+
sendMattermostTyping,
17+
updateMattermostPost,
18+
};
19+
});
1620

1721
vi.mock("./interactions.js", () => ({
1822
buildButtonProps,
@@ -104,6 +108,35 @@ describe("mattermost monitor resources", () => {
104108
});
105109
});
106110

111+
it("rejects unsafe file paths before media download", async () => {
112+
const saveRemoteMedia = vi.fn();
113+
const resources = createMattermostMonitorResources({
114+
accountId: "default",
115+
callbackUrl: "https://openclaw.test/callback",
116+
client: {
117+
apiBaseUrl: "https://chat.example.com/api/v4",
118+
baseUrl: "https://chat.example.com",
119+
token: "test-token",
120+
} as never,
121+
logger: {},
122+
mediaMaxBytes: 1024,
123+
saveRemoteMedia,
124+
mediaKindFromMime: () => "document",
125+
});
126+
127+
await expect(
128+
resources.resolveMattermostMedia([
129+
"../users/me",
130+
"%2e%2e/users/me",
131+
"..\\users\\me",
132+
".%0a./users/me",
133+
"%",
134+
]),
135+
).resolves.toEqual([]);
136+
137+
expect(saveRemoteMedia).not.toHaveBeenCalled();
138+
});
139+
107140
it("times out inbound media downloads when response headers never arrive", async () => {
108141
const { createServer } = await import("node:http");
109142
const { saveRemoteMedia } = await import("openclaw/plugin-sdk/media-runtime");

‎extensions/mattermost/src/mattermost/monitor-resources.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "openclaw/plugin-sdk/number-runtime";
88
import { normalizeStringEntries } from "openclaw/plugin-sdk/string-coerce-runtime";
99
import {
10+
buildMattermostApiUrl,
1011
fetchMattermostChannel,
1112
fetchMattermostUser,
1213
sendMattermostTyping,
@@ -124,7 +125,7 @@ export function createMattermostMonitorResources(params: {
124125
for (const fileId of ids) {
125126
try {
126127
const saved = await saveRemoteMedia({
127-
url: `${client.apiBaseUrl}/files/${fileId}`,
128+
url: buildMattermostApiUrl(client.baseUrl, `/files/${fileId}`),
128129
requestInit: {
129130
headers: {
130131
Authorization: `Bearer ${client.token}`,

0 commit comments

Comments
 (0)