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

Request body is already encoded as UTF-8 #24

Open
stanier opened this issue Mar 15, 2022 · 1 comment
Open

Request body is already encoded as UTF-8 #24

stanier opened this issue Mar 15, 2022 · 1 comment

Comments

@stanier
Copy link

stanier commented Mar 15, 2022

Upon receiving a webhook of default templating from Netbox v3.1.7, the following error is generated otherwise causing silent failure, on a fresh install.

The request data returned by request.get_data() appears to now be returning the request body encoded for UTF-8 by default, possibly as a new behavior of flask.

While I was not able to track down this exact behavior whilst skimming Flask's changelogs, I did find that they had done a lot of deprecation and other work with request JSON encoding/decoding. Referring to the documentation, https://tedboy.github.io/flask/generated/generated/flask.Request.get_data.html mentions an as_text flag for the function request.get_data(), saying this:

If as_text is set to True the return value will be a decoded unicode string

Amending the flag to the request.get_data() invocation parameters at line 34 of sensors/netbox_webhooks.py to become request.get_data(as_text=True) appears to mitigate the issue. This has been tested in production staging and appears to resolve it. Will be opening a PR to address this issue promptly.

2022-03-14 20:13:04,333 139891243266120 INFO _internal [-] 1.1.1.1 - - [14/Mar/2022 20:13:04] "ESC[35mESC[1mPOST /netbox/webhooks
/ HTTP/1.1ESC[0m" 500 -
2022-03-14 20:14:12,302 139891243266120 ERROR app [-] Exception on /netbox/webhooks/ [POST]
Traceback (most recent call last):
  File "/opt/stackstorm/virtualenvs/netbox/lib/python3.6/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/stackstorm/virtualenvs/netbox/lib/python3.6/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/stackstorm/virtualenvs/netbox/lib/python3.6/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/stackstorm/virtualenvs/netbox/lib/python3.6/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/opt/stackstorm/packs/netbox/sensors/netbox_webhooks.py", line 34, in event
    msg=request.get_data().encode('utf8'),
AttributeError: 'bytes' object has no attribute 'encode'
@stanier
Copy link
Author

stanier commented Mar 15, 2022

Alternatively, request.get_data().encode('utf8') could just become request.get_data() instead of request.get_data(as_text=True).encode('utf8'), assuming get_data() is and always will return UTF-8 encoded bytestrings when no flags are specified.

This determination would require more familiarity with Flask than I have, I know for a fact as_text=True yields the expected behavior but don't know Flask well enough to determine whether this is the proper fix.

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

No branches or pull requests

1 participant