- 
                Notifications
    You must be signed in to change notification settings 
- Fork 15
Description
ESC currently handles binary data by coercing binary values to a (potentially-invalid) strings. This works okay internally, since Go does not validate the wellformedness of strings when coercing from binary data. However, the standard Go JSON encoder ensures that all strings are coerced to valid UTF-8 during marshaling, which replaces any invalid bytes with the replacement character. This corrupts the data.
For example, the following environment should write the bytes dcc5 43bc 1b4f f4fb 8263 d630 a199 2688 166b c084 5b71 1240 24c3 a944 d937 e2fc to a temporary file:
values:
  files:
    MY_FILE:
      fn::fromBase64: 3MVDvBtP9PuCY9YwoZkmiBZrwIRbcRJAJMOpRNk34vw=Instead, it writes the bytes efbf bdef bfbd 43ef bfbd 1b4f efbf bdef bfbd efbf bd63 efbf bd30 efbf bdef bfbd 26ef bfbd 166b efbf bdef bfbd 5b71 1240 24c3 a944 efbf bd37 efbf bdef bfbd, because the decoded value of MY_FILE is marshaled as "��C�\u001bO���c�0��\u0026�\u0016k��[q\u0012@$éD�7��" due to the presence of invalid UTF-8 bytes in the decoded value.
The most reasonable proposal I can think of is:
- Stop coercing binary values to strings in the evaluator
- This may require allowing []bytevalues inesc.Value
 
- This may require allowing 
- Marshal all []bytevalues as base64 (this is the standard behavior ofencoding/json)
- Extend the behavior of filesso that it allows the user to supply an object with the keydatathat accepts base64-encoded data which is then decoded prior to writing the file
Another alternative for (3) is to attempt to decode all values in files as base64 and then use the decoded bytes if decoding succeeds. However, the likelihood of values decoding as valid base64 may be high enough that this would result in breaks in existing environments.