mirror of
https://github.com/coder/coder.git
synced 2026-11-19 18:51:12 +08:00
To prevent malicious apps and vendors to use the Coder session token we are adding safe protocols/schemas we want to support. - vscode: - vscode-insiders: - windsurf: - cursor: - jetbrains-gateway: - jetbrains: Fix https://github.com/coder/security/issues/77
136 lines
3.6 KiB
TypeScript
136 lines
3.6 KiB
TypeScript
import {
|
|
MockWorkspace,
|
|
MockWorkspaceAgent,
|
|
MockWorkspaceApp,
|
|
} from "testHelpers/entities";
|
|
import { SESSION_TOKEN_PLACEHOLDER, getAppHref } from "./apps";
|
|
|
|
describe("getAppHref", () => {
|
|
it("returns the URL without changes when external app has regular URL", () => {
|
|
const externalApp = {
|
|
...MockWorkspaceApp,
|
|
external: true,
|
|
url: "https://example.com",
|
|
};
|
|
const href = getAppHref(externalApp, {
|
|
host: "*.apps-host.tld",
|
|
path: "/path-base",
|
|
agent: MockWorkspaceAgent,
|
|
workspace: MockWorkspace,
|
|
});
|
|
expect(href).toBe(externalApp.url);
|
|
});
|
|
|
|
it("returns the URL with the session token replaced when external app needs session token", () => {
|
|
const externalApp = {
|
|
...MockWorkspaceApp,
|
|
external: true,
|
|
url: `vscode://example.com?token=${SESSION_TOKEN_PLACEHOLDER}`,
|
|
};
|
|
const href = getAppHref(externalApp, {
|
|
host: "*.apps-host.tld",
|
|
path: "/path-base",
|
|
agent: MockWorkspaceAgent,
|
|
workspace: MockWorkspace,
|
|
token: "user-session-token",
|
|
});
|
|
expect(href).toBe("vscode://example.com?token=user-session-token");
|
|
});
|
|
|
|
it("doesn't return the URL with the session token replaced when using the HTTP protocol", () => {
|
|
const externalApp = {
|
|
...MockWorkspaceApp,
|
|
external: true,
|
|
url: `https://example.com?token=${SESSION_TOKEN_PLACEHOLDER}`,
|
|
};
|
|
const href = getAppHref(externalApp, {
|
|
host: "*.apps-host.tld",
|
|
path: "/path-base",
|
|
agent: MockWorkspaceAgent,
|
|
workspace: MockWorkspace,
|
|
token: "user-session-token",
|
|
});
|
|
expect(href).toBe(externalApp.url);
|
|
});
|
|
|
|
it("doesn't return the URL with the session token replaced when using unauthorized protocol", () => {
|
|
const externalApp = {
|
|
...MockWorkspaceApp,
|
|
external: true,
|
|
url: `ftp://example.com?token=${SESSION_TOKEN_PLACEHOLDER}`,
|
|
};
|
|
const href = getAppHref(externalApp, {
|
|
host: "*.apps-host.tld",
|
|
agent: MockWorkspaceAgent,
|
|
workspace: MockWorkspace,
|
|
path: "/path-base",
|
|
token: "user-session-token",
|
|
});
|
|
expect(href).toBe(externalApp.url);
|
|
});
|
|
|
|
it("returns a path when app doesn't use a subdomain", () => {
|
|
const app = {
|
|
...MockWorkspaceApp,
|
|
subdomain: false,
|
|
};
|
|
const href = getAppHref(app, {
|
|
host: "*.apps-host.tld",
|
|
agent: MockWorkspaceAgent,
|
|
workspace: MockWorkspace,
|
|
path: "/path-base",
|
|
});
|
|
expect(href).toBe(
|
|
`/path-base/@${MockWorkspace.owner_name}/Test-Workspace.a-workspace-agent/apps/${app.slug}/`,
|
|
);
|
|
});
|
|
|
|
it("includes the command in the URL when app has a command", () => {
|
|
const app = {
|
|
...MockWorkspaceApp,
|
|
command: "ls -la",
|
|
};
|
|
const href = getAppHref(app, {
|
|
host: "*.apps-host.tld",
|
|
agent: MockWorkspaceAgent,
|
|
workspace: MockWorkspace,
|
|
path: "",
|
|
});
|
|
expect(href).toBe(
|
|
`/@${MockWorkspace.owner_name}/Test-Workspace.a-workspace-agent/terminal?command=ls%20-la`,
|
|
);
|
|
});
|
|
|
|
it("uses the subdomain when app has a subdomain", () => {
|
|
const app = {
|
|
...MockWorkspaceApp,
|
|
subdomain: true,
|
|
subdomain_name: "hellocoder",
|
|
};
|
|
const href = getAppHref(app, {
|
|
host: "*.apps-host.tld",
|
|
agent: MockWorkspaceAgent,
|
|
workspace: MockWorkspace,
|
|
path: "/path-base",
|
|
});
|
|
expect(href).toBe("http://hellocoder.apps-host.tld/");
|
|
});
|
|
|
|
it("returns a path when app has a subdomain but no subdomain name", () => {
|
|
const app = {
|
|
...MockWorkspaceApp,
|
|
subdomain: true,
|
|
subdomain_name: undefined,
|
|
};
|
|
const href = getAppHref(app, {
|
|
host: "*.apps-host.tld",
|
|
agent: MockWorkspaceAgent,
|
|
workspace: MockWorkspace,
|
|
path: "/path-base",
|
|
});
|
|
expect(href).toBe(
|
|
`/path-base/@${MockWorkspace.owner_name}/Test-Workspace.a-workspace-agent/apps/${app.slug}/`,
|
|
);
|
|
});
|
|
});
|