@@ -3,25 +3,16 @@ package org.skgroup.securityinspector.utils
33import com.intellij.ide.highlighter.JavaFileType
44import com.intellij.lang.java.JavaLanguage
55import com.intellij.openapi.application.ApplicationManager
6- import com.intellij.openapi.progress.EmptyProgressIndicator
7- import com.intellij.openapi.progress.ProgressIndicator
8- import com.intellij.openapi.progress.ProgressManager
9- import com.intellij.openapi.progress.Task
10- import com.intellij.openapi.project.DumbService
116import com.intellij.openapi.project.Project
127import com.intellij.openapi.vfs.LocalFileSystem
138import com.intellij.openapi.vfs.VirtualFile
149import com.intellij.psi.*
1510import com.intellij.psi.search.FileTypeIndex
1611import com.intellij.psi.search.GlobalSearchScope
17- import com.intellij.psi.search.ProjectScope
18- import com.intellij.psi.search.searches.ReferencesSearch
19- import org.skgroup.securityinspector.analysis.ast.ProjectIssue
2012import org.skgroup.securityinspector.analysis.ast.SourceSpan
2113import org.skgroup.securityinspector.analysis.ast.nodes.MethodNode
2214import org.skgroup.securityinspector.analysis.ast.nodes.ParameterNode
2315import org.skgroup.securityinspector.analysis.graphs.callgraph.CallGraph
24- import org.skgroup.securityinspector.enums.SinkCallMode
2516import java.nio.file.Files
2617import java.nio.file.Paths
2718
@@ -312,143 +303,4 @@ object GraphUtils {
312303 }
313304 }
314305
315- /* *
316- * Collect project issues 收集项目sink点,代替原始sink注册
317- *
318- * @param project
319- * @param chunkSize
320- * @param callback
321- * @receiver
322- */
323- fun collectProjectIssues (
324- project : Project ,
325- chunkSize : Int = 50,
326- callback : (List <ProjectIssue >) -> Unit ,
327- ) {
328- DumbService .getInstance(project).runWhenSmart {
329- ProgressManager .getInstance().runProcessWithProgressAsynchronously(
330- object : Task .Backgroundable (project, " Analyzing sink methods" , true ) {
331- private val issues = mutableListOf<ProjectIssue >()
332-
333- override fun run (indicator : ProgressIndicator ) {
334- ApplicationManager .getApplication().runReadAction {
335- val javaFiles = FileTypeIndex .getFiles(
336- JavaFileType .INSTANCE ,
337- GlobalSearchScope .projectScope(project)
338- ).asSequence()
339- javaFiles.chunked(chunkSize).forEachIndexed { index, chunk ->
340- if (indicator.isCanceled) return @runReadAction
341-
342- indicator.text = " Processing files ${index * chunkSize + 1 } ~${(index + 1 ) * chunkSize} "
343- indicator.fraction = index.toDouble() / (javaFiles.count() / chunkSize)
344-
345- processFileChunk(project, chunk, indicator)
346-
347- ApplicationManager .getApplication().invokeLater {
348- callback(issues.toList())
349- }
350-
351- }
352- }
353-
354- }
355-
356- private fun processFileChunk (
357- project : Project ,
358- files : List <VirtualFile >,
359- indicator : ProgressIndicator
360- ) {
361- ApplicationManager .getApplication().runReadAction {
362- val manager = PsiManager .getInstance(project)
363- files.forEach { virtualFile ->
364- if (indicator.isCanceled) return @runReadAction
365- if (virtualFile.path.contains(" src/test" )) return @forEach
366-
367- if (! virtualFile.isValid) return @forEach
368-
369- val psiFile = manager.findFile(virtualFile) as ? PsiJavaFile ? : return @forEach
370- psiFile.accept(object : JavaRecursiveElementWalkingVisitor () {
371- override fun visitMethodCallExpression (call : PsiMethodCallExpression ) {
372- if (! call.isValid || indicator.isCanceled) return
373-
374- val methodName = call.methodExpression.referenceName ? : return
375- val className = call.resolveMethod()?.containingClass?.qualifiedName ? : return
376-
377- val sinkMatch = SinkList .ALL_SUB_VUL_DEFINITIONS .firstOrNull { callSink ->
378- callSink.methodSinks[className]?.contains(methodName) == true
379- } ? : return
380-
381- val document = PsiDocumentManager .getInstance(project).getDocument(psiFile)
382- val line = document?.getLineNumber(call.textRange.startOffset)?.plus(1 ) ? : - 1
383-
384- var callMode = SinkCallMode .SINGLE_SINK
385- val method = call.resolveMethod()
386- val hasCall = method?.let {
387- ReferencesSearch .search(it, ProjectScope .getProjectScope(project))
388- .findFirst()
389- } != null
390- synchronized(issues) {
391- if (hasCall) {
392- callMode = SinkCallMode .HAS_CALL
393- }
394- issues.add(
395- ProjectIssue (
396- virtualFile,
397- line,
398- className,
399- methodName,
400- sinkMatch.subType.parent.name,
401- sinkMatch.subType.name,
402- callMode
403- )
404- )
405- }
406- }
407-
408- override fun visitNewExpression (new : PsiNewExpression ) {
409- if (! new.isValid || indicator.isCanceled) return
410-
411-
412- val methodName = " <init>"
413- val className = new.classReference?.qualifiedName ? : return
414-
415- val sinkMatch = SinkList .ALL_SUB_VUL_DEFINITIONS .firstOrNull { conSink ->
416- conSink.constructorSinks.contains(className)
417- } ? : return
418-
419- val document = PsiDocumentManager .getInstance(project).getDocument(psiFile)
420- val line = document?.getLineNumber(new.textRange.startOffset)?.plus(1 ) ? : - 1
421-
422- var callMode = SinkCallMode .SINGLE_SINK
423- val method = new.resolveMethod()
424- val hasCall = method?.let {
425- ReferencesSearch .search(it, ProjectScope .getProjectScope(project))
426- .findFirst()
427- } != null
428- synchronized(issues) {
429- if (hasCall) {
430- callMode = SinkCallMode .HAS_CALL
431- }
432- issues.add(
433- ProjectIssue (
434- virtualFile,
435- line,
436- className,
437- methodName,
438- sinkMatch.subType.parent.name,
439- sinkMatch.subType.name,
440- callMode
441- )
442- )
443- }
444- }
445- })
446- }
447- }
448- }
449- }, EmptyProgressIndicator ()
450- )
451- }
452- }
453-
454306}
0 commit comments