Skip to content

Commit

Permalink
feat: add cwe interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
teodora-sandu committed Mar 27, 2024
1 parent 571451c commit 55c64ea
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand All @@ -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) {" +
Expand All @@ -101,18 +112,27 @@ 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()
console.log(filePath)
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
);
}
Expand Down

0 comments on commit 55c64ea

Please sign in to comment.