Scripting Chaos¶
Scripting Chaos adds, level by level, HTTP tokens that the client must send back along the checkout flow. It is there to practise load-test scripting (JMeter, k6, Locust, Gatling): extracting a value from a response, sending it in the following requests, tracking values that change at every step.
It does not protect the shop. With the right headers the order goes through at every level; without them the server rejects the request and its message says what is missing.
Routes involved¶
| Route | Role in the exercise |
|---|---|
POST /api/auth/login |
returns the tokens of the active level, in the response headers |
POST /api/checkout/address |
checked step |
POST /api/checkout/shipping |
checked step |
POST /api/checkout/payment |
checked step |
POST /api/orders |
checked step (the order) |
The catalogue (/api/products) and the cart (/api/cart) are not checked at any level.
Levels¶
| Level | Name | Headers to send on the four checked steps |
|---|---|---|
| 0 | Disabled | none |
| 1 | Junior | X-Session-Token, X-Request-ID |
| 2 | Intermediate | the same, plus X-Action-Token |
| 3 | Expert | the same, plus X-CSRF-Token, X-Step-Token, X-Signature |
| 4 | Maestro | the same as level 3 |
Level 4 is called Maestro, not Master as in the other chaos families. Level 2 is named Intermediate by the API and shown as Confirmed in Chaos Admin.
What the server returns¶
Tokens travel in headers, never in the body. They are bound to the HTTP session: the script keeps the session cookie from start to finish (in JMeter, a cookie manager).
| Header | Returned by | Shape | How it evolves |
|---|---|---|---|
X-Session-Token |
login, then each of the three steps of the flow | 36 characters | does not change during the session |
X-Request-ID |
login | 36 characters | request identifier: the client supplies its own on every call |
X-Action-Token |
login, then each step (level 2 and above) | 36 characters | does not change during the session |
X-CSRF-Token |
login, then each step (levels 3 and 4) | 32 characters | changes after every successful step |
X-Step-Token |
login, then each step (levels 3 and 4) | step2 to step5 |
moves one notch after every successful step |
X-Signature |
login, then each step (levels 3 and 4) | 43 characters | changes after every successful step |
X-Key-Hint |
login (level 4) | 8 characters | a hint; the client does not send it back |
POST /api/orders returns no token: it is the last step.
sequenceDiagram
participant C as Client (script)
participant S as PerfShop
C->>S: POST /api/auth/login
S-->>C: 200, tokens of the active level (X-Step-Token is step2)
C->>S: POST /api/checkout/address, tokens received
S-->>C: 200, tokens (levels 3 and 4: new values, step3)
C->>S: POST /api/checkout/shipping, tokens received
S-->>C: 200, tokens (step4)
C->>S: POST /api/checkout/payment, tokens received
S-->>C: 200, tokens (step5)
C->>S: POST /api/orders, tokens received
S-->>C: 201, no token
Level by level¶
Level 1 — Junior¶
Skill practised: extracting a value from a response header and sending it back.
The script reads X-Session-Token in the headers of the login response and sends it on the four steps, together
with an X-Request-ID.
Level 2 — Intermediate¶
Skill practised: two values to track, specific to each virtual user.
Login returns one more token, X-Action-Token, to be sent on the four steps as well. As soon as the script simulates
several users, each one has its own tokens: they are stored in variables specific to each virtual user. Another
user's token is rejected.
Level 3 — Expert¶
Skill practised: re-extracting several values after every response.
Three headers are added: X-CSRF-Token, X-Step-Token and X-Signature. They change after every successful
step: the response of a step carries the values to send on the next one. The script extracts them from every
response, not only from login.
The server computes the signature and returns it with the other two tokens: the client sends back the value it received.
Level 4 — Maestro¶
Skill practised: the same as level 3.
The headers to send are those of level 3. The signing key is no longer shared by all sessions: each session has its
own. The login response carries one more header, X-Key-Hint: the first 8 characters of that key. The client does
not send it back. A wrong signature is rejected with E-HMAC-03 instead of E-SIG-07.
How the signature is built
How the signature is built, and the exercise key, are not documented here: this page is also read by students,
and working them out is the subject of an investigation lab. For the trainer, the source of the service is the
reference (class ChaosScriptingService).
Rejections¶
On the address, shipping and payment steps, the server first checks the login to the shop (401 without a session), then the request body (400 or 422 when it is incomplete or invalid), and only then the Scripting Chaos headers. A Scripting Chaos rejection always has the same shape:
| HTTP | error |
message |
Cause |
|---|---|---|---|
| 400 | 400 |
Missing header: X-Session-Token (or X-Request-ID, X-Action-Token) |
missing header |
| 401 | 401 |
Invalid session |
X-Session-Token is not the one of the session |
| 401 | E-TKN-99 |
No active token bundle - please login again |
the session has no tokens: log in again |
| 422 | 422 |
Correlation failed: X-Action-Token mismatch |
X-Action-Token is not the one of the session |
| 403 | E-CSRF-01 |
Missing header: X-CSRF-Token or CSRF token invalid |
CSRF token missing, or not the last one returned |
| 409 | E-STEP-04 |
Missing header: X-Step-Token or Step token mismatch - expected step2 |
step missing, or not the expected step (the message names it) |
| 401 | E-SIG-07 |
Missing header: X-Signature or Signature invalid |
signature missing (levels 3 and 4) or wrong (level 3) |
| 401 | E-HMAC-03 |
Signature invalid |
wrong signature (level 4) |
The E-… codes are specific to PerfShop.
Setting the level¶
The level applies to the whole stack: every user of the shop is subject to it.
Tokens are issued at login, for the level active at that moment. After a level change the script logs in again:
a session opened at level 0 has no tokens (E-TKN-99), a session opened at level 1 did not receive those of level 3.
By the student¶
From the Chaos Admin student page (Chaos Scripting tab), or through the API:
curl -X POST http://localhost:9080/api/chaos/student/scripting \
-H "Content-Type: application/json" \
-d '{"level": 1}'
- Student mode must be on (403 otherwise).
- Without a licence, level 1 is the highest; above it the response is
402with"error": "LICENSE_REQUIRED". - The state returns
scripting.level(current level) andscripting.maxLevel(highest level allowed).
Student routes in detail: API — Student page (self-service).
By the trainer¶
From Chaos Admin (trainer page), Chaos Scripting tab, or through the administration API. These routes
require the trainer: their session, or the X-Admin-Token header (see
Authentication); they answer 401 otherwise.
| Route | Effect |
|---|---|
POST /api/admin/chaos/scripting with {"level": 0-4} |
sets the level |
GET /api/admin/chaos/scripting/status |
returns level, levelLabel and activeBundles |
POST /api/admin/chaos/scripting/reset |
level 0, and all tokens held in memory are removed |
GET /api/admin/chaos/scripting/logs |
returns the log |
DELETE /api/admin/chaos/scripting/logs |
empties the log |
curl -X POST http://localhost:9080/api/admin/chaos/scripting \
-H "X-Admin-Token: <token>" \
-H "Content-Type: application/json" \
-d '{"level": 3}'
Two read routes feed the trainer's monitoring. They require the trainer as well; "public" is a historical name:
GET /api/chaos/public/scripting/statusGET /api/chaos/public/scripting/logs
Trainer log¶
The log keeps the last 100 events, most recent first. Each entry carries ts, type, session (first 8 characters
of the session identifier), email, token, detail and ok.
No token is written in full: only a fingerprint (its first 8 characters and its length). The log lists the sessions of all students: it is reserved for the trainer. Students read what concerns them in the rejections of their own session.
Prometheus metrics¶
| Metric | Type | Description |
|---|---|---|
chaos_scripting_level |
gauge | current level (0 to 4) |
chaos_scripting_bundles_active |
gauge | number of sessions holding tokens in memory |
The tokens of a session are created at login, from level 1. They are removed at logout, or by the trainer's reset.
Under a run whose users do not log out, chaos_scripting_bundles_active therefore only goes up.
In a load script¶
| Level | What the script must be able to do |
|---|---|
| Junior | extract a response header and send it back |
| Intermediate | track two values per virtual user, in variables of its own |
| Expert | re-extract three values after every response and send them on the next step |
| Maestro | the same; the X-Key-Hint hint is used by the investigation lab |
In JMeter, the technique is described step by step in the guide Creating a JMeter script compatible with PerfShop, step 9: extracting a response header, sending it back, and choosing a thread variable rather than a shared property.
Independently of Scripting Chaos, and at every level, POST /api/orders expects in its body the securityToken
returned by login. Without it the response is 403, before any check of the headers: see
API — Orders and checkout.
In the code¶
| Class | Role |
|---|---|
ChaosScriptingService |
tokens, checks, log |
ChaosScriptingController |
administration routes |
ChaosScriptingPublicController |
reading of the state and the log by the trainer |
ChaosStudentController |
setting by the student |