Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(discord-bot): match the OIDC repository claim exactly and require server permissions for /release

Richie McIlroy899f709b53dc
diff --git a/apps/discord-bot/src/index.ts b/apps/discord-bot/src/index.ts
index 3b2d0846a..710fe2a5d 100644
--- a/apps/discord-bot/src/index.ts
+++ b/apps/discord-bot/src/index.ts
@@ -6,6 +6,11 @@ import * as jose from "jose";
import { App } from "octokit";
import nacl from "tweetnacl";
import * as v from "valibot";
+import {
+ canManageGuild,
+ isGuildInvocation,
+ matchesRepositoryClaims,
+} from "./release-checks";
const DISCORD_APP_ID = "1334742236096757861";
const PUBLIC_KEY =
@@ -18,12 +23,16 @@ const GITHUB_APP_INSTALLATION = 63538726;
const WORKFLOW_FILE = "publish.yml";
const CHANNEL_MESSAGE_WITH_SOURCE = 4;
+const EPHEMERAL = 1 << 6;
type InteractionBody = {
id: string;
token: string;
channel_id: string;
+ guild_id?: string;
+ authorizing_integration_owners?: Partial<Record<string, string>>;
member: {
+ permissions?: string;
user: {
id: string;
username: string;
@@ -107,7 +116,7 @@ const ghActionsOidc = createMiddleware<{
if (!payload.sub) return new Response("Missing sub claim", { status: 401 });
- if (!payload.sub?.includes(`${GITHUB_ORG}/${GITHUB_REPO}`)) {
+ if (!matchesRepositoryClaims(payload, GITHUB_ORG, GITHUB_REPO)) {
return new Response("Invalid repository", { status: 401 });
}
@@ -235,6 +244,25 @@ async function handleCommand(interaction: InteractionBody, env: Env) {
switch (data.name) {
case "release": {
+ if (!isGuildInvocation(interaction))
+ return {
+ type: CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content: "/release can only be used in a server.",
+ flags: EPHEMERAL,
+ },
+ };
+
+ if (!canManageGuild(interaction.member.permissions))
+ return {
+ type: CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content:
+ "/release requires the Manage Server or Administrator permission.",
+ flags: EPHEMERAL,
+ },
+ };
+
await env.release_discord_interactions.put(
interaction.id,
JSON.stringify(interaction),
diff --git a/apps/discord-bot/src/release-checks.ts b/apps/discord-bot/src/release-checks.ts
new file mode 100644
index 000000000..e8a04b89b
--- /dev/null
+++ b/apps/discord-bot/src/release-checks.ts
@@ -0,0 +1,38 @@
+const ADMINISTRATOR = 1n << 3n;
+const MANAGE_GUILD = 1n << 5n;
+const GUILD_INSTALL = "0";
+
+export type ReleaseInvocation = {
+ guild_id?: string;
+ member?: { permissions?: string };
+ authorizing_integration_owners?: Partial<Record<string, string>>;
+};
+
+export function isGuildInvocation(interaction: ReleaseInvocation) {
+ if (typeof interaction.guild_id !== "string" || !interaction.guild_id)
+ return false;
+ if (!interaction.member) return false;
+
+ const owners = interaction.authorizing_integration_owners;
+ if (owners && owners[GUILD_INSTALL] !== interaction.guild_id) return false;
+
+ return true;
+}
+
+export function canManageGuild(permissions: unknown) {
+ if (typeof permissions !== "string" || !/^\d+$/.test(permissions))
+ return false;
+
+ return (BigInt(permissions) & (ADMINISTRATOR | MANAGE_GUILD)) !== 0n;
+}
+
+export function matchesRepositoryClaims(
+ payload: Record<string, unknown>,
+ owner: string,
+ repo: string,
+) {
+ return (
+ payload.repository === `${owner}/${repo}` &&
+ payload.repository_owner === owner
+ );
+}
diff --git a/apps/discord-bot/test/release-checks.spec.ts b/apps/discord-bot/test/release-checks.spec.ts
new file mode 100644
index 000000000..1ae47fd57
--- /dev/null
+++ b/apps/discord-bot/test/release-checks.spec.ts
@@ -0,0 +1,142 @@
+import { describe, expect, it } from "vitest";
+import {
+ canManageGuild,
+ isGuildInvocation,
+ matchesRepositoryClaims,
+} from "../src/release-checks";
+
+const GUILD_ID = "1100000000000000000";
+const USER_ID = "1200000000000000000";
+
+describe("isGuildInvocation", () => {
+ it("accepts a guild install invocation", () => {
+ expect(
+ isGuildInvocation({
+ guild_id: GUILD_ID,
+ member: { permissions: "8" },
+ authorizing_integration_owners: { "0": GUILD_ID },
+ }),
+ ).toBe(true);
+ });
+
+ it("accepts a guild invocation without integration owners", () => {
+ expect(
+ isGuildInvocation({ guild_id: GUILD_ID, member: { permissions: "8" } }),
+ ).toBe(true);
+ });
+
+ it("accepts a guild invocation authorized by both install types", () => {
+ expect(
+ isGuildInvocation({
+ guild_id: GUILD_ID,
+ member: { permissions: "8" },
+ authorizing_integration_owners: { "0": GUILD_ID, "1": USER_ID },
+ }),
+ ).toBe(true);
+ });
+
+ it("rejects a DM invocation", () => {
+ expect(
+ isGuildInvocation({
+ authorizing_integration_owners: { "1": USER_ID },
+ }),
+ ).toBe(false);
+ });
+
+ it("rejects a bot DM invocation", () => {
+ expect(
+ isGuildInvocation({ authorizing_integration_owners: { "0": "0" } }),
+ ).toBe(false);
+ });
+
+ it("rejects a user install invocation inside a guild", () => {
+ expect(
+ isGuildInvocation({
+ guild_id: GUILD_ID,
+ member: { permissions: "8" },
+ authorizing_integration_owners: { "1": USER_ID },
+ }),
+ ).toBe(false);
+ });
+
+ it("rejects a guild invocation without member data", () => {
+ expect(isGuildInvocation({ guild_id: GUILD_ID })).toBe(false);
+ });
+});
+
+describe("canManageGuild", () => {
+ it("accepts ADMINISTRATOR", () => {
+ expect(canManageGuild("8")).toBe(true);
+ });
+
+ it("accepts MANAGE_GUILD", () => {
+ expect(canManageGuild("32")).toBe(true);
+ });
+
+ it("reads bits above 2^53 without losing precision", () => {
+ expect(canManageGuild(((1n << 53n) | (1n << 5n)).toString())).toBe(true);
+ expect(canManageGuild(((1n << 53n) | 1n).toString())).toBe(false);
+ });
+
+ it("rejects members without either permission", () => {
+ expect(canManageGuild("0")).toBe(false);
+ expect(canManageGuild(((1n << 31n) | 0b111n).toString())).toBe(false);
+ });
+
+ it("rejects missing or malformed values", () => {
+ expect(canManageGuild(undefined)).toBe(false);
+ expect(canManageGuild("")).toBe(false);
+ expect(canManageGuild("-8")).toBe(false);
+ expect(canManageGuild("0x8")).toBe(false);
+ expect(canManageGuild(8)).toBe(false);
+ });
+});
+
+describe("matchesRepositoryClaims", () => {
+ it("accepts the exact repository and owner", () => {
+ expect(
+ matchesRepositoryClaims(
+ { repository: "CapSoftware/Cap", repository_owner: "CapSoftware" },
+ "CapSoftware",
+ "Cap",
+ ),
+ ).toBe(true);
+ });
+
+ it("rejects repositories that only contain the name", () => {
+ for (const repository of [
+ "CapSoftware/Cap-fork",
+ "someone/CapSoftware/Cap",
+ "CapSoftware/Caps",
+ ]) {
+ expect(
+ matchesRepositoryClaims(
+ { repository, repository_owner: "CapSoftware" },
+ "CapSoftware",
+ "Cap",
+ ),
+ ).toBe(false);
+ }
+ });
+
+ it("rejects a different owner", () => {
+ expect(
+ matchesRepositoryClaims(
+ { repository: "CapSoftware/Cap", repository_owner: "someone" },
+ "CapSoftware",
+ "Cap",
+ ),
+ ).toBe(false);
+ });
+
+ it("rejects missing claims", () => {
+ expect(matchesRepositoryClaims({}, "CapSoftware", "Cap")).toBe(false);
+ expect(
+ matchesRepositoryClaims(
+ { repository: "CapSoftware/Cap" },
+ "CapSoftware",
+ "Cap",
+ ),
+ ).toBe(false);
+ });
+});