Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Python >= 3.7 deprecations, and Improve Server Resilience #6

Open
wants to merge 30 commits into
base: master
Choose a base branch
from

Conversation

trinitronx
Copy link

@trinitronx trinitronx commented Jun 22, 2024

I noticed that after a recent python upgrade (Arch Linux is now on Python 3.10.12) that the server component stopped working due to the removal of some deprecated things in the Python language that were scheduled to be removed since Python 3.7. So this PR started out as just trying to fix those things.

However, it soon grew larger because after fixing those, I found that it was still throwing some exceptions and stack traces when doing things like uploading large files, image mime types, and generally exercising the API in ways that a security researcher might try to do.

I was inspired after reading your blog post about this Father-Son project, which I had been using for many years as a way to use Airdrop clipboard functionality between iOS and Linux.

So, in the spirit of Linux & Open Source, I decided to try my hand at making the server component much more resilient & secure. For example, I tested trying to use curl as the client and sending a large growing log file, but not sending the Content-Length header, which caused a possible DoS-style deadlock condition where both server and client would wait for something to be send from the other which would never happen. This led me towards fixing a bunch of issues that I encountered in testing.

This PR improves the Python-based server component in the following ways:

  • Support large file uploads, including many binary or non-text mime types (as multipart/form-data uploads)
  • Generally make the server much more resilient
    • Fix all Python language deprecations by using the recommended replacements from official Python documentation
    • Encountering an error exception no longer brings down the entire server
    • Send all error responses in JSON to the client for common cases
    • Improve checks for contents.items dict keys before trying to access them (was causing KeyError exceptions in certain cases
  • Improve logging:
    • Add support for up to 5 verbosity levels
    • Log exceptions encountered in server logs
    • Sanitize (backslash-escape + truncate) all client-provided data before logging it
    • Send proper HTTP error codes with stack traces (if verbosity is 3 or higher) when encountering server-side exceptions
    • Add 60 second server-side timeout to handle a request (Prevents indefinite deadlock DoS attack vector mentioned above)
  • Now the server makes Coffee and Tea (er... well really mostly Tea)
    • Just a fun easter-egg type thing, as a classic HTTP response code April Fools' joke

I hope these improvements are helpful to improve the server component. I haven't yet learned Swift, and am not familiar with iOS development yet, so unfortunately I couldn't improve the iOS client at the same time. So, some things I tested like image push only work in one direction (Linux -> iOS), but not yet the other way around. Using two Python-based server/clients should work, however.

P.S. I didn't see an OSI-approved LICENSE file in this repo. Would you be willing to choose a license and officially make this code truly Open-Source? I'd recommend the GPLv3 to help foster the ability of the community to share improvements.

Anyway, Thanks for such a great and useful project!

It is currently a request handler based on BaseHTTPRequestHandler, which does not allow us to pass in the verbosity level because it gets instantiated by the HTTPServer class
- Use plain string instead of f'' strings where not needed
- Remove semicolons where not needed
This is to prepare for adding verbosity level to RemoteStashServer request Handler & fixing the file push / POST capability
- Rename class RemoteStashServer(BaseHTTPRequestHandler)
  - to: RemoteStashServerRequestHandler(BaseHTTPRequestHandler)
- Implement stderr print function
- Implement sanitize_data() function for logging bytes / string
…ogging

This should allow figuring out the issues with file upload / POST
This seems to have been the cause of **many** RemoteStash pull failures!

What was happening here:

  - Client would call `GET /pull` when server has a binary file in stash
  - The client would see HTTP/1.1 200 ...long potentially binary string here...
  - Client would fail to parse the HTTP status line, and rightfully hang up
  - Server would see EOF in the middle of sending the item content
  - Exception would be thrown, and second 500 error response would trigger!
  - Client never sees the second 500 error response... but at least it was logged!

Error was:

    ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2426)

Traceback (most recent call last):
  File "/home/exampleuser/.local/bin/remotestash", line 919, in do_GET
    getattr(self, method)()
  File "/home/exampleuser/.local/bin/remotestash", line 879, in last
    self.respond_item( item )
  File "/home/exampleuser/.local/bin/remotestash", line 1092, in respond_item
    self.respond( 200, headers, message, log_message=f'Request: {item}' )
  File "/home/exampleuser/.local/bin/remotestash", line 1189, in respond
    self.end_headers()
  File "/home/exampleuser/.pyenv/versions/3.10.12/lib/python3.10/http/server.py", line 535, in end_headers
    self.flush_headers()
  File "/home/exampleuser/.pyenv/versions/3.10.12/lib/python3.10/http/server.py", line 539, in flush_headers
    self.wfile.write(b"".join(self._headers_buffer))
  File "/home/exampleuser/.pyenv/versions/3.10.12/lib/python3.10/socketserver.py", line 826, in write
    self._sock.sendall(b)
  File "/home/exampleuser/.pyenv/versions/3.10.12/lib/python3.10/ssl.py", line 1237, in sendall
    v = self.send(byte_view[count:])
  File "/home/exampleuser/.pyenv/versions/3.10.12/lib/python3.10/ssl.py", line 1206, in send
    return self._sslobj.write(data)
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2426)
 - Memory limit for form data processing only
   applies as written if the `Content-Type` is
   `"application/x-www-form-urlencoded"` or
   `"application/x-url-encoded"`.
 - The memory limmit is not applied to multipart
   form data processing unless the disk limit is
   less than the memory limit.
   - In other words:
   - If Content-Type is `"multipart/form-data"`,
     the memory limit is set to the lesser of
     memory limit or disk limit.
 - Provide references to relevant RFCs and errata.
 - Modify `parse_form_data()` function call to
   include the `strict_mode` and `mem_limit`
   parameters.
     - `strict_mode` is set to `True`
     - `mem_limit` is set to the value of
   `DEFAULT_FORMDATA_CONTENT_LENGTH_MEM_LIMIT`,
   which is 1 MiB (1 mebibyte = 1024*1024 bytes).
     - `disk_limit` is set to the value of
    `DEFAULT_FORMDATA_CONTENT_LENGTH_DISK_LIMIT`,
    which is 1 GiB (1 gibibyte = 1024*1024*1024
    bytes).
 - Ensures the multipart code handles form data
   processing correctly, throwing exceptions from
   `parse_form_data()` and adheres to the
   specified memory limit when dealing with
   URL-encoded form data.  Also ensures that the
   specified disk limit is adhered to for
   multipart form uploads.
 - Set the `timeout` property of the
     `RemoteStashServerRequestHandler` class to 60
     seconds.
 - If a request takes longer than 60 seconds to
    process, a `TimeoutError` will be raised.
 - Added exception handling blocks for
   `TimeoutError`.
 - Respond with status code 408 (Request Timeout)
    if a `TimeoutError` is raised.
 - Send `REQUEST_METHOD` to `parse_form_data()` to
   handle POST/GET requests properly.
 - Send appropriate response code: 405 Method Not
   Allowed with helpful `Allowed:` error header.
@trinitronx trinitronx force-pushed the fix-all-the-things branch from 812a706 to 23514ae Compare June 22, 2024 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant