Skip to content

Commit 6b5666c

Browse files
committed
feat(terminal): implement safety check for dangerous shell commands
#335 - Add ShellSyntaxSafetyCheck object to detect potentially harmful commands - Integrate safety check in TerminalSketchProvider's onDoneStream method- Notify user and prevent auto-execution of dangerous commands - Display warning message in terminal and result panel
1 parent 597ccf5 commit 6b5666c

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cc.unitmesh.terminal.sketch
2+
3+
object ShellSyntaxSafetyCheck {
4+
5+
/**
6+
* Check if shell command contains dangerous operations
7+
* @return Pair<Boolean, String> - first: is dangerous, second: reason message
8+
*/
9+
fun checkDangerousCommand(command: String): Pair<Boolean, String> {
10+
val dangerousPatterns = mapOf(
11+
"\\brm\\s+(-[a-zA-Z]*f|-[a-zA-Z]*r|-[a-zA-Z]*(rf|fr))\\b.*".toRegex() to "Dangerous rm command with recursive or force flags",
12+
"\\brm\\s+-[a-zA-Z]*\\s+/\\b.*".toRegex() to "Removing files from root directory",
13+
"\\brmdir\\s+/\\b.*".toRegex() to "Removing directories from root",
14+
"\\bmkfs\\b.*".toRegex() to "Filesystem formatting command",
15+
"\\bdd\\b.*".toRegex() to "Low-level disk operation",
16+
"\\b:[(][)][{]\\s*:|:&\\s*[}];:.*".toRegex() to "Potential fork bomb",
17+
"\\bchmod\\s+-[a-zA-Z]*R\\b.*777\\b.*".toRegex() to "Recursive chmod with insecure permissions",
18+
"\\bsudo\\s+rm\\b.*".toRegex() to "Removing files with elevated privileges",
19+
)
20+
21+
// Also catch simpler rm commands (without flags but still potentially dangerous)
22+
if (command.trim().startsWith("rm ") && !command.contains("-i") && !command.contains("--interactive")) {
23+
return Pair(true, "Remove command detected, use with caution")
24+
}
25+
26+
for ((pattern, message) in dangerousPatterns) {
27+
if (pattern.containsMatchIn(command)) {
28+
return Pair(true, message)
29+
}
30+
}
31+
32+
return Pair(false, "")
33+
}
34+
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,28 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
207207
}
208208

209209
override fun onDoneStream(allText: String) {
210-
if (content.lines().size > 1) return
210+
val (isDangerous, reason) = ShellSyntaxSafetyCheck.checkDangerousCommand(content)
211+
if (isDangerous) {
212+
AutoDevNotifications.notify(project, "Auto-execution has been disabled for safety: $reason")
213+
214+
ApplicationManager.getApplication().invokeLater {
215+
terminalWidget!!.terminalStarter?.sendString(
216+
"echo \"⚠️ WARNING: $reason - Command not auto-executed for safety\"",
217+
false
218+
)
211219

220+
// Set result panel with warning
221+
resultSketch.updateViewText(
222+
"⚠️ WARNING: $reason\nThe command was not auto-executed for safety reasons.\nPlease review and run manually if you're sure.",
223+
true
224+
)
225+
collapsibleResultPanel.setTitle("Safety Warning")
226+
collapsibleResultPanel.expand()
227+
}
228+
return
229+
}
230+
231+
if (content.lines().size > 1) return
212232
ApplicationManager.getApplication().invokeLater {
213233
terminalWidget!!.terminalStarter?.sendString(content, false)
214234

0 commit comments

Comments
 (0)