omniosorg / kayak

Kayak (PXE-enabled network imaging of OmniOS)
https://omnios.org
15 stars 71 forks source link

quick fix to make sure bytes are converted to str #257

Closed sperreault closed 11 months ago

citrus-it commented 11 months ago

Do you have any details on why this is needed and what happens if someone sends a sequence of bytes to the log server?

sperreault commented 11 months ago

Sorry for the late reply... The problem was happening when trying to install a machine using kayak:

172.16.0.11 - - [06/Sep/2023:20:47:01] "GET /kayak/omnios-r151046.zfs.xz HTTP/1.1" 404 735 "" "curl/8.0.1" [06/Sep/2023:20:47:02] HTTP Traceback (most recent call last): File "/usr/lib/pkg/python3.11/cherrypy/_cprequest.py", line 638, in respond self._do_respond(path_info) File "/usr/lib/pkg/python3.11/cherrypy/_cprequest.py", line 697, in _do_respond response.body = self.handler() ^^^^^^^^^^^^^^ File "/usr/lib/pkg/python3.11/cherrypy/lib/encoding.py", line 223, in __call__ self.body = self.oldhandler(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/pkg/python3.11/cherrypy/_cpdispatch.py", line 54, in __call__ return self.callable(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/var/svc/method/svc-kayak", line 133, in PUT log.write(cherrypy.request.body.read()) TypeError: write() argument must be str, not bytes 172.16.0.11 - - [06/Sep/2023:20:47:02] "PUT /kayaklog/3240A8484E69 HTTP/1.1" 500 824 "" "curl/8.0.1"

To be honest I did not dig into it more, I just added to quick hack to make sure I would get logs and not have the error.

citrus-it commented 11 months ago

Thanks for the extra detail. This bytes-to-string conversion should work as long as the input is ASCII or otherwise valid UTF-8 but to cover all bases, can you also change it to replace unknown characters rather than just fail? That would be something like:

log.write(str(cherrypy.request.body.read(), errors='replace'))