Copilot commented on code in PR #3511:
URL: https://github.com/apache/brpc/pull/3511#discussion_r3901055789
##########
src/brpc/policy/baidu_rpc_protocol.cpp:
##########
@@ -779,6 +779,9 @@ void ProcessRpcRequest(InputMessageBase* msg_base) {
mp->service->CallMethod(mp->method, cntl.get(), &breq, &bres,
nullptr);
break;
}
+ if (RejectBuiltinAccess(cntl.get(), *server, mp)) {
+ break;
+ }
Review Comment:
`BadMethodService` is dispatched before `RejectBuiltinAccess`, so in
security mode a client can still hit this builtin service on the public port
and get a list of available RPC methods (information leak). Run
`RejectBuiltinAccess` immediately after resolving `mp` and before any
`BadMethodService` dispatch.
##########
src/brpc/policy/hulu_pbrpc_protocol.cpp:
##########
@@ -457,6 +457,9 @@ void ProcessHuluRequest(InputMessageBase* msg_base) {
sp->service->CallMethod(sp->method, cntl.get(), &breq, &bres,
nullptr);
break;
}
+ if (RejectBuiltinAccess(cntl.get(), *server, sp)) {
+ break;
+ }
Review Comment:
`BadMethodService` is dispatched before `RejectBuiltinAccess`, so in
security mode a client can still hit this builtin service on the public port
and potentially enumerate available methods. Call `RejectBuiltinAccess` before
dispatching `BadMethodService`.
##########
src/brpc/policy/http_rpc_protocol.cpp:
##########
@@ -1620,10 +1620,7 @@ void ProcessHttpRequest(InputMessageBase *msg) {
if (!server->AcceptRequest(cntl)) {
return;
}
- } else if (security_mode) {
- cntl->SetFailed(EPERM, "Not allowed to access builtin services, try "
- "ServerOptions.internal_port=%d instead if you're in"
- " internal network", server->options().internal_port);
+ } else if (RejectBuiltinAccess(cntl, *server, mp)) {
return;
}
Review Comment:
`RejectBuiltinAccess` is called too late in the HTTP dispatch flow: it runs
after `BadMethodService` can already be invoked (early-return above) and after
`MethodStatus::OnRequested()` has run. This leaves `BadMethodService`
accessible from the public port in security mode (it prints available methods),
and may also skew method accounting for rejected builtin/tabbed requests.
Consider moving the `RejectBuiltinAccess` check to immediately after `mp` is
resolved (before the `BadMethodService` special-case and before `OnRequested`).
##########
src/brpc/details/server_private_accessor.h:
##########
@@ -104,6 +104,25 @@ class ServerPrivateAccessor {
const Server* _server;
};
+// Reject accesses to builtin services when the server is in security mode,
+// in which case they are only reachable from ServerOptions.internal_port.
+// Returns true if the access was rejected, in which case `cntl` was already
+// SetFailed() and the caller must stop dispatching the request immediately.
+// NOTE: Call this after ControllerPrivateAccessor::set_security_mode() and
+// before the method is counted by MethodStatus::OnRequested(), otherwise the
+// concurrency accounting would be unbalanced.
+inline bool RejectBuiltinAccess(Controller* cntl, const Server& server,
+ const Server::MethodProperty* mp) {
+ if (!cntl->is_security_mode() ||
+ (!mp->is_builtin_service && !mp->params.is_tabbed)) {
+ return false;
+ }
+ cntl->SetFailed(EPERM, "Not allowed to access builtin services, try "
+ "ServerOptions.internal_port=%d instead if you're in"
+ " internal network", server.options().internal_port);
+ return true;
+}
Review Comment:
`RejectBuiltinAccess` currently blocks both builtin services and tabbed
services (`mp->params.is_tabbed`), but the error message only mentions builtin
services. This can confuse users when a tabbed service is rejected in security
mode.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]