Skip to content

Commit 48cff36

Browse files
authored
Merge pull request #24 from SpringKill-team/feature/register
fix: Fix sink finder sorting bug #5 #12
2 parents a7920a2 + 24b1ff3 commit 48cff36

4 files changed

Lines changed: 69 additions & 12 deletions

File tree

src/main/kotlin/org/skgroup/securityinspector/analysis/ast/ProjectIssue.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import org.skgroup.securityinspector.enums.SinkCallMode
1212
data class ProjectIssue(
1313
val file: VirtualFile,
1414
val line: Int,
15+
val sinkClass: String,
16+
val sinkMethod: String,
1517
val type: String,
1618
val subType: String,
1719
val callMode: SinkCallMode

src/main/kotlin/org/skgroup/securityinspector/ui/IssueViewWindow.kt

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.intellij.openapi.project.Project
99
import com.intellij.openapi.vfs.VirtualFile
1010
import com.intellij.openapi.wm.ToolWindow
1111
import com.intellij.openapi.wm.ToolWindowFactory
12+
import com.intellij.ui.components.JBPanel
1213
import com.intellij.ui.components.JBScrollPane
1314
import com.intellij.ui.content.ContentFactory
1415
import com.intellij.ui.table.JBTable
@@ -19,8 +20,8 @@ import java.awt.BorderLayout
1920
import java.awt.event.MouseAdapter
2021
import java.awt.event.MouseEvent
2122
import javax.swing.JButton
22-
import javax.swing.JPanel
2323
import javax.swing.table.DefaultTableModel
24+
import javax.swing.table.TableRowSorter
2425

2526
/**
2627
* 类描述:IssueViewWindow 类用于创建SinkFinder。
@@ -31,15 +32,19 @@ import javax.swing.table.DefaultTableModel
3132
class IssueViewWindow : ToolWindowFactory {
3233

3334
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
34-
val panel = JPanel(BorderLayout())
35-
val tableModel = DefaultTableModel(arrayOf("File", "Line", "Type", "SubType", "CallMode"), 0)
35+
val panel = JBPanel<JBPanel<*>>(BorderLayout())
36+
val tableModel =
37+
DefaultTableModel(arrayOf("File", "Line", "SinkClass", "SinkMethod", "Type", "SubType", "CallMode"), 0)
3638
val refreshButton = JButton("Init Sink")
3739
val table = object : JBTable(tableModel) {
3840
override fun isCellEditable(row: Int, column: Int): Boolean {
3941
return false
4042
}
4143
}
4244

45+
val sorter = TableRowSorter(tableModel)
46+
table.rowSorter = sorter
47+
4348
table.apply {
4449
columnModel.getColumn(0).cellRenderer = FirstColumnRenderer()
4550
columnModel.getColumn(4).cellRenderer = HighlightRenderer()
@@ -49,9 +54,9 @@ class IssueViewWindow : ToolWindowFactory {
4954
if (e.clickCount == 2) {
5055
val selectedRow = table.selectedRow
5156
if (selectedRow != -1) {
52-
53-
val file = tableModel.getValueAt(selectedRow, 0) as VirtualFile
54-
val line = tableModel.getValueAt(selectedRow, 1).toString().toInt()
57+
val modelRow = table.rowSorter.convertRowIndexToModel(selectedRow)
58+
val file = tableModel.getValueAt(modelRow, 0) as VirtualFile
59+
val line = tableModel.getValueAt(modelRow, 1).toString().toInt()
5560

5661
OpenFileDescriptor(project, file, line, 0).navigate(true)
5762
}
@@ -69,8 +74,19 @@ class IssueViewWindow : ToolWindowFactory {
6974
ApplicationManager.getApplication().invokeLater {
7075
tableModel.rowCount = 0
7176
issues.forEach { issue ->
72-
tableModel.addRow(arrayOf(issue.file, issue.line.toString(), issue.type, issue.subType, issue.callMode))
77+
tableModel.addRow(
78+
arrayOf(
79+
issue.file,
80+
issue.line.toString(),
81+
issue.sinkClass,
82+
issue.sinkMethod,
83+
issue.type,
84+
issue.subType,
85+
issue.callMode
86+
)
87+
)
7388
}
89+
sorter.sort()
7490
}
7591
})
7692
}

src/main/kotlin/org/skgroup/securityinspector/ui/component/CallGraphUIComponents.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class CallGraphUIComponents(val project: Project) {
278278
}
279279

280280
// 组合底部信息面板
281-
return JPanel(BorderLayout()).apply {
281+
return JBPanel<JBPanel<*>>(BorderLayout()).apply {
282282
add(verticalSplit, BorderLayout.CENTER)
283283
add(infoPanel, BorderLayout.SOUTH)
284284

@@ -290,7 +290,7 @@ class CallGraphUIComponents(val project: Project) {
290290
}
291291
}
292292

293-
private fun createTitledPanel(title: String, component: JComponent): JPanel {
293+
private fun createTitledPanel(title: String, component: JComponent): JBPanel<JBPanel<*>> {
294294
return JBPanel<JBPanel<*>>(BorderLayout()).apply {
295295
border = BorderFactory.createTitledBorder(title)
296296
add(component, BorderLayout.CENTER)

src/main/kotlin/org/skgroup/securityinspector/utils/GraphUtils.kt

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ object GraphUtils {
375375
val methodName = call.methodExpression.referenceName ?: return
376376
val className = call.resolveMethod()?.containingClass?.qualifiedName ?: return
377377

378-
val sinkMatch = SinkList.ALL_SUB_VUL_DEFINITIONS.firstOrNull { sink ->
379-
sink.methodSinks[className]?.contains(methodName) == true
378+
val sinkMatch = SinkList.ALL_SUB_VUL_DEFINITIONS.firstOrNull { callSink ->
379+
callSink.methodSinks[className]?.contains(methodName) == true
380380
} ?: return
381381

382382
val document = PsiDocumentManager.getInstance(project).getDocument(psiFile)
@@ -396,6 +396,46 @@ object GraphUtils {
396396
ProjectIssue(
397397
virtualFile,
398398
line,
399+
className,
400+
methodName,
401+
sinkMatch.subType.parent.name,
402+
sinkMatch.subType.name,
403+
callMode
404+
)
405+
)
406+
}
407+
}
408+
409+
override fun visitNewExpression(new: PsiNewExpression) {
410+
if (!new.isValid || indicator.isCanceled) return
411+
412+
413+
val methodName = "<init>"
414+
val className = new.classReference?.qualifiedName ?: return
415+
416+
val sinkMatch = SinkList.ALL_SUB_VUL_DEFINITIONS.firstOrNull { conSink ->
417+
conSink.constructorSinks.contains(className)
418+
} ?: return
419+
420+
val document = PsiDocumentManager.getInstance(project).getDocument(psiFile)
421+
val line = document?.getLineNumber(new.textRange.startOffset)?.plus(1) ?: -1
422+
423+
var callMode = SinkCallMode.SINGLE_SINK
424+
val method = new.resolveMethod()
425+
val hasCall = method?.let {
426+
ReferencesSearch.search(it, ProjectScope.getProjectScope(project))
427+
.findFirst()
428+
} != null
429+
synchronized(issues) {
430+
if (hasCall) {
431+
callMode = SinkCallMode.HAS_CALL
432+
}
433+
issues.add(
434+
ProjectIssue(
435+
virtualFile,
436+
line,
437+
className,
438+
methodName,
399439
sinkMatch.subType.parent.name,
400440
sinkMatch.subType.name,
401441
callMode
@@ -412,5 +452,4 @@ object GraphUtils {
412452
}
413453
}
414454

415-
416455
}

0 commit comments

Comments
 (0)