chenBright commented on code in PR #3767:
URL: https://github.com/apache/brpc/pull/3767#discussion_r4729405607
##########
src/brpc/server.cpp:
##########
@@ -1494,6 +1635,28 @@ int
Server::AddServiceInternal(google::protobuf::Service* service,
return -1;
}
+ // Services added while Start() is running (e.g. from bthread_init_fn) are
+ // not seen by the validation of the user-provided options, so reject their
+ // restful mappings here if they cannot be served. Do it before touching
+ // the tables so that a rejection changes no state and no ownership.
+ if (_in_start) {
+ butil::StringPiece restful_mappings = svc_opt.restful_mappings;
+ restful_mappings.trim_spaces();
+ if (!restful_mappings.empty()) {
+ std::unordered_set<std::string> whitelist;
+ ParseEnabledProtocols(_options.enabled_protocols, &whitelist);
+ if (!whitelist.empty() && whitelist.count("http") == 0 &&
+ whitelist.count("h2") == 0) {
+ LOG(ERROR) << "Fail to add restful service=`" <<
sd->full_name()
+ << "': ServerOptions.enabled_protocols=`"
+ << _options.enabled_protocols
+ << "' enables neither http nor h2";
+ errno = EINVAL;
+ return -1;
Review Comment:
The service table has not been modified when registration is rejected here,
but session data may already have been reserved before `bthread_init_fn` runs.
If the callback returns `false`, as in the new test, `Start()` exits in the
`READY` state and `Join()` skips session pool cleanup. If the caller then
destroys the factory, retrying or destroying the `Server` will call
`DestroyData()` through an invalid factory. Please clean up the session pool
before returning from this startup failure path.
##########
src/brpc/server.cpp:
##########
@@ -784,6 +879,17 @@ int Server::InitALPNOptions(const ServerSSLOptions*
options) {
LOG(ERROR) << "Server does not support alpn=" << alpn;
return -1;
}
+ // Negotiating a protocol without an installed handler makes the client
+ // fail on its first request, reject the configuration instead.
+ std::unordered_set<std::string> whitelist;
+ ParseEnabledProtocols(enabled_protocols, &whitelist);
+ if (!whitelist.empty() && whitelist.count(protocol->name) == 0) {
+ LOG(ERROR) << "alpn=" << alpn << " is not in "
+ << "ServerOptions.enabled_protocols=`"
+ << enabled_protocols << '\'';
+ errno = EINVAL;
+ return -1;
+ }
raw_protocol.append(ALPNProtocolToString(protocol_type));
Review Comment:
This checks the registration name, but `ALPNProtocolToString()` encodes any
`http` prefix as `http/1.1`. For example, if only the custom protocol
`http_alt` is enabled, validation passes, but TLS can still negotiate HTTP/1.1
with the client even though the listener no longer has an `http` handler.
Consider restricting this conversion to `PROTOCOL_HTTP`.
--
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]