Site name: https://infallible-elion-0e4df6.netlify.app
Repository: https://github.com/Xenonym/netlify-binary-reprod
The site is a simple reproduction of an issue I am facing with sending requests with binary data to a Netlify function.
When you click Test, the site will send a fetch request with binary data to a Netlify function:
// As Base64: AAABAAABAAAAAAAAB2V4YW1wbGUDY29tAAABAAE=
const data = new Uint8Array([
0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
0, 0, 7, 101, 120, 97, 109, 112, 108, 101,
3, 99, 111, 109, 0, 0, 1, 0, 1
]);
async function reflect(contentType) {
const response = await fetch('/.netlify/functions/reflect', {
method: "POST",
headers: {
"Content-Type": contentType,
"Content-Length": data.length
},
body: data
});
return response.text();
}
(The binary data is a DNS packet for an A query for example.com.)
The Netlify function simply returns whatever was sent to it:
export async function handler({ body }) {
return {
statusCode: 200,
body,
};
}
This function should return identical data from what is being passed in. However, when deployed, the function seems to receive malformed data:
Sent: AAABAAABAAAAAAAAB2V4YW1wbGUDY29tAAABAAE=
Recieved: AAABAAABAAAAAAAAB2V4YW1wbGUDY29tAAAB
The data received by the function is missing the last two bytes, which can also be observed from the Base64 encoded version ending with AB
instead of the original AE=
.
This happens both when using the Content-Type
for DNS packets (application/dns-message
) or for generic binary data (application/octet-stream
).
Is this a bug with handling binary data in Netlify functions? Given that the function above does not do anything apart from what is being sent in, I am confused as to why the binary data received by the function is different from the one being sent by the client.