-
Notifications
You must be signed in to change notification settings - Fork 0
add Rce.java #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
add Rce.java #3
Conversation
Summary by CodeRabbit
Walkthrough本次提交新增了 Changes
Sequence Diagram(s)sequenceDiagram
participant C as Client
participant R as Rce Controller
participant E as Executor
participant S as ScriptEngine
participant Y as YAML Loader
C->>R: HTTP 请求 (/rce?参数)
alt 使用命令执行
R->>E: Runtime.exec 或 ProcessBuilder 执行命令
E-->>R: 返回命令输出
else 使用脚本执行
R->>S: 调用 jsEngine 或 groovyshell
S-->>R: 返回脚本执行结果
else 加载 YAML 内容
R->>Y: 调用 yarm 或 secYarm 方法
Y-->>R: 完成 YAML 加载
end
R->>C: 返回执行结果
Poem
Tip ⚡🧪 Multi-step agentic review comment chat (experimental)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
🧹 Nitpick comments (2)
src/main/java/org/joychou/controller/Rce2.java (2)
117-120: 使用 SafeConstructor 依旧需要注意附加安全策略
虽然此处已改用new SafeConstructor(),但在加载不受信任的 YAML 时,仍需保证类型白名单或额外的安全校验,以彻底阻止潜在的反序列化攻击。
134-136: 建议在主方法中完善异常处理或移除此测试用逻辑
Runtime.getRuntime().exec("touch /tmp/x")未对执行结果进行检查,且无明显业务意义。若此逻辑仅用于本地开发测试,建议在生产环境移除,或补充异常处理以防止执行失败时出现未知错误。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/main/java/org/joychou/controller/Rce2.java(1 hunks)
🧰 Additional context used
🪛 GitHub Check: SonarCloud
src/main/java/org/joychou/controller/Rce2.java
[failure] 36-36: OS commands should not be vulnerable to command injection attacks
Change this code to not construct the OS command from user-controlled data.See more on SonarQube Cloud
[failure] 69-69: OS commands should not be vulnerable to command injection attacks
Change this code to not construct the OS command from user-controlled data.See more on SonarQube Cloud
[failure] 100-100: Dynamic code execution should not be vulnerable to injection attacks
Change this code to not dynamically execute code influenced by user-controlled data.See more on SonarQube Cloud
| public void yarm(String content) { | ||
| Yaml y = new Yaml(); | ||
| y.load(content); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
通过不安全的构造器加载 YAML 或导致任意代码执行
使用 new Yaml() 加载未受控内容时,可能触发反序列化漏洞或其他任意代码执行风险(尤其在 org.yaml.snakeyaml 旧版本中)。如需解析复杂数据结构,应启用更严格的限制或使用 SafeConstructor 并进一步加强类型白名单。
| public void groovyshell(String content) { | ||
| GroovyShell groovyShell = new GroovyShell(); | ||
| groovyShell.evaluate(content); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
动态执行 Groovy 脚本存在远程代码执行风险
GroovyShell 可执行任意自定义脚本。若用户可控制 content,将存在高危RCE。应视具体需求对脚本进行严格限制或考虑移除此功能。
| public void jsEngine(String jsurl) throws Exception{ | ||
| // js nashorn javascript ecmascript | ||
| ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); | ||
| Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); | ||
| String cmd = String.format("load(\"%s\")", jsurl); | ||
| engine.eval(cmd, bindings); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
动态执行JS脚本存在严重安全隐患
jsEngine 方法从外部 URL 加载并执行 JavaScript,这为攻击者提供了远程任意代码执行的可能。建议彻底审查业务需求,若非必要,不要允许外部脚本的直接加载与执行。
如果确有场景需要运行受信任脚本,建议采用签名校验或严格白名单方式:
String cmd = String.format("load(\"%s\")", jsurl);
-engine.eval(cmd, bindings);
+throw new UnsupportedOperationException("禁止直接加载外部JS,需改用安全方案或白名单。");📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| public void jsEngine(String jsurl) throws Exception{ | |
| // js nashorn javascript ecmascript | |
| ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); | |
| Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); | |
| String cmd = String.format("load(\"%s\")", jsurl); | |
| engine.eval(cmd, bindings); | |
| } | |
| public void jsEngine(String jsurl) throws Exception{ | |
| // js nashorn javascript ecmascript | |
| ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); | |
| Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); | |
| String cmd = String.format("load(\"%s\")", jsurl); | |
| throw new UnsupportedOperationException("禁止直接加载外部JS,需改用安全方案或白名单。"); | |
| } |
🧰 Tools
🪛 GitHub Check: SonarCloud
[failure] 100-100: Dynamic code execution should not be vulnerable to injection attacks
Change this code to not dynamically execute code influenced by user-controlled data.See more on SonarQube Cloud
| @GetMapping("/runtime/exec") | ||
| public String CommandExec(String cmd) { | ||
| Runtime run = Runtime.getRuntime(); | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| try { | ||
| Process p = run.exec(cmd); | ||
| BufferedInputStream in = new BufferedInputStream(p.getInputStream()); | ||
| BufferedReader inBr = new BufferedReader(new InputStreamReader(in)); | ||
| String tmpStr; | ||
|
|
||
| while ((tmpStr = inBr.readLine()) != null) { | ||
| sb.append(tmpStr); | ||
| } | ||
|
|
||
| if (p.waitFor() != 0) { | ||
| if (p.exitValue() == 1) | ||
| return "Command exec failed!!"; | ||
| } | ||
|
|
||
| inBr.close(); | ||
| in.close(); | ||
| } catch (Exception e) { | ||
| return e.toString(); | ||
| } | ||
| return sb.toString(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
存在高危的系统命令注入风险(Runtime.exec)
此处直接将用户输入 cmd 传给 Runtime.exec(),可能导致攻击者执行任意命令。建议对输入进行严格校验或移除此功能,并在必要时使用更安全的替代方案(例如预定义的命令白名单)。
以下示例展示如何禁止直接执行来自用户的输入:
public String CommandExec(String cmd) {
- Process p = run.exec(cmd);
+ // TODO: 强制使用安全设计,禁止直接执行用户命令
+ throw new UnsupportedOperationException("禁止传入任意命令执行。请先进行安全改造。");
}Committable suggestion skipped: line range outside the PR's diff.
🧰 Tools
🪛 GitHub Check: SonarCloud
[failure] 36-36: OS commands should not be vulnerable to command injection attacks
Change this code to not construct the OS command from user-controlled data.See more on SonarQube Cloud
| public String processBuilder(String cmd) { | ||
|
|
||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| try { | ||
| String[] arrCmd = {"/bin/sh", "-c", cmd}; | ||
| ProcessBuilder processBuilder = new ProcessBuilder(arrCmd); | ||
| Process p = processBuilder.start(); | ||
| BufferedInputStream in = new BufferedInputStream(p.getInputStream()); | ||
| BufferedReader inBr = new BufferedReader(new InputStreamReader(in)); | ||
| String tmpStr; | ||
|
|
||
| while ((tmpStr = inBr.readLine()) != null) { | ||
| sb.append(tmpStr); | ||
| } | ||
| } catch (Exception e) { | ||
| return e.toString(); | ||
| } | ||
|
|
||
| return sb.toString(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同样存在高危的系统命令注入风险(ProcessBuilder)
与 Runtime.exec 相似,ProcessBuilder 在此处直接使用了用户输入的命令字符串 cmd。若攻击者能控制此参数,则可能导致任意命令执行。应避免基于未经校验的外部输入拼装命令。
可考虑通过以下方式处理:
String[] arrCmd = {"/bin/sh", "-c", cmd};
-ProcessBuilder processBuilder = new ProcessBuilder(arrCmd);
+// TODO: 禁止使用未校验的用户输入进行命令执行
+throw new UnsupportedOperationException("禁止传入任意命令执行。请先进行安全改造。");Committable suggestion skipped: line range outside the PR's diff.
🧰 Tools
🪛 GitHub Check: SonarCloud
[failure] 69-69: OS commands should not be vulnerable to command injection attacks
Change this code to not construct the OS command from user-controlled data.See more on SonarQube Cloud
|
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| try { | ||
| Process p = run.exec(cmd); |
Check failure
Code scanning / SonarCloud
OS commands should not be vulnerable to command injection attacks
|
|
||
| try { | ||
| String[] arrCmd = {"/bin/sh", "-c", cmd}; | ||
| ProcessBuilder processBuilder = new ProcessBuilder(arrCmd); |
Check failure
Code scanning / SonarCloud
OS commands should not be vulnerable to command injection attacks
| ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); | ||
| Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); | ||
| String cmd = String.format("load(\"%s\")", jsurl); | ||
| engine.eval(cmd, bindings); |
Check failure
Code scanning / SonarCloud
Dynamic code execution should not be vulnerable to injection attacks
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| try { | ||
| Process p = run.exec(cmd); |
Check failure
Code scanning / SonarCloud
OS commands should not be vulnerable to command injection attacks
|
|
||
| try { | ||
| String[] arrCmd = {"/bin/sh", "-c", cmd}; | ||
| ProcessBuilder processBuilder = new ProcessBuilder(arrCmd); |
Check failure
Code scanning / SonarCloud
OS commands should not be vulnerable to command injection attacks
| ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); | ||
| Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); | ||
| String cmd = String.format("load(\"%s\")", jsurl); | ||
| engine.eval(cmd, bindings); |
Check failure
Code scanning / SonarCloud
Dynamic code execution should not be vulnerable to injection attacks




No description provided.