|
| 1 | +/* |
| 2 | + * Zed Attack Proxy (ZAP) and its related class files. |
| 3 | + * |
| 4 | + * ZAP is an HTTP/HTTPS proxy for assessing web application security. |
| 5 | + * |
| 6 | + * Copyright 2026 The ZAP Development Team |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +package org.zaproxy.addon.commonlib.ui; |
| 21 | + |
| 22 | +import java.awt.Toolkit; |
| 23 | +import java.awt.datatransfer.StringSelection; |
| 24 | +import javax.swing.JCheckBox; |
| 25 | +import javax.swing.JOptionPane; |
| 26 | +import org.apache.commons.lang3.StringUtils; |
| 27 | +import org.parosproxy.paros.Constant; |
| 28 | +import org.parosproxy.paros.core.scanner.Alert; |
| 29 | +import org.parosproxy.paros.view.View; |
| 30 | +import org.zaproxy.addon.commonlib.CommonAlertTag; |
| 31 | +import org.zaproxy.addon.commonlib.CommonlibParam; |
| 32 | +import org.zaproxy.zap.extension.alert.PopupMenuItemAlert; |
| 33 | + |
| 34 | +/** |
| 35 | + * A right-click menu item for alerts that generates a prompt the user can paste into an LLM to ask |
| 36 | + * it to fix the vulnerability. The prompt is copied to the system clipboard. |
| 37 | + * |
| 38 | + * <p>The prompt text is intentionally written in English rather than translated, as LLMs generally |
| 39 | + * perform better with English input. |
| 40 | + */ |
| 41 | +@SuppressWarnings("serial") |
| 42 | +public class GenerateFixPromptMenu extends PopupMenuItemAlert { |
| 43 | + |
| 44 | + private static final long serialVersionUID = 1L; |
| 45 | + |
| 46 | + // Prompt text is not i18n - LLMs handle English better than other languages. |
| 47 | + private static final String INTRO = |
| 48 | + "A security scanner (ZAP by Checkmarx) has found a vulnerability in the application you are" |
| 49 | + + " working on. Please identify where in the codebase this vulnerability exists" |
| 50 | + + " and provide a fix."; |
| 51 | + private static final String SYSTEMIC_NOTE = |
| 52 | + "Note: This vulnerability appears to occur across the application - there may be" |
| 53 | + + " multiple instances that all need to be fixed."; |
| 54 | + private static final String CLOSING = |
| 55 | + "Please identify where in the codebase this vulnerability exists and provide a fix."; |
| 56 | + |
| 57 | + private final CommonlibParam param; |
| 58 | + |
| 59 | + public GenerateFixPromptMenu(CommonlibParam param) { |
| 60 | + super(Constant.messages.getString("commonlib.alert.generatefixprompt.menu"), false); |
| 61 | + this.param = param; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void performAction(Alert alert) { |
| 66 | + String prompt = buildPrompt(alert); |
| 67 | + Toolkit.getDefaultToolkit() |
| 68 | + .getSystemClipboard() |
| 69 | + .setContents(new StringSelection(prompt), null); |
| 70 | + |
| 71 | + if (param.isShowFixPromptCopiedDialog()) { |
| 72 | + JCheckBox doNotShowAgain = |
| 73 | + new JCheckBox( |
| 74 | + Constant.messages.getString( |
| 75 | + "commonlib.alert.generatefixprompt.donotshowagain")); |
| 76 | + JOptionPane.showMessageDialog( |
| 77 | + View.getSingleton().getMainFrame(), |
| 78 | + new Object[] { |
| 79 | + Constant.messages.getString("commonlib.alert.generatefixprompt.copied"), |
| 80 | + " ", |
| 81 | + doNotShowAgain |
| 82 | + }, |
| 83 | + Constant.messages.getString("commonlib.alert.generatefixprompt.title"), |
| 84 | + JOptionPane.INFORMATION_MESSAGE); |
| 85 | + param.setShowFixPromptCopiedDialog(!doNotShowAgain.isSelected()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + static String buildPrompt(Alert alert) { |
| 90 | + StringBuilder sb = new StringBuilder(); |
| 91 | + |
| 92 | + sb.append(INTRO); |
| 93 | + sb.append("\n\n"); |
| 94 | + |
| 95 | + boolean systemic = |
| 96 | + alert.getTags() != null |
| 97 | + && alert.getTags().containsKey(CommonAlertTag.SYSTEMIC.getTag()); |
| 98 | + if (systemic) { |
| 99 | + sb.append(SYSTEMIC_NOTE); |
| 100 | + sb.append("\n\n"); |
| 101 | + } |
| 102 | + |
| 103 | + sb.append("## Vulnerability\n\n"); |
| 104 | + |
| 105 | + appendField(sb, "Name", alert.getName()); |
| 106 | + |
| 107 | + int risk = alert.getRisk(); |
| 108 | + if (risk >= 0 && risk < Alert.MSG_RISK.length) { |
| 109 | + appendField(sb, "Risk", Alert.MSG_RISK[risk]); |
| 110 | + } |
| 111 | + |
| 112 | + int confidence = alert.getConfidence(); |
| 113 | + if (confidence >= 0 && confidence < Alert.MSG_CONFIDENCE.length) { |
| 114 | + appendField(sb, "Confidence", Alert.MSG_CONFIDENCE[confidence]); |
| 115 | + } |
| 116 | + |
| 117 | + appendField(sb, "URL", alert.getUri()); |
| 118 | + appendField(sb, "Method", alert.getMethod()); |
| 119 | + appendField(sb, "Parameter", alert.getParam()); |
| 120 | + appendField(sb, "Attack", alert.getAttack()); |
| 121 | + appendField(sb, "Evidence", alert.getEvidence()); |
| 122 | + |
| 123 | + appendSection(sb, "Description", alert.getDescription()); |
| 124 | + appendSection(sb, "Solution", alert.getSolution()); |
| 125 | + appendSection(sb, "References", alert.getReference()); |
| 126 | + appendSection(sb, "Other Information", alert.getOtherInfo()); |
| 127 | + |
| 128 | + sb.append("\n").append(CLOSING); |
| 129 | + |
| 130 | + return sb.toString(); |
| 131 | + } |
| 132 | + |
| 133 | + private static void appendField(StringBuilder sb, String label, String value) { |
| 134 | + if (StringUtils.isNotBlank(value)) { |
| 135 | + sb.append("**").append(label).append("**: ").append(value).append("\n"); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static void appendSection(StringBuilder sb, String heading, String value) { |
| 140 | + if (StringUtils.isNotBlank(value)) { |
| 141 | + sb.append("\n## ").append(heading).append("\n\n"); |
| 142 | + sb.append(value).append("\n"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public boolean isSafe() { |
| 148 | + return true; |
| 149 | + } |
| 150 | +} |
0 commit comments