mirror of
https://github.com/outline/outline.git
synced 2026-12-06 03:05:08 +03:00
* wip * fix: Address review feedback on Granola integration - Limit the size of Granola API responses - Load the team when authorizing notes into personal collections - Add the user's connection from granola.list to the integrations store * feat: Connect Granola from a dialog * fix: Accept Granola webhook test events * Copy pass * Copy pass
242 lines
7.4 KiB
TypeScript
242 lines
7.4 KiB
TypeScript
import crypto from "node:crypto";
|
|
import { IntegrationService, IntegrationType } from "@shared/types";
|
|
import { Integration } from "@server/models";
|
|
import {
|
|
buildAdmin,
|
|
buildGuestUser,
|
|
buildIntegration,
|
|
buildTeam,
|
|
buildUser,
|
|
buildViewer,
|
|
} from "@server/test/factories";
|
|
import { getTestServer, mockTaskSchedule } from "@server/test/support";
|
|
import { GitHub, InstallationAccess } from "../github";
|
|
import env from "../env";
|
|
import { SetupAction } from "./schema";
|
|
|
|
const server = getTestServer();
|
|
|
|
describe("#github.callback", () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("should reject an installation the user cannot manage", async () => {
|
|
const user = await buildAdmin();
|
|
const state = JSON.stringify({ teamId: user.teamId, nonce: "nonce" });
|
|
const client = {
|
|
requestAppInstallations: async () => [
|
|
{ id: 1, permissions: { issues: "read" }, account: null },
|
|
],
|
|
} as unknown as Awaited<ReturnType<typeof GitHub.authenticateAsUser>>;
|
|
vi.spyOn(GitHub, "authenticateAsUser").mockResolvedValue(client);
|
|
const spy = vi
|
|
.spyOn(GitHub, "canManageInstallation")
|
|
.mockResolvedValue(InstallationAccess.Denied);
|
|
|
|
const res = await server.get(
|
|
`/api/github.callback?state=${encodeURIComponent(
|
|
state
|
|
)}&code=123&setup_action=${SetupAction.install}&installation_id=1`,
|
|
user,
|
|
{
|
|
redirect: "manual",
|
|
headers: { Cookie: "githubOAuthNonce=nonce" },
|
|
}
|
|
);
|
|
|
|
expect(res.status).toEqual(302);
|
|
expect(res.headers.get("location")).toContain("error=denied");
|
|
expect(spy).toHaveBeenCalledWith(
|
|
client,
|
|
expect.objectContaining({ id: 1 })
|
|
);
|
|
expect(
|
|
await Integration.count({
|
|
where: { teamId: user.teamId, service: IntegrationService.GitHub },
|
|
})
|
|
).toEqual(0);
|
|
});
|
|
|
|
it("should reject callback when state cookie is missing", async () => {
|
|
const user = await buildUser();
|
|
const state = JSON.stringify({
|
|
teamId: user.teamId,
|
|
nonce: "state-nonce",
|
|
});
|
|
const res = await server.get(
|
|
`/api/github.callback?state=${encodeURIComponent(
|
|
state
|
|
)}&code=123&setup_action=${SetupAction.install}&installation_id=1`,
|
|
user,
|
|
{ redirect: "manual" }
|
|
);
|
|
const body = await res.json();
|
|
expect(res.status).toEqual(400);
|
|
expect(body.error).toEqual("state_mismatch");
|
|
expect(body.message).toEqual("OAuth state cookie was missing");
|
|
});
|
|
|
|
it("should reject callback when state nonce does not match cookie", async () => {
|
|
const user = await buildUser();
|
|
const state = JSON.stringify({
|
|
teamId: user.teamId,
|
|
nonce: "attacker-nonce",
|
|
});
|
|
const res = await server.get(
|
|
`/api/github.callback?state=${encodeURIComponent(
|
|
state
|
|
)}&code=123&setup_action=${SetupAction.install}&installation_id=1`,
|
|
user,
|
|
{
|
|
redirect: "manual",
|
|
headers: { Cookie: "githubOAuthNonce=cookie-nonce" },
|
|
}
|
|
);
|
|
const body = await res.json();
|
|
expect(res.status).toEqual(400);
|
|
expect(body.error).toEqual("state_mismatch");
|
|
expect(body.message).toEqual("State returned in OAuth flow did not match");
|
|
});
|
|
|
|
it("should reject callback when nonce is missing from state", async () => {
|
|
const user = await buildUser();
|
|
const state = JSON.stringify({ teamId: user.teamId });
|
|
const res = await server.get(
|
|
`/api/github.callback?state=${encodeURIComponent(
|
|
state
|
|
)}&code=123&setup_action=${SetupAction.install}&installation_id=1`,
|
|
user,
|
|
{
|
|
redirect: "manual",
|
|
headers: { Cookie: "githubOAuthNonce=cookie-nonce" },
|
|
}
|
|
);
|
|
const body = await res.json();
|
|
expect(res.status).toEqual(400);
|
|
expect(body.error).toEqual("state_mismatch");
|
|
expect(body.message).toEqual("State returned in OAuth flow was missing");
|
|
});
|
|
|
|
it("should fail when state is not valid JSON", async () => {
|
|
const user = await buildUser();
|
|
const res = await server.get(
|
|
`/api/github.callback?state=bad&code=123&setup_action=${SetupAction.install}&installation_id=1`,
|
|
user,
|
|
{ redirect: "manual" }
|
|
);
|
|
expect(res.status).toEqual(400);
|
|
});
|
|
|
|
it.each([
|
|
["member", buildUser],
|
|
["viewer", buildViewer],
|
|
])("should not allow a %s to create an integration", async (_, build) => {
|
|
const user = await build();
|
|
const state = JSON.stringify({ teamId: user.teamId, nonce: "nonce" });
|
|
const res = await server.get(
|
|
`/api/github.callback?state=${encodeURIComponent(state)}&code=123&setup_action=${SetupAction.install}&installation_id=1`,
|
|
user,
|
|
{
|
|
redirect: "manual",
|
|
headers: { Cookie: "githubOAuthNonce=nonce" },
|
|
}
|
|
);
|
|
expect(res.status).toEqual(403);
|
|
});
|
|
});
|
|
|
|
describe("#integrations.list", () => {
|
|
it("should expose the github settings of the integration", async () => {
|
|
const team = await buildTeam();
|
|
const member = await buildUser({ teamId: team.id });
|
|
const installation = {
|
|
id: 1,
|
|
account: { id: 2, name: "outline", avatarUrl: "https://example.com" },
|
|
};
|
|
await buildIntegration({
|
|
userId: member.id,
|
|
teamId: team.id,
|
|
service: IntegrationService.GitHub,
|
|
type: IntegrationType.Embed,
|
|
settings: {
|
|
github: { installation },
|
|
url: "https://example.com",
|
|
},
|
|
});
|
|
|
|
const res = await server.post("/api/integrations.list", member);
|
|
const body = await res.json();
|
|
|
|
expect(res.status).toEqual(200);
|
|
expect(body.data[0].settings).toEqual({
|
|
url: "https://example.com",
|
|
github: { installation },
|
|
});
|
|
});
|
|
|
|
it("should not expose github settings to guests", async () => {
|
|
const team = await buildTeam();
|
|
const admin = await buildAdmin({ teamId: team.id });
|
|
const guest = await buildGuestUser({ teamId: team.id });
|
|
await buildIntegration({
|
|
userId: admin.id,
|
|
teamId: team.id,
|
|
service: IntegrationService.GitHub,
|
|
type: IntegrationType.Embed,
|
|
settings: {
|
|
url: "https://example.com",
|
|
github: {
|
|
installation: {
|
|
id: 123,
|
|
account: { id: 456, name: "acme", avatarUrl: "" },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const res = await server.post("/api/integrations.list", guest);
|
|
const body = await res.json();
|
|
|
|
expect(res.status).toEqual(200);
|
|
expect(body.data[0].settings).toEqual({ url: "https://example.com" });
|
|
});
|
|
});
|
|
|
|
describe("#github.webhooks", () => {
|
|
const schedule = mockTaskSchedule();
|
|
|
|
// Spaces and non-ASCII text are not kept when the parsed body is stringified again.
|
|
const body = '{ "action": "opened", "title": "Caf\\u00e9 ☕" }';
|
|
|
|
const deliver = (signedBody: string) =>
|
|
server.post("/api/github.webhooks", {
|
|
body,
|
|
headers: {
|
|
"content-type": "application/json",
|
|
"x-hub-signature-256": `sha256=${crypto
|
|
.createHmac("sha256", env.GITHUB_WEBHOOK_SECRET!)
|
|
.update(signedBody)
|
|
.digest("hex")}`,
|
|
},
|
|
});
|
|
|
|
it("accepts a signature of the raw body", async () => {
|
|
const res = await deliver(body);
|
|
|
|
expect(res.status).toEqual(202);
|
|
expect(schedule).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
payload: { action: "opened", title: "Café ☕" },
|
|
})
|
|
);
|
|
});
|
|
|
|
it("rejects a signature of the parsed body", async () => {
|
|
const res = await deliver(JSON.stringify(JSON.parse(body)));
|
|
|
|
expect(res.status).toEqual(401);
|
|
expect(schedule).not.toHaveBeenCalled();
|
|
});
|
|
});
|