Skip to content

Commit 4cb55b7

Browse files
committed
feat(core): enhance shell command safety check
- Add checkDangerousCommand function to identify potentially harmful shell commands - Update safety check logic to catch more dangerous operations - Modify TerminalSketchProvider to use the new checkDangerousCommand function - Improve language detection for code fences in Markdown
1 parent 5039744 commit 4cb55b7

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchToolWindow.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ open class SketchToolWindow(
313313
}
314314

315315
var language = codeFence.language
316+
/// in stream API, the <devin> maybe split, like `<dev`, `<devin` or `<devin>`,
317+
/// so we need to check the language again
316318
if (language.displayName == "Markdown" && codeFence.text.startsWith("<devin>")) {
317319
logger<SketchToolWindow>().warn("Try to fix language error")
318320
language = findLanguage("DevIn")

core/src/main/kotlin/cc/unitmesh/devti/sketch/run/ShellSyntaxSafetyCheck.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ object ShellSyntaxSafetyCheck {
2121
checkerRegistry.register(PatternCommandChecker())
2222
}
2323

24+
fun checkDangerousCommand(command: String): Pair<Boolean, String> {
25+
val dangerousPatterns = mapOf(
26+
"\\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",
27+
"\\brm\\s+-[a-zA-Z]*\\s+/\\b.*".toRegex() to "Removing files from root directory",
28+
"\\brmdir\\s+/\\b.*".toRegex() to "Removing directories from root",
29+
"\\bmkfs\\b.*".toRegex() to "Filesystem formatting command",
30+
"\\bdd\\b.*".toRegex() to "Low-level disk operation",
31+
"\\b:[(][)][{]\\s*:|:&\\s*[}];:.*".toRegex() to "Potential fork bomb",
32+
"\\bchmod\\s+-[a-zA-Z]*R\\b.*777\\b.*".toRegex() to "Recursive chmod with insecure permissions",
33+
"\\bsudo\\s+rm\\b.*".toRegex() to "Removing files with elevated privileges",
34+
)
35+
36+
// Also catch simpler rm commands (without flags but still potentially dangerous)
37+
if (command.trim().startsWith("rm ") && !command.contains("-i") && !command.contains("--interactive")) {
38+
return Pair(true, "Remove command detected, use with caution")
39+
}
40+
41+
for ((pattern, message) in dangerousPatterns) {
42+
if (pattern.containsMatchIn(command)) {
43+
return Pair(true, message)
44+
}
45+
}
46+
47+
return Pair(false, "")
48+
}
49+
2450
/**
2551
* Check if shell command contains dangerous operations
2652
* @return Pair<Boolean, String> - first: is dangerous, second: reason message

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
261261
titleLabel.text = "Terminal - ($content)"
262262

263263
val (isDangerous, reason) = try {
264-
ShellSyntaxSafetyCheck.checkDangerousCommand(project, content)
264+
ShellSyntaxSafetyCheck.checkDangerousCommand(content)
265265
} catch (e: Exception) {
266266
Pair(true, "Error checking command safety: ${e.message}")
267267
}

0 commit comments

Comments
 (0)