Skip to content

Commit dec78ec

Browse files
committed
Automate MCP session initialization for ZAP
Adds a script to initialize MCP sessions and updates the GitHub workflow to inject the session ID into the ZAP automation framework. Refactors the ZAP automation config to use the Replacer add-on for header injection, removing custom authentication and session management scripts.
1 parent bb31ffa commit dec78ec

3 files changed

Lines changed: 125 additions & 35 deletions

File tree

.github/workflows/rapidast.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ jobs:
120120
mkdir -p results
121121
chmod 777 results
122122
123+
- name: Initialize MCP session for ZAP
124+
run: |
125+
echo "Initializing MCP session..."
126+
MCP_SESSION_ID=$(node scripts/init-mcp-session.cjs http://localhost:3000/mcp test-token)
127+
echo "MCP_SESSION_ID=$MCP_SESSION_ID" >> $GITHUB_ENV
128+
echo "Session ID: $MCP_SESSION_ID"
129+
130+
# Update the automation framework with the actual session ID
131+
sed -i "s/MCP_SESSION_ID_PLACEHOLDER/$MCP_SESSION_ID/" scripts/zap/automation-framework.yml
132+
133+
echo "Updated ZAP automation framework with session ID"
134+
123135
- name: Run RapiDAST scan
124136
id: rapidast
125137
continue-on-error: true

scripts/init-mcp-session.cjs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Initialize MCP Session for ZAP Security Scanning
5+
*
6+
* This script:
7+
* 1. Sends an MCP initialization request to the MCP server
8+
* 2. Extracts the Mcp-Session-Id from the response header
9+
* 3. Outputs the session ID to stdout for use in the workflow
10+
*
11+
* Usage:
12+
* node scripts/init-mcp-session.cjs [mcp-url] [bearer-token]
13+
*
14+
* Environment Variables:
15+
* MCP_URL - MCP server URL (default: http://localhost:3000/mcp)
16+
* BEARER_TOKEN - Bearer token for authentication (default: test-token)
17+
*/
18+
19+
const http = require('http');
20+
const https = require('https');
21+
const { URL } = require('url');
22+
23+
// Get configuration from args or environment
24+
const MCP_URL = process.argv[2] || process.env.MCP_URL || 'http://localhost:3000/mcp';
25+
const BEARER_TOKEN = process.argv[3] || process.env.BEARER_TOKEN || 'test-token';
26+
27+
// MCP initialization request payload
28+
const initRequest = JSON.stringify({
29+
jsonrpc: '2.0',
30+
id: 1,
31+
method: 'initialize',
32+
params: {
33+
protocolVersion: '2024-11-05',
34+
capabilities: {},
35+
clientInfo: {
36+
name: 'ZAP-Security-Scanner',
37+
version: '1.0.0'
38+
}
39+
}
40+
});
41+
42+
/**
43+
* Send MCP initialization request
44+
*/
45+
function initializeMcpSession() {
46+
const url = new URL(MCP_URL);
47+
const isHttps = url.protocol === 'https:';
48+
const client = isHttps ? https : http;
49+
50+
const options = {
51+
hostname: url.hostname,
52+
port: url.port || (isHttps ? 443 : 80),
53+
path: url.pathname + url.search,
54+
method: 'POST',
55+
headers: {
56+
'Content-Type': 'application/json',
57+
'Authorization': `Bearer ${BEARER_TOKEN}`,
58+
'Accept': 'application/json, text/event-stream',
59+
'Content-Length': Buffer.byteLength(initRequest)
60+
}
61+
};
62+
63+
console.error(`Initializing MCP session at ${MCP_URL}...`);
64+
65+
const req = client.request(options, (res) => {
66+
let data = '';
67+
68+
res.on('data', (chunk) => {
69+
data += chunk;
70+
});
71+
72+
res.on('end', () => {
73+
const sessionId = res.headers['mcp-session-id'];
74+
75+
if (res.statusCode === 200 && sessionId) {
76+
console.error(`✓ Successfully obtained MCP session ID: ${sessionId}`);
77+
console.error(`Response status: ${res.statusCode}`);
78+
79+
// Output just the session ID to stdout (for capture in workflow)
80+
console.log(sessionId);
81+
process.exit(0);
82+
} else {
83+
console.error(`✗ Failed to obtain MCP session ID`);
84+
console.error(`Response status: ${res.statusCode}`);
85+
console.error(`Response headers:`, res.headers);
86+
console.error(`Response body:`, data);
87+
process.exit(1);
88+
}
89+
});
90+
});
91+
92+
req.on('error', (error) => {
93+
console.error(`✗ Request failed:`, error.message);
94+
process.exit(1);
95+
});
96+
97+
// Send the request
98+
req.write(initRequest);
99+
req.end();
100+
}
101+
102+
// Run the initialization
103+
initializeMcpSession();
Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
# ZAP Automation Framework Configuration for MCP Protocol
3-
# This configures custom authentication and session management scripts
4-
# for the MCP (Model Context Protocol) used by AAP MCP Server
3+
# Uses the Replacer add-on to inject Mcp-Session-Id header into all requests
54

65
env:
76
contexts:
@@ -11,44 +10,20 @@ env:
1110
includePaths:
1211
- "http://localhost:3000/.*"
1312
excludePaths: []
14-
authentication:
15-
method: "script"
16-
parameters:
17-
script: "MCP Authentication"
18-
scriptEngine: "ECMAScript : Graal.js"
19-
Login URL: "http://localhost:3000/mcp"
20-
verification:
21-
method: "response"
22-
loggedInRegex: "\\Q{\"result\":\\E"
23-
loggedOutRegex: "\\Qerror\\E"
24-
sessionManagement:
25-
method: "script"
26-
parameters:
27-
script: "MCP Session Management"
28-
scriptEngine: "ECMAScript : Graal.js"
29-
users:
30-
- name: "test-user"
31-
credentials:
32-
token: "test-token"
3313

3414
parameters:
3515
failOnError: true
3616
failOnWarning: false
3717
progressToStdout: true
3818

3919
jobs:
40-
- type: script
20+
# Add the Mcp-Session-Id header to all requests using Replacer
21+
- type: replacer
4122
parameters:
42-
action: add
43-
name: "MCP Authentication"
44-
type: authentication
45-
engine: "ECMAScript : Graal.js"
46-
file: "/opt/rapidast/work/scripts/zap/mcp-authentication.js"
47-
48-
- type: script
49-
parameters:
50-
action: add
51-
name: "MCP Session Management"
52-
type: session
53-
engine: "ECMAScript : Graal.js"
54-
file: "/opt/rapidast/work/scripts/zap/mcp-session-management.js"
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: []

0 commit comments

Comments
 (0)