fix: harden AI tool access check
This commit is contained in:
@@ -100,8 +100,9 @@ public final class AiToolRegistry {
|
|||||||
if (!employee.toolNames().contains(tool.name())) {
|
if (!employee.toolNames().contains(tool.name())) {
|
||||||
blockers.add("Employee " + employee.id() + " is not allowed to use tool " + tool.name());
|
blockers.add("Employee " + employee.id() + " is not allowed to use tool " + tool.name());
|
||||||
}
|
}
|
||||||
Set<String> granted = new LinkedHashSet<>(request.grantedScopes() == null
|
List<String> requestScopes = request == null || request.grantedScopes() == null
|
||||||
? List.of() : request.grantedScopes());
|
? List.of() : request.grantedScopes();
|
||||||
|
Set<String> granted = new LinkedHashSet<>(requestScopes);
|
||||||
List<String> missingScopes = new ArrayList<>();
|
List<String> missingScopes = new ArrayList<>();
|
||||||
for (String scope : tool.requiredScopes()) {
|
for (String scope : tool.requiredScopes()) {
|
||||||
if (!granted.contains(scope)) {
|
if (!granted.contains(scope)) {
|
||||||
@@ -116,11 +117,12 @@ public final class AiToolRegistry {
|
|||||||
if (!missingScopes.isEmpty()) {
|
if (!missingScopes.isEmpty()) {
|
||||||
blockers.add("Missing scopes: " + String.join(",", missingScopes));
|
blockers.add("Missing scopes: " + String.join(",", missingScopes));
|
||||||
}
|
}
|
||||||
String targetEnv = text(request.targetEnv()).toLowerCase(Locale.ROOT);
|
String targetEnv = text(request == null ? null : request.targetEnv()).toLowerCase(Locale.ROOT);
|
||||||
if (tool.localOnly() && !isLocalEnv(targetEnv)) {
|
if (tool.localOnly() && !isLocalEnv(targetEnv)) {
|
||||||
blockers.add("Tool " + tool.name() + " is local-only, targetEnv=" + targetEnv);
|
blockers.add("Tool " + tool.name() + " is local-only, targetEnv=" + targetEnv);
|
||||||
}
|
}
|
||||||
if (request.productionWrite() || isProductionEnv(targetEnv)) {
|
boolean productionWrite = request != null && request.productionWrite();
|
||||||
|
if (productionWrite || isProductionEnv(targetEnv)) {
|
||||||
if (!tool.productionWriteAllowed()) {
|
if (!tool.productionWriteAllowed()) {
|
||||||
blockers.add("Tool " + tool.name() + " is not allowed to perform production writes");
|
blockers.add("Tool " + tool.name() + " is not allowed to perform production writes");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,4 +65,13 @@ class AiToolRegistryTest {
|
|||||||
assertFalse(decision.allowed());
|
assertFalse(decision.allowed());
|
||||||
assertTrue(decision.blockers().stream().anyMatch(b -> b.contains("not allowed")));
|
assertTrue(decision.blockers().stream().anyMatch(b -> b.contains("not allowed")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void nullRequestIsRejectedWithoutException() {
|
||||||
|
var decision = registry.check(null);
|
||||||
|
|
||||||
|
assertFalse(decision.allowed());
|
||||||
|
assertTrue(decision.blockers().stream().anyMatch(b -> b.contains("Unknown AI employee")));
|
||||||
|
assertTrue(decision.blockers().stream().anyMatch(b -> b.contains("Unknown AI tool")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -209,6 +209,20 @@ class AiAgentControllerTest {
|
|||||||
assertTrue(captor.getValue().productionWrite());
|
assertTrue(captor.getValue().productionWrite());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 工具访问检查允许空body由service返回拒绝判定() throws Exception {
|
||||||
|
when(service.checkToolAccess(null)).thenReturn(new AiToolAccessDecision(
|
||||||
|
false, List.of(), List.of("Unknown AI employee: ", "Unknown AI tool: "),
|
||||||
|
null, null));
|
||||||
|
|
||||||
|
mvc.perform(post("/api/ai/tools/access-check"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.allowed").value(false))
|
||||||
|
.andExpect(jsonPath("$.blockers[0]").value("Unknown AI employee: "));
|
||||||
|
|
||||||
|
verify(service).checkToolAccess(null);
|
||||||
|
}
|
||||||
|
|
||||||
private AiAutoDevelopmentRun autoDevelopmentRun() {
|
private AiAutoDevelopmentRun autoDevelopmentRun() {
|
||||||
AiFeatureDevelopment development = new AiFeatureDevelopment(task(), List.of(draft()), changeSet());
|
AiFeatureDevelopment development = new AiFeatureDevelopment(task(), List.of(draft()), changeSet());
|
||||||
AiReleasePlan releasePlan = new AiReleasePlan("ait-1", "aics-1", "READY",
|
AiReleasePlan releasePlan = new AiReleasePlan("ait-1", "aics-1", "READY",
|
||||||
|
|||||||
Reference in New Issue
Block a user