安全防护

This commit is contained in:
lu7213 committed 2026-11-16 02:09:23 -05:00
1 parent 1daf477899
commit 98657ace5b
25 files changed
+570 -4

No files matched your search

@@ -0,0 +1,18 @@
package com.ruoyi.web.core.filter;
import com.ruoyi.system.security.SafeYamlPropertySourceLoader;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import static org.junit.Assert.*;
/** 只解析当前项目配置,不启动应用、不连接数据库,且不打印配置中的秘密。 */
public class ExistingYamlCompatibilityTest {
@Test
public void allShippedConfigurationFilesRemainReadable() throws Exception {
SafeYamlPropertySourceLoader loader = new SafeYamlPropertySourceLoader();
for (String name : new String[] {"application.yml", "application-prod.yml", "application-local.yml",
"application-druid.yml", "application-rental.yml", "application-hardening.yml"}) {
assertFalse(name, loader.load(name, new ClassPathResource(name)).isEmpty());
}
}
}
@@ -0,0 +1,49 @@
package com.ruoyi.web.core.filter;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.junit.Assert.*;
public class ManagementSourceFilterTest {
private boolean invoke(String path, String remote, String forwarded, String cidrs) throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServletPath(path);
request.setRemoteAddr(remote);
request.addHeader("X-Forwarded-For", forwarded);
MockHttpServletResponse response = new MockHttpServletResponse();
AtomicBoolean passed = new AtomicBoolean();
new ManagementSourceFilter(cidrs).doFilter(request, response,
(req, res) -> passed.set(true));
if (!passed.get()) assertEquals(403, response.getStatus());
return passed.get();
}
@Test
public void directPublicClientCannotSpoofLoopback() throws Exception {
for (String path : new String[] {"/monitor/job", "/monitor/job/run", "/monitor/jobLog/list",
"/monitor/job.json", "/monitor/job;id=1", "/druid/"}) {
assertFalse(invoke(path, "203.0.113.10", "127.0.0.1", "127.0.0.1/32,::1/128"));
}
}
@Test
public void trustedIpv4AndIpv6CanReachOriginalAuthenticationChain() throws Exception {
assertTrue(invoke("/monitor/job/list", "10.8.0.10", "203.0.113.10", "10.8.0.0/24"));
assertTrue(invoke("/monitor/job/list", "::1", "203.0.113.10", "::1/128"));
}
@Test
public void paymentCallbackAndOtherBusinessRoutesRemainUnchanged() throws Exception {
for (String path : new String[] {"/api/v1/order/query", "/api/rental/payment/notify/1",
"/api/telegram/webhook", "/node/v1/socket", "/login", "/system/wallet/list"}) {
assertTrue(invoke(path, "203.0.113.10", "203.0.113.10", "127.0.0.1/32"));
}
}
@Test(expected = IllegalArgumentException.class)
public void invalidHostnameFailsClosedWithoutDns() {
new ManagementSourceFilter("example.invalid");
}
}