# Cmdr bridge security

> Who a command runs as, how your hooks and allow-lists apply, how requests and commands are protected against replay, and what Bloxlytics can never do in your game.

The Cmdr bridge lets people outside your game cause things to happen inside it, so it is built so that **your game stays in charge**. Every command passes Bloxlytics' own permission checks, then your game's configuration, then Cmdr's validation and your own BeforeRun hooks. This page explains each layer. For Bloxlytics as a whole, see [security and privacy](https://docs.bloxlytics.app/security).

## Who a command runs as

Everyone signs in to Bloxlytics with their Roblox account, so Bloxlytics knows each team member's verified Roblox user id. That decides how a Cmdr command runs.

### As the sender's player

If the sender's Roblox account is **in the server** the command goes to, the bridge runs it with that `Player` as Cmdr's executor. Your BeforeRun hooks see `context.Executor` as their `Player`, and your in-game permissions apply exactly as if they had typed the command into Cmdr themselves. A team member who can't run `ban` in game can't run it from Bloxlytics either, and you don't need to change any hooks.

Replies are returned to Bloxlytics and also shown in that player's own Cmdr window.

When you don't pick a server or a target player, Bloxlytics sends the command to the server you are playing in, so that it runs as you.

### In the system context

Otherwise the command runs with **no executor** (`context.Executor` is `nil`), as a command from the platform. Your hook can recognise it with [`CmdrBridge.IsPlatformContext`](https://docs.bloxlytics.app/sdk/cmdr-bridge/reference#isplatformcontext) and read who sent it from `context.Data.Bloxlytics`:

| Field | Value |
| --- | --- |
| `Id` | The sender's Bloxlytics user id. |
| `Name` | The sender's name in Bloxlytics. |
| `Role` | Their role in the workspace: `owner`, `admin`, `developer`, `analyst` or `viewer`. |
| `ExecutionId` | The id of this execution in the command history. |

`context.Data.Bloxlytics` is set in the player context too, so a hook can tell that a command came through Bloxlytics either way; `IsPlatformContext` is only `true` for the system context. In the system context, `context:SendEvent` does nothing, because there is no player to send to.

Commands that assume `context.Executor` is a player should handle `nil`, or be refused in the system context by your hook.

### Refusing the system context

Set `AllowSystemCommands = false` to allow **only** the player context. Bloxlytics then won't send a command to that server unless the sender is in it, and the bridge refuses any that arrive anyway with `This game only runs Bloxlytics commands as a team member who is in the server (AllowSystemCommands = false).` Servers with this setting are also skipped when Bloxlytics picks "any server".

Every result records which context was used, `player` or `system`, in the command history.

## Hooks

For a Cmdr command, the bridge does this, in order, and stops at the first failure:

1. Looks up the first word of the command line with `Cmdr.Registry:GetCommand`. Unknown: `"<name>" is not a registered command.`
2. Checks the command's group against `AllowedGroups`.
3. Finds the executor (the sender's `Player`, if in the server) and applies `AllowSystemCommands`.
4. Parses the line with `Cmdr.Dispatcher:Evaluate`, passing the executor and `context.Data`.
5. Validates the arguments with Cmdr (`context:Validate`): types, required arguments, player names.
6. Runs your **BeforeRun** hooks with `Cmdr.Dispatcher:RunHooks("BeforeRun", context)`. Any hook returning a value rejects the command: `Rejected by BeforeRun hook: <value>`.
7. Runs the command and returns its replies and return value.

Cmdr refuses every command in a live game when no BeforeRun hook is registered, and because the bridge runs Cmdr's hooks, that applies to Bloxlytics too. The bridge runs BeforeRun hooks only; it doesn't call AfterRun hooks.

A hook that allows platform commands only for certain Bloxlytics roles:

```luau ServerScriptService/CmdrHooks.server.luau
local Cmdr = require(game.ServerScriptService.Cmdr)
local CmdrBridge = require(game.ServerScriptService.Analytics.CmdrBridge)

local PLATFORM_ROLES = { owner = true, admin = true }

Cmdr.Registry:RegisterHook("BeforeRun", function(context)
	if CmdrBridge.IsPlatformContext(context) then
		local sender = context.Data.Bloxlytics
		if not PLATFORM_ROLES[sender.Role] then
			return `{sender.Name} ({sender.Role}) can't run commands without being in the server.`
		end
		return nil
	end

	-- Your normal in-game check. Platform commands sent by a team member
	-- who is in the server also reach this point, as their own player.
	if context.Group == "Admin" and not isAdmin(context.Executor) then
		return "You don't have permission to run this command."
	end
	return nil
end)
```

`isAdmin` stands for your own permission check.

## Permission layers

A Cmdr command from Bloxlytics must pass every one of these:

| Layer | Where | What it checks |
| --- | --- | --- |
| Role permission | Bloxlytics | **Execute Cmdr commands** for commands, **Run macros** for macros. By default owners, admins and developers have them; see [roles and permissions](https://docs.bloxlytics.app/workspaces/roles-and-permissions). |
| **Cmdr groups per role** | Bloxlytics | If set for the sender's role in the SDK configuration, the command's Cmdr group must be in the list. |
| Registered command | Bloxlytics | The command must have been reported by one of your servers in the last 24 hours, with the right number of arguments. |
| **Cmdr bridge** switch | Bloxlytics | Must be on in the SDK configuration. |
| `AllowedGroups` | Your game | The command's Cmdr group must be in the list, if you set one. |
| `AllowSystemCommands` | Your game | With `false`, the sender must be in the server. |
| Cmdr validation | Your game | Argument types and values, checked by Cmdr. |
| BeforeRun hooks | Your game | Your own rules, with the sender as executor or the system context. |

Kicks and warnings are not Cmdr commands and don't pass through Cmdr or your hooks. They need the **Kick, warn, ban and unban players** permission in Bloxlytics, the target player must be in a server running the bridge, and your game must allow them: kicks unless `AllowKick = false`, warnings only if you pass `OnWarn`. `AllowedGroups` and `AllowSystemCommands` don't apply to them. Announcements run only through your `OnAnnounce` function.

## Group allow-lists

`AllowedGroups` is the in-game list of Cmdr groups Bloxlytics may run:

```luau
AllowedGroups = { "Admin", "DefaultAdmin" },
```

- Matching is exact and case-sensitive against each command's `Group`.
- When set, commands with **no group** are refused too.
- When left out (`nil`), every group is allowed, and your hooks are the only in-game check.

The bridge reports each command with an `allowed` flag, so Bloxlytics refuses commands outside the list before they are sent, and the bridge checks again when they arrive: `The <group> group isn't allowed from Bloxlytics (AllowedGroups).`

Use the dashboard's **Cmdr groups per role** for finer control per Bloxlytics role, and `AllowedGroups` as the hard limit your game enforces whatever the dashboard says.

## Replay protection

### Requests

Every request the bridge makes carries:

| Header | Value |
| --- | --- |
| `Authorization` | `Bearer` and the bridge key. |
| `X-Bloxlytics-Project` | The Universe ID. |
| `X-Bloxlytics-Timestamp` | The server's `os.time()`. |
| `X-Bloxlytics-Nonce` | A new GUID for every request. |

The API rejects, with `401`, a request whose timestamp is more than **120 seconds** from its own clock (`stale_request`), whose nonce is missing or malformed (`bad_nonce`), or whose nonce has been used before (`replayed`). A captured request can't be sent again. See [authentication](https://docs.bloxlytics.app/api/authentication).

### Commands

- Each command has a unique id and expires **60 seconds** after it was sent. Bloxlytics never hands out an expired command; it is marked as timed out instead.
- The bridge refuses a command whose expiry has passed by its own clock (`Command expired before it reached the server.`) and any id it has already run in the last 10 minutes (`Duplicate command ignored.`).
- A result is accepted only from the server the command was dispatched to, and only once. A second result for the same command is ignored.
- If no result arrives within 75 seconds, Bloxlytics marks the command as timed out.

## What Bloxlytics can never do

The bridge is built so that, even acting through it, Bloxlytics can't:

- **Run code.** There is no way to send Luau to the bridge. It never calls `loadstring` or evaluates source.
- **Run a command you didn't register.** Only commands in your Cmdr registry exist.
- **Skip your checks.** Every Cmdr command goes through Cmdr's argument validation and your BeforeRun hooks, and through `AllowedGroups` and `AllowSystemCommands`.
- **Act as a player who isn't there.** The player context needs that player to be in the server; otherwise the command has no executor.
- **Connect to your servers.** All traffic is outgoing HTTPS from your servers. Without the bridge installed, Bloxlytics can't reach a server at all.
- **Kick or warn without your permission.** Kicks stop with `AllowKick = false`; warnings and announcements only exist if you provide `OnWarn` and `OnAnnounce`.

The write key can't receive commands at all; it can only submit events and read the SDK's configuration.

> **Note:** The player context relies on Bloxlytics naming the right player: the bridge runs a command as whichever player in the server the command names, and Bloxlytics only ever names the sender's own verified Roblox account. If you'd rather your game didn't extend that trust, check `context.Data.Bloxlytics` in your hook for every command that has it, not only in the system context, for example by requiring an `owner` or `admin` role for dangerous commands.

### If a key leaks

Rotate it on the SDK page. Someone holding your bridge key could pretend to be one of your servers: register a fake command list, receive commands that would have gone to that server and report false results. They still couldn't run anything in your real servers. Rotating the key stops the old one at once; your servers need restarting with the new one. To report a security problem, see [reporting issues](https://docs.bloxlytics.app/security#reporting-issues).
