| | |
| | | /** |
| | | * 安全校验:防止路径遍历攻击 |
| | | */ |
| | | // private boolean isSafePath(String companyId, String fileName) { |
| | | // if (StrUtil.isBlank(companyId) || StrUtil.isBlank(fileName)) { |
| | | // return false; |
| | | // } |
| | | // // 限制字符集,禁止 .. 和以点开头 |
| | | // boolean companyIdValid = companyId.matches("^[a-zA-Z0-9_-]{1,64}$"); |
| | | // boolean fileNameValid = fileName.matches("^[a-zA-Z0-9._-]{1,255}$") && |
| | | // !fileName.startsWith(".") && |
| | | // !fileName.contains(".."); |
| | | // return companyIdValid && fileNameValid; |
| | | // } |
| | | |
| | | private boolean isSafePath(String companyId, String fileName) { |
| | | if (StrUtil.isBlank(companyId) || StrUtil.isBlank(fileName)) { |
| | | return false; |
| | | } |
| | | // 限制字符集,禁止 .. 和以点开头 |
| | | boolean companyIdValid = companyId.matches("^[a-zA-Z0-9_-]{1,64}$"); |
| | | boolean fileNameValid = fileName.matches("^[a-zA-Z0-9._-]{1,255}$") && |
| | | !fileName.startsWith(".") && |
| | | !fileName.contains(".."); |
| | | return companyIdValid && fileNameValid; |
| | | // companyId 仍然建议保持严格(防止意外目录穿越) |
| | | if (!companyId.matches("^[a-zA-Z0-9_-]{1,64}$")) { |
| | | return false; |
| | | } |
| | | // 禁止路径遍历和危险字符 |
| | | if (fileName.contains("..") || fileName.contains("/") || fileName.contains("\\") || fileName.contains("\0")) { |
| | | return false; |
| | | } |
| | | // 长度限制 |
| | | if (fileName.length() > 255) { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |