-
-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: kingthorin <kingthorin@users.noreply.github.com>
- Loading branch information
1 parent
d424fbf
commit 1b4cc93
Showing
6 changed files
with
502 additions
and
12 deletions.
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
164 changes: 164 additions & 0 deletions
164
addOns/client/src/main/java/org/zaproxy/addon/client/internal/ClientMapWriter.java
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,164 @@ | ||
/* | ||
* Zed Attack Proxy (ZAP) and its related class files. | ||
* | ||
* ZAP is an HTTP/HTTPS proxy for assessing web application security. | ||
* | ||
* Copyright 2025 The ZAP Development Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.zaproxy.addon.client.internal; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.Writer; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.parosproxy.paros.control.Control; | ||
import org.zaproxy.addon.client.ExtensionClientIntegration; | ||
import org.zaproxy.addon.client.ui.PopupMenuExportClientMap; | ||
import org.zaproxy.zap.utils.Stats; | ||
|
||
public final class ClientMapWriter { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(ClientMapWriter.class); | ||
|
||
private static final String NODE_KEY = "node"; | ||
private static final String ROOT_NODE_NAME = "ClientMap"; | ||
private static final String CHILDREN_KEY = "children"; | ||
private static final String NAME_KEY = "name"; | ||
private static final String STORAGE_KEY = "isStorage"; | ||
private static final String VISITED_KEY = "visited"; | ||
|
||
private static ExtensionClientIntegration extension; | ||
|
||
ClientMapWriter() {} | ||
|
||
public static void exportClientMap(File file) throws IOException { | ||
try (FileWriter fw = new FileWriter(file, false)) { | ||
extension = | ||
Control.getSingleton() | ||
.getExtensionLoader() | ||
.getExtension(ExtensionClientIntegration.class); | ||
exportClientMap(fw); | ||
} | ||
} | ||
|
||
public static void exportClientMap(Writer fw) throws IOException { | ||
exportClientMap(fw, extension.getClientTree()); | ||
} | ||
|
||
public static void exportClientMap(Writer fw, ClientMap clientMap) throws IOException { | ||
try (BufferedWriter bw = new BufferedWriter(fw)) { | ||
outputNode(bw, clientMap.getRoot(), 0); | ||
} | ||
} | ||
|
||
private static boolean outputKV( | ||
Writer fw, String indent, boolean first, String key, Object value) throws IOException { | ||
fw.write(indent); | ||
if (first) { | ||
fw.write("- "); | ||
} else { | ||
fw.write(" "); | ||
} | ||
fw.write(key); | ||
fw.write(": "); | ||
ObjectMapper mapper = | ||
new ObjectMapper( | ||
new YAMLFactory() | ||
.enable(Feature.LITERAL_BLOCK_STYLE) | ||
.disable(Feature.WRITE_DOC_START_MARKER)); | ||
// For some reason the disable start marker doesn't seem to work | ||
String output = mapper.writeValueAsString(value).replace("--- ", ""); | ||
fw.write(output); | ||
return false; | ||
} | ||
|
||
private static void outputNode(Writer fw, ClientNode node, int level) throws IOException { | ||
if (node.isStorage()) { | ||
// Skip storage nodes in the tree | ||
// Those details are represented as components of their source | ||
return; | ||
} | ||
// We could create a set of data structures and use jackson, but the format is very | ||
// simple and this is much more memory efficient - it still uses jackson for value | ||
// output | ||
String indent = " ".repeat(level * 2); | ||
|
||
outputKV(fw, indent, true, NODE_KEY, level == 0 ? ROOT_NODE_NAME : node.toString()); | ||
|
||
outputKV(fw, indent, false, NAME_KEY, node.getUserObject().getName()); | ||
outputKV(fw, indent, false, STORAGE_KEY, node.getUserObject().isStorage()); | ||
outputKV(fw, indent, false, VISITED_KEY, node.getUserObject().isVisited()); | ||
if (node.getUserObject().getComponents() != null | ||
&& !node.getUserObject().getComponents().isEmpty()) { | ||
indent = outputComponents(fw, node.getUserObject().getComponents(), level, indent); | ||
} | ||
|
||
Stats.incCounter(PopupMenuExportClientMap.STATS_EXPORT_CLIENTMAP + ".node"); | ||
|
||
if (node.getChildCount() > 0) { | ||
fw.write(indent); | ||
fw.write(" "); | ||
fw.write(CHILDREN_KEY); | ||
fw.write(":"); | ||
fw.write('\n'); | ||
node.children() | ||
.asIterator() | ||
.forEachRemaining( | ||
c -> { | ||
try { | ||
outputNode(fw, (ClientNode) c, level + 1); | ||
} catch (IOException e) { | ||
LOGGER.error(e.getMessage(), e); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private static String outputComponents( | ||
Writer fw, Set<ClientSideComponent> components, int level, String indent) | ||
throws IOException { | ||
fw.write(indent + " "); | ||
fw.write("components:\n"); | ||
indent = " ".repeat((level + 1) * 2); | ||
boolean first = true; | ||
|
||
LinkedHashSet<ClientSideComponent> sortedComponents = | ||
components.stream().sorted().collect(Collectors.toCollection(LinkedHashSet::new)); | ||
for (ClientSideComponent component : sortedComponents) { | ||
first = outputKV(fw, indent, first, "typeForDisplay", component.getTypeForDisplay()); | ||
first = outputKV(fw, indent, first, "href", component.getHref()); | ||
if (!component.isStorageEvent()) { | ||
first = outputKV(fw, indent, first, "text", component.getText()); | ||
} | ||
first = outputKV(fw, indent, first, "id", component.getId()); | ||
first = outputKV(fw, indent, first, "tagName", component.getTagName()); | ||
first = outputKV(fw, indent, first, "tagType", component.getTagType()); | ||
first = outputKV(fw, indent, first, "formId", component.getFormId()); | ||
first = outputKV(fw, indent, first, "type", component.getType()); | ||
outputKV(fw, indent, first, "isStorageEvent", component.isStorageEvent()); | ||
first = true; | ||
} | ||
return indent; | ||
} | ||
} |
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
Oops, something went wrong.