-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export the ZAP site tree as a JSON file (#229)
Export the ZAP site tree as a JSON file See: #229
- Loading branch information
Showing
4 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Script to traverse the site tree and export node information to a JSON file | ||
* | ||
* This script retrieves the root of the site tree from the current ZAP session, | ||
* traverses each child node, and collects relevant information such as node name, | ||
* HTTP method, and status code. The collected data is then written to a JSON file | ||
* named 'zap-site-tree.json' in the session's results directory | ||
*/ | ||
|
||
var File = Java.type('java.io.File'); | ||
var FileWriter = Java.type('java.io.FileWriter'); | ||
var BufferedWriter = Java.type('java.io.BufferedWriter'); | ||
|
||
const defaultFileName = "zap-site-tree.json"; | ||
|
||
try { | ||
var fileName = org.zaproxy.zap.extension.script.ScriptVars.getGlobalVar('siteTreeFileName') || defaultFileName; | ||
|
||
} catch (e) { | ||
var fileName = defaultFileName; | ||
print("Error retrieving 'siteTreeFileName': " + e.message + ". Using default value: '" + defaultFileName); | ||
} | ||
|
||
function listChildren(node, resultList) { | ||
for (var j = 0; j < node.getChildCount(); j++) { | ||
listChildren(node.getChildAt(j), resultList); | ||
} | ||
|
||
if (node.getChildCount() == 0) { | ||
var href = node.getHistoryReference(); | ||
var nodeInfo = {}; | ||
nodeInfo["name"] = node.getHierarchicNodeName(); | ||
|
||
if (href != null) { | ||
nodeInfo["method"] = href.getMethod(); | ||
nodeInfo["status"] = href.getStatusCode(); | ||
} else { | ||
nodeInfo["method"] = "No History Reference"; | ||
nodeInfo["status"] = "No History Reference"; | ||
} | ||
|
||
resultList.push(nodeInfo); | ||
} | ||
} | ||
|
||
try { | ||
var root = model.getSession().getSiteTree().getRoot(); | ||
var resultList = []; | ||
|
||
listChildren(root, resultList); | ||
|
||
var jsonOutput = JSON.stringify(resultList, null, 4); | ||
|
||
var defaultResultsDir = model.getSession().getSessionFolder(); | ||
var outputFilePath = new File(defaultResultsDir, fileName).getAbsolutePath(); | ||
|
||
var file = new File(outputFilePath); | ||
var writer = new BufferedWriter(new FileWriter(file)); | ||
writer.write(jsonOutput); | ||
writer.close(); | ||
|
||
print("Site tree data has been written to: " + outputFilePath); | ||
|
||
} catch (e) { | ||
print("An error occurred: " + e); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
import configmodel | ||
from scanners.zap.zap_none import ZapNone | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def test_config(): | ||
return configmodel.RapidastConfigModel({"application": {"url": "http://example.com"}}) | ||
|
||
|
||
@patch("os.path.exists") | ||
@patch("scanners.zap.zap.shutil.copy") | ||
@patch("scanners.zap.zap.shutil.copytree") | ||
@patch("scanners.zap.zap.tarfile") | ||
def test_zap_none_postprocess_copy_site_tree_path(mock_tarfile, mock_copytree, mock_copy, mock_exists, test_config): | ||
mock_exists.return_value = True | ||
|
||
test_zap = ZapNone(config=test_config) | ||
with patch.object(test_zap, "_copy_site_tree") as mock_copy_site_tree: | ||
test_zap.postprocess() | ||
mock_copy_site_tree.assert_called_once() | ||
|
||
|
||
@patch("os.path.exists") | ||
@patch("shutil.copy") | ||
def test_copy_site_tree_success(mock_copy, mock_exists, test_config): | ||
mock_exists.return_value = True | ||
test_zap = ZapNone(config=test_config) | ||
test_zap._copy_site_tree() | ||
|
||
mock_copy.assert_called_once_with( | ||
os.path.join(test_zap.host_work_dir, f"session_data/{ZapNone.SITE_TREE_FILENAME}"), test_zap.results_dir | ||
) | ||
|
||
|
||
@patch("os.path.exists") | ||
@patch("shutil.copy") | ||
def test_copy_site_tree_file_not_found(mock_copy, mock_exists, test_config): | ||
mock_exists.return_value = False | ||
test_zap = ZapNone(config=test_config) | ||
test_zap._copy_site_tree() | ||
|
||
assert not mock_copy.called |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters