From 55c64ea16b8b9850bb1d85aa8313d1c186f559e3 Mon Sep 17 00:00:00 2001 From: Teodora Sandu Date: Wed, 27 Mar 2024 18:16:22 +0000 Subject: [PATCH] feat: add cwe interaction --- .../SuggestionDescriptionPanelFromLS.kt | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/SuggestionDescriptionPanelFromLS.kt b/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/SuggestionDescriptionPanelFromLS.kt index 9a49267b1..fb0036751 100644 --- a/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/SuggestionDescriptionPanelFromLS.kt +++ b/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/SuggestionDescriptionPanelFromLS.kt @@ -1,6 +1,7 @@ package io.snyk.plugin.ui.toolwindow.panels import com.intellij.icons.AllIcons +import com.intellij.ide.BrowserUtil import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.util.Iconable import com.intellij.ui.HyperlinkLabel @@ -24,6 +25,7 @@ import io.snyk.plugin.ui.SnykBalloonNotificationHelper import io.snyk.plugin.ui.baseGridConstraintsAnchorWest import io.snyk.plugin.ui.descriptionHeaderPanel import io.snyk.plugin.ui.panelGridConstraints +import io.snyk.plugin.ui.toolwindow.LabelProvider import io.snyk.plugin.ui.toolwindow.SnykToolWindowPanel import io.snyk.plugin.ui.wrapWithScrollPane import org.cef.browser.CefBrowser @@ -36,6 +38,7 @@ import java.awt.Color import java.awt.Dimension import java.awt.Font import java.awt.Insets +import java.net.URL import javax.swing.JComponent import javax.swing.JLabel import javax.swing.JPanel @@ -76,6 +79,12 @@ class SuggestionDescriptionPanelFromLS( return JBCefJSQuery.Response("success") } + + fun openCWELink(cweLink: String): JBCefJSQuery.Response { + BrowserUtil.open(URL(cweLink).toExternalForm()) + return JBCefJSQuery.Response("success") + } + init { if (pluginSettings().isGlobalIgnoresFeatureEnabled && issue.additionalData.details != null) { if (!JBCefApp.isSupported()) { @@ -87,12 +96,14 @@ class SuggestionDescriptionPanelFromLS( val jbCefBrowser = JBCefBrowserBuilder().setClient(cefClient).setEnableOpenDevToolsMenuItem(true).build() val openFileQuery = JBCefJSQuery.create(jbCefBrowser) + val openCWELinkQuery = JBCefJSQuery.create(jbCefBrowser) val loadHandler = object : CefLoadHandlerAdapter() { override fun onLoadEnd(browser: CefBrowser, frame: CefFrame, httpStatusCode: Int) { if (frame.isMain) { cefClient.setProperty("JS_QUERY_POOL_SIZE", 1) openFileQuery.addHandler { openFile(it) } + openCWELinkQuery.addHandler { openCWELink(it) } browser.executeJavaScript( "window.openFileQuery = function(filePath) {" + @@ -101,10 +112,15 @@ class SuggestionDescriptionPanelFromLS( browser.url, 0 ); + browser.executeJavaScript( + "window.openCWELinkQuery = function(cweLink) {" + + openCWELinkQuery.inject("cweLink") + + "};", + browser.url, 0 + ); browser.executeJavaScript( """ const dataFlowFilePaths = document.getElementsByClassName('data-flow-filepath') - console.log(dataFlowFilePaths) for (const filePath of dataFlowFilePaths) { filePath.addEventListener('click', function (e) { e.preventDefault() @@ -112,7 +128,11 @@ class SuggestionDescriptionPanelFromLS( console.log(e) window.openFileQuery(filePath.innerText); }); - }""", + } + document.getElementById('meta').addEventListener('click', function (e) { + e.preventDefault() + window.openCWELinkQuery("https://cwe.mitre.org/data/definitions/547.html"); + });""", browser.url, 0 ); }