Search code examples
rubygoogle-nativeclient

Write string with null characters to stdin of program


I'm attempting to write a Ruby wrapper for a Native Message client, so that I can open an external program that usually interfaces with a website in Chrome via the command line. The Native Message protocol is a message that's JSON encoded (in UTF-8) that's preceded by a 32 bit integer of the message length, all in binary. The Native Message client then takes this message through stdin and gives back a message encoded in the same way via stdout.

That I have tried doing is:

message = '{"message":"version"}'
input = [message.length, message].pack('LA*')
output = `./myNaClProgram #{input}`
puts output

The problem is that when I run this, I get the following error: string contains null byte (ArgumentError). This is presumably from the fact that there are some \0 when the message was initially turned into a uint32.

How can I pass the input string to the Native Message client without Ruby complaining about null bytes within the string? Can I use a different kind of string, or pass it in a different way?


Solution

  • This is not a ruby-specific problem. Arguments on a nix system are passed as null-terminated strings (see exec(2) for more details). That means null characters are the one type of characters arguments to any command can't have. You'll need to think of a way of passing the null-containing message in a different way than via arguments. (stdin would probably be the simplest alternative).