Skip to content

Conversation

@cccfeng
Copy link
Owner

@cccfeng cccfeng commented Mar 16, 2025

No description provided.

@coderabbitai
Copy link

coderabbitai bot commented Mar 16, 2025

Summary by CodeRabbit

  • 新功能
    • 新增文件上传接口,支持通过网页上传文件。
    • 提供通用文件上传和专用图片上传入口,对上传文件类型及内容进行严格验证,确保安全性与正确性。

Walkthrough

该次提交在 org.joychou.controller 包下新增了一个 FileUpload 类,用于处理文件上传功能。类中包含多个端点:indexuploadPic 提供上传页面,singleFileUpload 用于处理通用文件上传,而 uploadPicture 专注于图片上传并执行扩展名、MIME 类型及内容校验。此外,还新增了辅助方法 deleteFileconvertisImage,并通过日志记录上传过程及错误信息。

Changes

文件路径 变更摘要
src/main/.../FileUpload2.java 新增 FileUpload 类及其相关方法:indexuploadPicsingleFileUploaduploadPicturedeleteFileconvertisImage,用于处理文件与图片上传并进行多重验证和记录日志。

Sequence Diagram(s)

sequenceDiagram
    participant Client as 用户
    participant Controller as FileUpload 控制器
    participant Validator as 文件验证器
    participant Storage as 文件存储
    participant Logger as 日志记录器

    Client->>Controller: 请求上传页面 (普通/图片)
    alt 普通文件上传
        Controller->>Validator: 检查文件是否为空
        alt 文件为空
            Controller->>Client: 返回错误提示
        else
            Controller->>Storage: 保存文件
            Controller->>Logger: 记录上传成功
            Controller->>Client: 返回上传成功信息
        end
    else 图片上传
        Controller->>Validator: 检查扩展名、MIME 及图片内容
        alt 验证失败
            Controller->>Storage: 删除错误文件
            Controller->>Client: 返回错误信息
        else
            Controller->>Storage: 保存图片文件
            Controller->>Logger: 记录上传成功
            Controller->>Client: 返回成功信息
        end
    end
Loading

Poem

在代码的草原上跳跃,
我这兔子满怀欢畅。
新增上传功能如同嫩草,
每一方法都是清新一刀。
文件流转间散发着香,
🐰 跳跃中享受代码乐章!

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
84.8% Duplication on New Code (required ≤ 3%)
E Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Copy link

@coderabbitai coderabbitai bot left a 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/FileUpload2.java (2)

38-42: 方法命名与描述存在拼写错误。
注释“uplaod any file”拼写应为“upload”。同时方法 index() 返回“upload”视图,命名含义不够直观,可能改为更具描述性的名称,例如“uploadAnyFilePage”。


191-197: 图片校验逻辑通过 ImageIO.read() 判断是否为图片,思路可行,但仍需注意恶意文件伪造风险。
若攻击者提供的文件部分头部信息恰好能通过 ImageIO.read() 解析,仍可能常规通过;建议配合更严格的签名解析或额外安全校验。

您需要更严谨的图片内容检测吗?如有需要,我可以协助编写更高强度的检测逻辑或帮助打开新 issue。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4711f4e and 04bbe92.

📒 Files selected for processing (1)
  • src/main/java/org/joychou/controller/FileUpload2.java (1 hunks)
🔇 Additional comments (1)
src/main/java/org/joychou/controller/FileUpload2.java (1)

1-2: 类命名与功能保持一致,整体无大问题。
此处仅是包名与类定义的起始部分,看起来合理,未发现语法或逻辑错误。

public class FileUpload {

// Save the uploaded file to this folder
private static final String UPLOADED_FOLDER = "/tmp/";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

硬编码上传目录可能会带来安全与灵活性问题。
建议通过配置文件或环境变量来管理上传目录,避免在生产环境中直接写死“/tmp/”路径,并确认文件权限。

Comment on lines +50 to +74
@PostMapping("/upload")
public String singleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
// 赋值给uploadStatus.html里的动态参数message
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
return "redirect:/file/status";
}

try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);

redirectAttributes.addFlashAttribute("message",
"You successfully uploaded '" + UPLOADED_FOLDER + file.getOriginalFilename() + "'");

} catch (IOException e) {
redirectAttributes.addFlashAttribute("message", "upload failed");
logger.error(e.toString());
}

return "redirect:/file/status";
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

文件名直接拼接至路径存在潜在路径穿越风险。
当前未对 file.getOriginalFilename() 做目录穿越检测,若上传文件名包含 ../ 等路径可导致覆盖任意文件。建议使用安全的文件名过滤或自动生成随机文件名,避免产生安全隐患。

Comment on lines +81 to +155
// only upload picture
@PostMapping("/upload/picture")
@ResponseBody
public String uploadPicture(@RequestParam("file") MultipartFile multifile) throws Exception {
if (multifile.isEmpty()) {
return "Please select a file to upload";
}

String fileName = multifile.getOriginalFilename();
String Suffix = fileName.substring(fileName.lastIndexOf(".")); // 获取文件后缀名
String mimeType = multifile.getContentType(); // 获取MIME类型
String filePath = UPLOADED_FOLDER + fileName;
File excelFile = convert(multifile);


// 判断文件后缀名是否在白名单内 校验1
String[] picSuffixList = {".jpg", ".png", ".jpeg", ".gif", ".bmp", ".ico"};
boolean suffixFlag = false;
for (String white_suffix : picSuffixList) {
if (Suffix.toLowerCase().equals(white_suffix)) {
suffixFlag = true;
break;
}
}
if (!suffixFlag) {
logger.error("[-] Suffix error: " + Suffix);
deleteFile(filePath);
return "Upload failed. Illeagl picture.";
}


// 判断MIME类型是否在黑名单内 校验2
String[] mimeTypeBlackList = {
"text/html",
"text/javascript",
"application/javascript",
"application/ecmascript",
"text/xml",
"application/xml"
};
for (String blackMimeType : mimeTypeBlackList) {
// 用contains是为了防止text/html;charset=UTF-8绕过
if (SecurityUtil.replaceSpecialStr(mimeType).toLowerCase().contains(blackMimeType)) {
logger.error("[-] Mime type error: " + mimeType);
deleteFile(filePath);
return "Upload failed. Illeagl picture.";
}
}

// 判断文件内容是否是图片 校验3
boolean isImageFlag = isImage(excelFile);
deleteFile(randomFilePath);

if (!isImageFlag) {
logger.error("[-] File is not Image");
deleteFile(filePath);
return "Upload failed. Illeagl picture.";
}


try {
// Get the file and save it somewhere
byte[] bytes = multifile.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + multifile.getOriginalFilename());
Files.write(path, bytes);
} catch (IOException e) {
logger.error(e.toString());
deleteFile(filePath);
return "Upload failed";
}

logger.info("[+] Safe file. Suffix: {}, MIME: {}", Suffix, mimeType);
logger.info("[+] Successfully uploaded {}", filePath);
return String.format("You successfully uploaded '%s'", filePath);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

图片上传检查逻辑较完善,但以下细节可进一步增强安全性与可维护性:

  1. 变量命名不当:excelFile 命名与图片类型不符,会引起混淆。
  2. 存在多处返回“Illeagl picture.”的字符串,单词拼写应为“Illegal”。
  3. SecurityUtil.replaceSpecialStr() 的实现细节未知,需确保其不会误删关键字符或弱化安全校验。
  4. 使用静态成员 randomFilePath 保存动态文件路径容易出现并发覆盖问题,多线程下存在 race condition 风险。

Comment on lines +157 to +166
private void deleteFile(String filePath) {
File delFile = new File(filePath);
if(delFile.isFile() && delFile.exists()) {
if (delFile.delete()) {
logger.info("[+] " + filePath + " delete successfully!");
return;
}
}
logger.info(filePath + " delete failed!");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

缺少对删除文件操作的更多限制与验证。
deleteFile() 方法未校验传入路径是否在指定上传目录内;可能导致删除系统任意文件。建议增加目录约束或白名单校验。

Comment on lines +168 to +189
/**
* 为了使用ImageIO.read()
*
* 不建议使用transferTo,因为原始的MultipartFile会被覆盖
* https://stackoverflow.com/questions/24339990/how-to-convert-a-multipart-file-to-file
*/
private File convert(MultipartFile multiFile) throws Exception {
String fileName = multiFile.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));
UUID uuid = Generators.timeBasedGenerator().generate();
randomFilePath = UPLOADED_FOLDER + uuid + suffix;
// 随机生成一个同后缀名的文件
File convFile = new File(randomFilePath);
boolean ret = convFile.createNewFile();
if (!ret) {
return null;
}
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(multiFile.getBytes());
fos.close();
return convFile;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

convert 方法中缺少文件大小限制及异常处理机制。
在将 MultipartFile 写入磁盘时,若文件过大或写入受限,可能出现资源耗尽或异常未捕获的问题。使用 try-with-resources 可确保 FileOutputStream 正常关闭。

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
84.8% Duplication on New Code (required ≤ 3%)
E Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);

Check failure

Code scanning / SonarCloud

I/O function calls should not be vulnerable to path injection attacks High

Change this code to not construct the path from user-controlled data. See more on SonarQube Cloud
}
}
if (!suffixFlag) {
logger.error("[-] Suffix error: " + Suffix);

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarQube Cloud
// Get the file and save it somewhere
byte[] bytes = multifile.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + multifile.getOriginalFilename());
Files.write(path, bytes);

Check failure

Code scanning / SonarCloud

I/O function calls should not be vulnerable to path injection attacks High

Change this code to not construct the path from user-controlled data. See more on SonarQube Cloud
return "Upload failed";
}

logger.info("[+] Safe file. Suffix: {}, MIME: {}", Suffix, mimeType);

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarQube Cloud
}

logger.info("[+] Safe file. Suffix: {}, MIME: {}", Suffix, mimeType);
logger.info("[+] Successfully uploaded {}", filePath);

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarQube Cloud

private void deleteFile(String filePath) {
File delFile = new File(filePath);
if(delFile.isFile() && delFile.exists()) {

Check warning

Code scanning / SonarCloud

Accessing files should not lead to filesystem oracle attacks Medium

Change this code to not construct the path from user-controlled data. See more on SonarQube Cloud

private void deleteFile(String filePath) {
File delFile = new File(filePath);
if(delFile.isFile() && delFile.exists()) {

Check warning

Code scanning / SonarCloud

Accessing files should not lead to filesystem oracle attacks Medium

Change this code to not construct the path from user-controlled data. See more on SonarQube Cloud
private void deleteFile(String filePath) {
File delFile = new File(filePath);
if(delFile.isFile() && delFile.exists()) {
if (delFile.delete()) {

Check failure

Code scanning / SonarCloud

I/O function calls should not be vulnerable to path injection attacks High

Change this code to not construct the path from user-controlled data. See more on SonarQube Cloud
File delFile = new File(filePath);
if(delFile.isFile() && delFile.exists()) {
if (delFile.delete()) {
logger.info("[+] " + filePath + " delete successfully!");

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarQube Cloud
return;
}
}
logger.info(filePath + " delete failed!");

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarQube Cloud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants