AnDiXL commented on code in PR #3559:
URL: https://github.com/apache/brpc/pull/3559#discussion_r4470981554


##########
src/brpc/policy/flatbuffers_protocol.cpp:
##########
@@ -0,0 +1,475 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/policy/flatbuffers_protocol.h"
+
+#if BRPC_WITH_FLATBUFFERS
+#include <algorithm>
+#include <cstring>
+#include <limits>
+#include <memory>
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "brpc/controller.h"
+#include "brpc/server.h"
+#include "brpc/socket.h"
+#include "brpc/flatbuffers/message.h"
+#include "brpc/flatbuffers/service.h"
+#include "brpc/policy/most_common_message.h"
+#include "brpc/details/controller_private_accessor.h"
+#include "brpc/details/server_private_accessor.h"
+#include "brpc/details/usercode_backup_pool.h"
+
+extern "C" void bthread_assign_data(void* data);
+
+namespace brpc {
+namespace policy {
+namespace {
+
+const size_t kHeaderSize = 12;
+const size_t kRequestMetaSize = 24;
+const size_t kResponseMetaSize = 20;
+
+// Every integer in the header and in the fixed metadata prefix is in network
+// byte order (big-endian). meta_size permits appended fields. memcpy handles
+// fragmented or unaligned input without packed structs or aliasing issues.
+uint32_t Load32(const unsigned char* p) {
+    uint32_t value;
+    memcpy(&value, p, sizeof(value));
+    return butil::NetToHost32(value);
+}
+
+uint64_t Load64(const unsigned char* p) {
+    uint64_t value;
+    memcpy(&value, p, sizeof(value));
+    return butil::NetToHost64(value);
+}
+
+void Store32(unsigned char* p, uint32_t value) {
+    const uint32_t net = butil::HostToNet32(value);
+    memcpy(p, &net, sizeof(net));
+}
+
+void Store64(unsigned char* p, uint64_t value) {
+    const uint64_t net = butil::HostToNet64(value);
+    memcpy(p, &net, sizeof(net));
+}
+
+bool ValidSizes(size_t message_size, size_t attachment_size, size_t meta_size) 
{
+    const size_t limit = 
static_cast<size_t>(std::numeric_limits<int32_t>::max());
+    return message_size <= limit && attachment_size <= limit &&
+           meta_size <= limit && message_size <= limit - meta_size &&
+           attachment_size <= limit - meta_size - message_size &&
+           meta_size + message_size + attachment_size <=
+               static_cast<size_t>(FLAGS_max_body_size);
+}
+
+void PackHeader(unsigned char* header, size_t meta_size, size_t payload_size) {
+    memcpy(header, "FRPC", 4);
+    Store32(header + 4, static_cast<uint32_t>(meta_size + payload_size));
+    Store32(header + 8, static_cast<uint32_t>(meta_size));
+}
+
+bool SerializePayload(const flatbuffers::Message& message, butil::IOBuf* out) {
+    butil::IOBuf body;
+    if (!message.append_msg_to_iobuf(body)) {
+        return false;
+    }
+    body.pop_front(message.get_meta_size());
+    if (body.size() != message.size()) {
+        return false;
+    }
+    out->append(body);
+    return true;
+}
+
+bool ValidPayloadSizes(uint32_t message_size, uint32_t attachment_size,
+                       size_t payload_size) {
+    const uint32_t limit = 
static_cast<uint32_t>(std::numeric_limits<int32_t>::max());
+    return message_size <= limit && attachment_size <= limit &&
+           attachment_size <= payload_size &&
+           message_size == payload_size - attachment_size;
+}
+
+// One closure owns the request, response, controller and concurrency 
accounting
+// until the application completes, including asynchronous service methods.
+class FlatBuffersCall : public google::protobuf::Closure {
+public:
+    FlatBuffersCall(uint64_t id, int64_t received_us)
+        : correlation_id(id), received_us(received_us), cntl(new Controller),
+          service(nullptr), method(nullptr), status(nullptr) {}
+
+    ~FlatBuffersCall() override {
+        {
+            ConcurrencyRemover remover(status, cntl.get(), received_us);
+        }
+        cntl->CallAfterRpcResp(&request, &response);
+    }
+
+    void Run() override {
+        std::unique_ptr<FlatBuffersCall> self(this);
+        ControllerPrivateAccessor accessor(cntl.get());
+        Socket* socket = accessor.get_sending_socket();
+        if (cntl->IsCloseConnection()) {
+            socket->SetFailed();
+            return;
+        }
+
+        butil::IOBuf payload;
+        size_t attachment_size = 0;
+        if (!cntl->Failed()) {
+            if (cntl->response_compress_type() != COMPRESS_TYPE_NONE ||
+                cntl->response_checksum_type() != CHECKSUM_TYPE_NONE) {
+                cntl->SetFailed(ERESPONSE, "FRPC does not support compression 
or checksums");
+            } else if (!SerializePayload(response, &payload)) {
+                cntl->SetFailed(ERESPONSE, "Empty or invalid FlatBuffers 
response");
+            } else {
+                attachment_size = cntl->response_attachment().size();
+                if (!ValidSizes(payload.size(), attachment_size, 
kResponseMetaSize)) {
+                    cntl->SetFailed(ERESPONSE, "FlatBuffers response is too 
large");
+                }
+            }
+        }
+        if (cntl->Failed()) {
+            payload.clear();
+            attachment_size = 0;
+        }
+
+        unsigned char header[kHeaderSize + kResponseMetaSize] = {};
+        PackHeader(header, kResponseMetaSize, payload.size() + 
attachment_size);
+        Store32(header + 12, static_cast<uint32_t>(cntl->ErrorCode()));
+        Store32(header + 16, static_cast<uint32_t>(payload.size()));
+        Store32(header + 20, static_cast<uint32_t>(attachment_size));
+        Store64(header + 24, correlation_id);
+        butil::IOBuf wire;
+        if (wire.append(header, sizeof(header)) != 0) {
+            socket->SetFailed(ENOMEM, "Fail to allocate FlatBuffers response 
header");
+            return;
+        }
+        wire.append(payload);
+        if (attachment_size) {
+            wire.append(cntl->response_attachment());
+        }
+        Socket::WriteOptions options;
+        options.ignore_eovercrowded = true;
+        if (socket->Write(&wire, &options) != 0) {
+            cntl->SetFailed(errno, "Fail to write FlatBuffers response");
+        }
+    }
+
+    static void Invoke(void* arg) {
+        FlatBuffersCall* call = static_cast<FlatBuffersCall*>(arg);
+        call->service->FBCallMethod(call->method, call->cntl.get(),
+                                   &call->request, &call->response, call);
+    }
+
+    uint64_t correlation_id;
+    int64_t received_us;
+    std::unique_ptr<Controller, LogErrorTextAndDelete> cntl;
+    flatbuffers::Message request;
+    flatbuffers::Message response;
+    flatbuffers::Service* service;
+    const flatbuffers::MethodDescriptor* method;
+    MethodStatus* status;
+
+private:
+    DISALLOW_COPY_AND_ASSIGN(FlatBuffersCall);
+};
+
+}  // namespace
+
+ParseResult ParseFlatBuffersMessage(butil::IOBuf* source, Socket*, bool,
+                                   const void*) {
+    unsigned char header[kHeaderSize] = {};
+    const size_t n = source->copy_to(header, sizeof(header));
+    if (memcmp(header, "FRPC", std::min(n, size_t(4))) != 0) {
+        return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+    }
+    if (n < sizeof(header)) {
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+    const uint32_t body_size = Load32(header + 4);
+    const uint32_t meta_size = Load32(header + 8);
+    if (body_size > static_cast<uint64_t>(FLAGS_max_body_size)) {
+        return MakeParseError(PARSE_ERROR_TOO_BIG_DATA);
+    }
+    if (meta_size > body_size) {
+        return MakeParseError(PARSE_ERROR_ABSOLUTELY_WRONG,
+                              "FRPC metadata exceeds frame body");
+    }
+    if (source->size() - kHeaderSize < body_size) {
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+    MostCommonMessage* message = MostCommonMessage::Get();
+    if (!message) {
+        return MakeParseError(PARSE_ERROR_NO_RESOURCE);
+    }
+    source->pop_front(kHeaderSize);
+    source->cutn(&message->meta, meta_size);
+    source->cutn(&message->payload, body_size - meta_size);
+    return MakeMessage(message);
+}
+
+bool VerifyFlatBuffersRequest(const InputMessageBase* message) {
+    const Server* server = static_cast<const Server*>(message->arg());
+    // No credential format has been defined for FRPC. Never silently bypass
+    // authentication configured for the server's other protocols.
+    return server->options().auth == nullptr;
+}
+
+void ProcessFlatBuffersRequest(InputMessageBase* message_base) {
+    DestroyingPtr<MostCommonMessage> message(
+        static_cast<MostCommonMessage*>(message_base));
+    SocketUniquePtr socket_guard(message->ReleaseSocket());
+    Socket* socket = socket_guard.get();
+    const Server* server = static_cast<const Server*>(message->arg());
+    ScopedNonServiceError non_service_error(server);
+    unsigned char meta[kRequestMetaSize];
+    if (message->meta.copy_to(meta, sizeof(meta)) != sizeof(meta)) {
+        socket->SetFailed(EREQUEST, "Truncated FlatBuffers request metadata");
+        return;
+    }
+    const uint32_t service_id = Load32(meta);
+    const uint32_t method_id = Load32(meta + 4);
+    const uint32_t message_size = Load32(meta + 8);
+    const uint32_t attachment_size = Load32(meta + 12);
+    auto call = std::make_unique<FlatBuffersCall>(Load64(meta + 16),
+                                                  message->received_us());
+    Controller* cntl = call->cntl.get();
+    ControllerPrivateAccessor accessor(cntl);
+    ServerPrivateAccessor server_accessor(server);
+    const bool security_mode = server->options().security_mode() &&
+                               socket->user() == server_accessor.acceptor();
+    cntl->set_rpc_received_us(message->received_us());
+    accessor.set_server(server)
+        .set_security_mode(security_mode)
+        .set_peer_id(socket->id())
+        .set_remote_side(socket->remote_side())
+        .set_local_side(socket->local_side())
+        .set_auth_context(socket->auth_context())

Review Comment:
   No. FRPC has no credential field, so authentication is rejected instead of 
skipped: if the server has `ServerOptions::auth` set, 
`VerifyFlatBuffersRequest` fails and the connection is closed, and a client 
channel with `auth` set fails the RPC with `EREQUEST`. 
`AuthenticationOnServerCannotBeBypassed` and `ClientAuthenticationIsRejected` 
cover both sides, and the guide lists authentication as unsupported. Support 
can be added later as a metadata extension; `meta_size` already allows appended 
fields.



-- 
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]

Reply via email to