|
| 1 | +// This HttpSender script adds headers to all messages transmitted by zaproxy, |
| 2 | +// including automated tools. Refer to the HttpSender class definition: |
| 3 | +// https://github.com/zaproxy/zaproxy/blob/main/zap/src/main/java/org/parosproxy/paros/network/HttpSender.java |
| 4 | +// for a list of 'initiator' values (although we don't use them). |
| 5 | + |
| 6 | +var ScriptVars = Java.type("org.zaproxy.zap.extension.script.ScriptVars"); |
| 7 | + |
| 8 | +/* |
| 9 | + * HttpSender scripts do not support parameters, so we'll use a known global |
| 10 | + * variable to supply desired content. The value of this variable should be a |
| 11 | + * JSON string containing a serialized map<String, String> object. The map keys |
| 12 | + * are the desired header name and the values are the header values. |
| 13 | + * |
| 14 | + * Example: |
| 15 | + * add_headers defined with value '{"x-this": "v1", "x-that": "v2"}' will |
| 16 | + * result in the following headers being added to every request: |
| 17 | + * x-this: v1 |
| 18 | + * x-that: v2 |
| 19 | + */ |
| 20 | + |
| 21 | +PARAMETER_VARIABLE = "add_headers"; |
| 22 | +user_headers = null; |
| 23 | + |
| 24 | +// Logging with the script name is super helpful! |
| 25 | +function logger() { |
| 26 | + print('[' + this['zap.script.name'] + '] ' + arguments[0]); |
| 27 | +} |
| 28 | + |
| 29 | +// Parse and store headers where we can get at them quickly |
| 30 | +function initializeHeaders(variableName) { |
| 31 | + logger("Initializing..."); |
| 32 | + user_headers = JSON.parse(ScriptVars.getGlobalVar(variableName)); |
| 33 | +} |
| 34 | + |
| 35 | +/* |
| 36 | + * Processes messages by adding user-specified headers (overwriting original |
| 37 | + * values if header already exists). This may be pointless for some initiators |
| 38 | + * (CHECK_FOR_UPDATES) and redundant for others (FUZZER). |
| 39 | + * |
| 40 | + * Called before forwarding the message to the server. |
| 41 | + * |
| 42 | + * @param {HttpMessage} msg - The message that will be forwarded to the server. |
| 43 | + * @param {int} initiator - The initiator that generated the message. |
| 44 | + * @param {HttpSenderScriptHelper} helper - A utility object with helper functions. |
| 45 | + */ |
| 46 | +function sendingRequest(msg, initiator, helper) { |
| 47 | + // Get user-supplied headers if we didn't already do it |
| 48 | + if (!user_headers) { |
| 49 | + initializeHeaders(PARAMETER_VARIABLE); |
| 50 | + } |
| 51 | + |
| 52 | + // Ensure each header is present with the required value |
| 53 | + for (var key in user_headers) { |
| 54 | + var value = user_headers[key]; |
| 55 | + // logger("Setting " + key + " to " + value); |
| 56 | + msg.getRequestHeader().setHeader(key, value); |
| 57 | + } |
| 58 | + |
| 59 | + return msg; |
| 60 | +} |
| 61 | + |
| 62 | +/* Called after receiving the response from the server. |
| 63 | + * |
| 64 | + * @param {HttpMessage} msg - The message that was forwarded to the server. |
| 65 | + * @param {int} initiator - The initiator that generated the message. |
| 66 | + * @param {HttpSenderScriptHelper} helper - A utility object with helper functions. |
| 67 | + */ |
| 68 | +function responseReceived(msg, initiator, helper) { |
| 69 | + // Nothing to do here |
| 70 | +} |
0 commit comments