Skip to content

Commit d3a109e

Browse files
committed
refactor(terminal): improve shell syntax safety check #335
- Remove unnecessary import of ShFile - Simplify command parsing in checkDangerousCommand function - Update test cases to use new checkDangerousCommand function
1 parent 20a7a22 commit d3a109e

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/ShellSyntaxSafetyCheck.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.intellij.psi.PsiFileFactory
55
import com.intellij.psi.util.PsiTreeUtil
66
import com.intellij.sh.ShLanguage
77
import com.intellij.sh.psi.ShCommand
8-
import com.intellij.sh.psi.ShFile
98

109
object ShellSyntaxSafetyCheck {
1110
private val checkerRegistry = ShellCommandCheckerRegistry()
@@ -27,7 +26,7 @@ object ShellSyntaxSafetyCheck {
2726
*/
2827
fun checkDangerousCommand(project: Project, command: String): Pair<Boolean, String> {
2928
val psiFile = PsiFileFactory.getInstance(project)
30-
.createFileFromText("temp.sh", ShLanguage.INSTANCE, command) as? ShFile
29+
.createFileFromText("temp.sh", ShLanguage.INSTANCE, command)
3130
?: return Pair(true, "Could not parse command")
3231

3332
val commandElements = PsiTreeUtil.findChildrenOfType(psiFile, ShCommand::class.java)

exts/ext-terminal/src/test/kotlin/cc/unitmesh/terminal/sketch/ShellSyntaxSafetyCheckTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ShellSyntaxSafetyCheckTest : BasePlatformTestCase() {
1414
)
1515

1616
for (command in safeCommands) {
17-
val result = ShellSyntaxSafetyCheck.checkDangerousCommandByPsi(project, command)
17+
val result = ShellSyntaxSafetyCheck.checkDangerousCommand(project, command)
1818
assertFalse("Should be safe: $command", result.first)
1919
assertEquals("", result.second)
2020
}
@@ -30,14 +30,14 @@ class ShellSyntaxSafetyCheckTest : BasePlatformTestCase() {
3030
)
3131

3232
for ((command, expectedMessage) in dangerousCommands) {
33-
val result = ShellSyntaxSafetyCheck.checkDangerousCommandByPsi(project, command)
33+
val result = ShellSyntaxSafetyCheck.checkDangerousCommand(project, command)
3434
assertTrue("Should be dangerous: $command", result.first)
3535
assertEquals(expectedMessage, result.second)
3636
}
3737

3838
// Test fork bomb
3939
val forkBomb = ":(){ :|:& };:"
40-
val result = ShellSyntaxSafetyCheck.checkDangerousCommandByPsi(project, forkBomb)
40+
val result = ShellSyntaxSafetyCheck.checkDangerousCommand(project, forkBomb)
4141
assertTrue("Fork bomb should be detected", result.first)
4242
assertEquals("Potential fork bomb", result.second)
4343
}

0 commit comments

Comments
 (0)