Trouble building flatbuffers

I'm working on PHP flatbuffers implementation. But I receive a raw string containing this message "Envelope buffer did not pass verifier".

I think I'm missing something while building.

This is the PHP code I'm using, functions should be universal between languages, what do you think is wrong?

        $builder = new Google\FlatBuffers\FlatbufferBuilder(0);

        $account = $builder->createString($account);  
        $correlation_id = $builder->createString((string) $this->id);
        $credentials = $builder->createString($this->nanoApiKey);
        $message_type = $builder->createString('AccountWeight');
        
        $message = nanoapi\AccountWeight::createAccountWeight($builder, $account);
        
        nanoapi\Envelope::startEnvelope($builder);
        nanoapi\Envelope::addCorrelationId($builder, $correlation_id);
        
        if ($this->nanoApiKey != null) {      
            nanoapi\Envelope::addCredentials($builder, $credentials);
        }
        
        nanoapi\Envelope::addMessageType($builder, $message_type);
        nanoapi\Envelope::addMessage($builder, $message);
        
        $request = nanoapi\Envelope::endEnvelope($builder);
        $builder->finish($request);

There is this option that controls verification.

[node.ipc.flatbuffers]
#verify_buffers = true

But it is important and points to a problem in the message, before the node even checks what's inside:

Access of untrusted buffers
The generated accessor functions access fields over offsets, which is very quick. These offsets are not verified at run-time, so a malformed buffer could cause a program to crash by accessing random memory.
When you're processing large amounts of data from a source you know (e.g. your own generated data on disk), this is acceptable, but when reading data from the network that can potentially have been modified by an attacker, this is undesirable.

https://google.github.io/flatbuffers/md__cpp_usage.html

I was reading this page for PHP: https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html

What you should be sending is not $request but the buffer. Like this:


// Call `finish()` to instruct the builder that this monster is complete.
 $builder->finish($orc);

The buffer is now ready to be stored somewhere, sent over the network, be compressed, or whatever you'd like to do with it. You can access the buffer like so:

// This must be called after `finish()`.
$buf = $builder->dataBuffer(); // Of type `Google\FlatBuffers\ByteBuffer`
// The data in this ByteBuffer does NOT start at 0, but at buf->getPosition().
// The end of the data is marked by buf->capacity(), so the size is
// buf->capacity() - buf->getPosition().

Now you can write the bytes to a file, send them over the network.. Make sure your file mode (or transfer protocol) is set to BINARY, not text. If you transfer a FlatBuffer in text mode, the buffer will be corrupted, which will lead to hard to find problems when you read the buffer.

Well, I solved it ...

To get the bytes:

$request = $builder->sizedByteArray();

Also message_type isn't a string but a integer, you can get it in the generated Message.php

Here is the working code, I'll update my repository anyway

        $builder = new Google\FlatBuffers\FlatbufferBuilder(0);

        foreach ($params[0] as $key => $value) {
            $params[0][$key] = $builder->createString($value);
        }    
        $params[0] = array_merge([$builder], $params[0]);
        
        $correlation_id = $builder->createString((string) $this->id);
        $credentials    = $builder->createString($this->nanoApiKey);
        $message_type   = constant("nanoapi\Message::$method");
        
        $message = call_user_func_array(
            'nanoapi\\' . $method . '::create' . $method,
            $params[0]
        );
        
        $envelope = nanoapi\Envelope::createEnvelope(
            $builder,
            null,
            $credentials,
            $correlation_id,
            $message_type,
            $message
        );

        $builder->finish($envelope);
        $request = $builder->sizedByteArray();

Thanks @Dotcom btw =)