Fix deploy and lock down the DM route; add /invitation
All checks were successful
Deploy Palico Bot / deploy (push) Successful in 32s

- Deploy workflow: pin node:24-bookworm; install openssh-client only if missing
- Auth failure limiter counts only 401s (422/404 no longer lock out clients)
- trust proxy configurable (WEB_TRUSTED_PROXIES), never "true"
- DM route: loopback connections only, dedicated NOTIFICATIONS_API_KEY, 503 when unset
- Jukebox: no polling without a key, stop on 401
- /invitation: posts the temporary invite message after checking the link
- Stop tracking storage/library.json; README describes the real deployment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-29 10:57:22 +01:00
parent 3655b1ad74
commit 6895d20459
16 changed files with 554 additions and 99 deletions

View file

@ -31,7 +31,7 @@ async function loadCommands() {
test("all Discord commands expose valid command data", async () => {
const modules = await loadCommands();
assert.equal(modules.length, 9);
assert.equal(modules.length, 10);
for (const module of modules) {
assert.doesNotThrow(() => module.command.data.toJSON());
assert.equal(typeof module.command.execute, "function");
@ -42,6 +42,7 @@ test("administrative commands require Discord permissions", async () => {
const modules = await loadCommands();
const protectedCommands = new Set([
"deletemsgs",
"invitation",
"muteall",
"unmuteall",
"deafall",

84
test/invitation.test.js Normal file
View file

@ -0,0 +1,84 @@
import test from "node:test";
import assert from "node:assert/strict";
import { DiscordjsErrorCodes } from "discord.js";
import {
InvitationError,
buildInvitationMessage,
checkInvitation,
parseInviteCode,
} from "../src/features/invitation.js";
const GUILD_ID = "111111111111111111";
function fakeGuild({ invite = null, fetchError = null } = {}) {
return {
id: GUILD_ID,
invites: {
async fetch() {
if (fetchError) throw fetchError;
if (!invite) throw Object.assign(new Error("not found"), { code: DiscordjsErrorCodes.InviteNotFound });
return invite;
},
},
};
}
function fakeClient(publicInvite) {
return {
async fetchInvite() {
if (!publicInvite) throw Object.assign(new Error("Unknown Invite"), { code: 10006 });
return publicInvite;
},
};
}
test("parseInviteCode accepts invite links and bare codes only", () => {
for (const input of ["https://discord.gg/AbC-12", "discord.gg/AbC-12", "https://discord.com/invite/AbC-12", " AbC-12 "]) {
assert.equal(parseInviteCode(input), "AbC-12", input);
}
for (const input of ["https://evil.example/AbC-12", "https://discord.gg/", "a b", "", undefined]) {
assert.equal(parseInviteCode(input), null, String(input));
}
});
test("a temporary invite without limits is published as is", async () => {
const guild = fakeGuild({ invite: { code: "AbC-12", temporary: true, maxUses: 0, expiresTimestamp: null } });
assert.deepEqual(await checkInvitation(guild, fakeClient(), "discord.gg/AbC-12"), {
link: "https://discord.gg/AbC-12",
warnings: [],
});
});
test("permanent and unknown invites are refused", async () => {
const permanent = fakeGuild({ invite: { code: "AbC-12", temporary: false } });
await assert.rejects(checkInvitation(permanent, fakeClient(), "AbC-12"), InvitationError);
await assert.rejects(checkInvitation(fakeGuild(), fakeClient(), "AbC-12"), InvitationError);
await assert.rejects(checkInvitation(fakeGuild(), fakeClient(), "pas un lien"), InvitationError);
});
test("limited or expiring invites are published with warnings", async () => {
const guild = fakeGuild({
invite: { code: "AbC-12", temporary: true, maxUses: 5, uses: 2, expiresTimestamp: 1_800_000_000_000 },
});
const { warnings } = await checkInvitation(guild, fakeClient(), "AbC-12");
assert.equal(warnings.length, 2);
});
test("without Manage Server the bot checks only the guild", async () => {
const missingPermissions = Object.assign(new Error("Missing Permissions"), { code: 50013 });
const guild = fakeGuild({ fetchError: missingPermissions });
const sameGuild = fakeClient({ code: "AbC-12", guild: { id: GUILD_ID }, temporary: null });
const result = await checkInvitation(guild, sameGuild, "AbC-12");
assert.equal(result.link, "https://discord.gg/AbC-12");
assert.equal(result.warnings.length, 1);
const otherGuild = fakeClient({ code: "AbC-12", guild: { id: "222222222222222222" }, temporary: null });
await assert.rejects(checkInvitation(guild, otherGuild, "AbC-12"), InvitationError);
});
test("the invitation message shows the link and how to stay permanently", () => {
const message = buildInvitationMessage("https://discord.gg/AbC-12");
assert.match(message, /```\nhttps:\/\/discord\.gg\/AbC-12\n```/);
assert.match(message, /Hangaround/);
});

116
test/webServer.test.js Normal file
View file

@ -0,0 +1,116 @@
import test from "node:test";
import assert from "node:assert/strict";
Object.assign(process.env, {
NODE_ENV: "test",
DISCORD_GUILD_ID: "1",
SHINUWA_USER_ID: "1",
WEB_SERVER_PORT: "3000",
WEB_API_KEY: "web-key",
NOTIFICATIONS_API_KEY: "notifications-key",
DISCORD_APPLICATION_ID: "1",
DISCORD_TOKEN: "test-token",
VOICE_CHANNEL_ID: "1",
TEXT_CHANNEL_ID: "1",
GAME_SERVERS_MESSAGE_ID: "1",
});
const { createApp } = await import("../src/web/server.js");
const { isLoopbackAddress } = await import("../src/web/middlewares/auth.js");
const USER_ID = "123456789012345678";
const CLOSED_USER_ID = "223456789012345678";
function fakeClient() {
const sent = [];
return {
sent,
users: {
async fetch(userId) {
return {
async send(message) {
if (userId === CLOSED_USER_ID) {
throw Object.assign(new Error("Cannot send messages to this user"), { code: 50007 });
}
sent.push({ userId, ...message });
},
};
},
},
};
}
const fakeQueue = {
snapshot: () => ({ current: null, upcoming: [], status: "idle" }),
};
async function withServer(options, run) {
const client = fakeClient();
const app = createApp({ queue: fakeQueue, client }, options);
const server = app.listen(0, "127.0.0.1");
await new Promise((resolve) => server.once("listening", resolve));
const base = `http://127.0.0.1:${server.address().port}`;
try {
await run({ base, client });
} finally {
await new Promise((resolve) => server.close(resolve));
}
}
function sendDm(base, key, userId = USER_ID) {
return fetch(`${base}/api/notifications/dm`, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${key}` },
body: JSON.stringify({ userId, content: "Votre expédition est de retour !" }),
});
}
test("the DM route accepts only the notifications key", async () => {
await withServer({}, async ({ base, client }) => {
assert.equal((await sendDm(base, "notifications-key")).status, 204);
assert.equal((await sendDm(base, "web-key")).status, 401);
assert.equal(client.sent.length, 1);
});
});
test("refused DMs do not lock out an authenticated application", async () => {
await withServer({}, async ({ base, client }) => {
for (let index = 0; index < 25; index++) {
assert.equal((await sendDm(base, "notifications-key", CLOSED_USER_ID)).status, 422);
}
assert.equal((await sendDm(base, "notifications-key")).status, 204);
assert.equal(client.sent.length, 1);
});
});
test("repeated wrong keys are still rate limited", async () => {
await withServer({}, async ({ base }) => {
for (let index = 0; index < 20; index++) {
const response = await fetch(`${base}/api/queue`, {
headers: { Authorization: "Bearer wrong-key" },
});
assert.equal(response.status, 401);
}
const blocked = await fetch(`${base}/api/queue`, {
headers: { Authorization: "Bearer web-key" },
});
assert.equal(blocked.status, 429);
});
});
test("the DM route is disabled without a notifications key", async () => {
await withServer({ notificationsApiKey: null }, async ({ base, client }) => {
const response = await sendDm(base, "notifications-key");
assert.equal(response.status, 503);
assert.equal(client.sent.length, 0);
});
});
test("only loopback connections count as local", () => {
for (const address of ["127.0.0.1", "::1", "::ffff:127.0.0.1"]) {
assert.equal(isLoopbackAddress(address), true);
}
for (const address of ["192.168.0.104", "213.251.163.57", "::ffff:192.168.0.104", undefined]) {
assert.equal(isLoopbackAddress(address), false);
}
});