Skip to content

Commit 8dab2de

Browse files
authored
[activities] add Validating Proxy Request Headers documentation (#7809)
1 parent 7a6449d commit 8dab2de

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

docs/activities/development-guides/multiplayer-experience.mdx

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ Activities are surfaced through iframes in the Discord app. The activity website
138138
139139
It is theoretically possible for a malicious client to mock Discord's RPC protocol or load one activity website when launching another. Because the activity is loaded inside Discord, the RPC protocol is active, and the activity is none the wiser.
140140
141+
### Using the Activity Instance API
142+
141143
To enable an activity to "lock down" activity access, we encourage utilizing the `get_activity_instance` API, found at `discord.com/api/applications/<application_id>/activity-instances/<instance_id>'`. The route requires a Bot token of the application. It returns a serialized active activity instance for the given application, if found, otherwise it returns a 404. Here are two example responses:
142144
143145
```javascript
@@ -150,5 +152,99 @@ curl https://discord.com/api/applications/1215413995645968394/activity-instances
150152
151153
With this API, the activity's backend can verify that a client is in fact in an instance of that activity before allowing the client to participate in any meaningful gameplay. How an activity implements "session verification" is left to the developer's discretion. The solution can be as granular as gating specific features or as binary as not returning the activity HTML except for valid sessions.
152154
155+
###### Validating Proxy Request Headers
156+
157+
For apps that want additional security validation, Discord provides an optional proxy authentication system. When your embedded app makes requests through Discord's proxy, each request can include cryptographic headers that prove the request's authenticity.
158+
159+
Each proxy-authenticated request is sent with the following headers:
160+
161+
- `X-Signature-Ed25519` as a cryptographic signature
162+
- `X-Signature-Timestamp` as a Unix timestamp
163+
- `X-Discord-Proxy-Payload` as a base64-encoded payload containing user context
164+
165+
If you choose to use proxy authentication, you can validate these headers to ensure requests are legitimate. If the signature fails validation, your app should respond with a `401` error code.
166+
167+
<Collapsible title="Validating Proxy Headers" description="Code example for validating proxy authentication headers" icon="code">
168+
Below are some code examples that show how to validate the headers sent in proxy-authenticated requests.
169+
170+
**JavaScript**
171+
172+
```js
173+
const nacl = require("tweetnacl");
174+
175+
// Your public key can be found on your application in the Developer Portal
176+
const PUBLIC_KEY = "APPLICATION_PUBLIC_KEY";
177+
178+
const signature = req.get("X-Signature-Ed25519");
179+
const timestamp = req.get("X-Signature-Timestamp");
180+
const payload = req.get("X-Discord-Proxy-Payload");
181+
182+
// Decode the base64 payload
183+
const payloadBytes = Buffer.from(payload, "base64");
184+
const payloadString = payloadBytes.toString("utf-8");
185+
const payloadData = JSON.parse(payloadString);
186+
187+
// Verify timestamp matches payload
188+
if (payloadData.created_at.toString() !== timestamp) {
189+
return res.status(401).end("invalid request timestamp");
190+
}
191+
192+
// Check if token has expired
193+
if (payloadData.expires_at < Math.floor(Date.now() / 1000)) {
194+
return res.status(401).end("expired proxy token");
195+
}
196+
197+
// Verify the signature using tweetnacl
198+
const isVerified = nacl.sign.detached.verify(
199+
payloadBytes,
200+
Buffer.from(signature, "base64"),
201+
Buffer.from(PUBLIC_KEY, "hex")
202+
);
203+
204+
if (!isVerified) {
205+
return res.status(401).end("invalid request signature");
206+
}
207+
```
208+
209+
**Python**
210+
211+
```py
212+
import json
213+
import base64
214+
import time
215+
from nacl.signing import VerifyKey
216+
from nacl.exceptions import BadSignatureError
217+
218+
# Your public key can be found on your application in the Developer Portal
219+
PUBLIC_KEY = 'APPLICATION_PUBLIC_KEY'
220+
221+
verify_key = VerifyKey(bytes.fromhex(PUBLIC_KEY))
222+
223+
signature = request.headers["X-Signature-Ed25519"]
224+
timestamp = request.headers["X-Signature-Timestamp"]
225+
payload = request.headers["X-Discord-Proxy-Payload"]
226+
227+
# Decode the base64 payload
228+
payload_bytes = base64.b64decode(payload)
229+
payload_string = payload_bytes.decode('utf-8')
230+
payload_data = json.loads(payload_string)
231+
232+
# Verify timestamp matches payload
233+
if str(payload_data['created_at']) != timestamp:
234+
abort(401, 'invalid request timestamp')
235+
236+
# Check if token has expired
237+
if payload_data['expires_at'] < int(time.time()):
238+
abort(401, 'expired proxy token')
239+
240+
try:
241+
verify_key.verify(payload_bytes, bytes.fromhex(signature))
242+
except BadSignatureError:
243+
abort(401, 'invalid request signature')
244+
```
245+
</Collapsible>
246+
247+
Proxy authentication is entirely optional and provided as an additional security layer for apps that choose to implement it.
248+
153249
In the below flow diagram, we show how the server can deliver the activity website, only for valid users in a valid activity instance:
154250
![application-test-mode-prod](images/activities/activity-instance-validation.webp)

0 commit comments

Comments
 (0)