NyxBot sheetSheet Note N3 · Sheet 03 TR / EN

Note N3 · NyxBot

SECURITY PASS

A security pass on a Discord bot: an unverified token, a permission gap in prefix commands and more

Sheet
NYXBOT
Topic
Node.js · Express · JWT · discord.js
Source
backend/routes/auth.js
Date
2026-11-12
Reading
3 min

Short answer: if you read anything from a token, verify its signature first; never take privilege flags (developer, admin) from a token or the client, compute them every time from server configuration. If a command has two entry points (slash and prefix), the permission check must be the same code in both. In NyxBot, breaking these two rules could hand developer access to every signed-in user and !ban to every member.

01Critical 1: developer access from an unverified token

A panel session lives on a short-lived access token and a long-lived refresh token. The refresh endpoint carried user details over from the old token when issuing a new one, but it read the old one with jwt.decode. decode does not check the signature.

// before
const decoded = jwt.decode(currentAccessToken);
isDev: Boolean(decoded.isDev || isDeveloper(decoded.id))

Any signed-in user could send a fake token with isDev: true together with their valid refresh token and get developer access: every server's ticket transcripts and live events.

// after
const decoded = jwt.verify(currentAccessToken, JWT_SECRET, { algorithms: ['HS256'], ignoreExpiration: true });
if (decoded && String(decoded.id) === String(tokenRecord.userId)) { ... }
// The privilege flag is always computed on the server
isDev: isDeveloper(userId)
  • The old token is read only for the display name and avatar; its signature is verified, an expired token is accepted.
  • If the id in the token doesn't match the refresh record's owner, it is ignored. Identity and privileges always come from the refresh record.
  • The algorithm is pinned to HS256; the access token's lifetime went from 30 days to 1, and the session continues through refresh.

A test comes with the fix: a refresh with a token carrying a fake isDev must return an unprivileged session.

02Critical 2: prefix commands skipped permissions

Commands are defined as slash commands, and Discord itself enforces the permission set with setDefaultMemberPermissions: someone without the Ban Members permission doesn't even see /ban. NyxBot also runs the same commands with a prefix, such as !ban. On that path the bot, not Discord, had to check the permission, and it didn't: every member on the server could run !ban or !nuke.

function hasCommandPermission(command, member) {
  const required = requiredPermissions(command);
  if (required === null) return true;                 // command needs no permission
  if (!member?.permissions) return false;
  if (required === '0') return member.permissions.has(PermissionFlagsBits.Administrator);
  return member.permissions.has(BigInt(required));
}

The check reads the permission from the slash command's definition; no separate permission list is kept for the two paths. "0" means "administrators only" on Discord, and that behaviour carried over too. The tests cover both the function (member without permission, the right permission, administrator, admin-only command, command with no permission, a message without a member) and the end-to-end flow: an unprivileged member's !ban must not run and must get a reply containing "yetki".

03Critical 3: the transcript could run scripts in the panel

Closed tickets' transcripts were shown in the panel inside an iframe with both allow-same-origin and allow-scripts. A transcript carries what members wrote; that pair means member content can run scripts in the panel's origin. The iframe was moved to an opaque origin, and Content-Security-Policy: sandbox was added to the API's HTML response.

04Closed in the same pass

  • The category id in the body was embedded straight into SQL when opening a manual ticket; it moved to a parameterised query with validation.
  • Ticket channel permissions granted almost every right through a negative bit field; they were reduced to View, Send, Attach and History.
  • Server ids are validated as snowflakes; 500 responses hide internal error text; prototype keys such as __proto__ are rejected in module names.
  • The image proxy doesn't follow redirects, rejects SVG, limits size while streaming; responses carry sandbox and nosniff.
  • The custom command preview put role and channel names into innerHTML without escaping; fixed.
  • An API secret left in the code was moved to an environment variable.

05Lesson

The three critical holes share a root: trusting a value from an untrusted source. A token's content is text the client wrote until it is signed; a command's second entry point doesn't inherit the first one's security assumptions; text a member wrote is not something to run in the panel's origin. All fixes were closed in one commit together with their tests; part of the 64 backend test files comes from this pass.

→More notes from this project

—End of notewritten by MUHAMMEDPYZ