Skip to content

Commit 5928b42

Browse files
committed
Switch to HTTP Sender script for MCP header injection
Replaces the Replacer add-on with a custom HTTP Sender script to inject the Mcp-Session-Id header into all requests in ZAP. Updates the automation framework configuration to set the session ID as a global variable and load, run, and enable the new script for authenticated scanning.
1 parent dec78ec commit 5928b42

2 files changed

Lines changed: 59 additions & 10 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ZAP HTTP Sender Script - Add MCP Session ID Header
3+
*
4+
* This script intercepts all outgoing HTTP requests from ZAP
5+
* and adds the Mcp-Session-Id header to enable authenticated scanning.
6+
*
7+
* The session ID is stored as a global variable by the automation framework.
8+
*/
9+
10+
function sendingRequest(msg, initiator, helper) {
11+
// Get the MCP session ID from global variable
12+
var sessionId = org.zaproxy.zap.extension.script.ScriptVars.getGlobalVar('mcpSessionId');
13+
14+
if (sessionId != null && sessionId != '') {
15+
// Add the Mcp-Session-Id header to the request
16+
msg.getRequestHeader().setHeader('Mcp-Session-Id', sessionId);
17+
18+
// Also ensure we have the Accept header for MCP endpoints
19+
var uri = msg.getRequestHeader().getURI().toString();
20+
if (uri.indexOf('/mcp') !== -1) {
21+
msg.getRequestHeader().setHeader('Accept', 'application/json, text/event-stream');
22+
msg.getRequestHeader().setHeader('Content-Type', 'application/json');
23+
}
24+
}
25+
}
26+
27+
function responseReceived(msg, initiator, helper) {
28+
// No action needed on response
29+
}
Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
# ZAP Automation Framework Configuration for MCP Protocol
3-
# Uses the Replacer add-on to inject Mcp-Session-Id header into all requests
3+
# Uses HTTP Sender script to inject Mcp-Session-Id header into all requests
44

55
env:
66
contexts:
@@ -17,13 +17,33 @@ env:
1717
progressToStdout: true
1818

1919
jobs:
20-
# Add the Mcp-Session-Id header to all requests using Replacer
21-
- type: replacer
20+
# Store the MCP session ID as a global variable for the HTTP Sender script
21+
- type: script
2222
parameters:
23-
rules:
24-
- description: "Add MCP Session ID header"
25-
enabled: true
26-
matchType: REQ_HEADER
27-
matchString: "Mcp-Session-Id"
28-
replacement: "Mcp-Session-Id: MCP_SESSION_ID_PLACEHOLDER"
29-
initiators: []
23+
action: add
24+
type: standalone
25+
engine: "ECMAScript : Graal.js"
26+
name: set-mcp-session-id
27+
inline: |
28+
org.zaproxy.zap.extension.script.ScriptVars.setGlobalVar('mcpSessionId', 'MCP_SESSION_ID_PLACEHOLDER')
29+
30+
- type: script
31+
parameters:
32+
action: run
33+
type: standalone
34+
name: set-mcp-session-id
35+
36+
# Load and enable the HTTP Sender script to add headers to all requests
37+
- type: script
38+
parameters:
39+
action: add
40+
type: httpsender
41+
engine: "Oracle Nashorn"
42+
name: add-mcp-session-header
43+
file: /opt/rapidast/work/scripts/zap/add-mcp-session-header.js
44+
45+
- type: script
46+
parameters:
47+
action: enable
48+
type: httpsender
49+
name: add-mcp-session-header

0 commit comments

Comments
 (0)