StackStorm-Exchange / StackStorm-Exchange/stackstorm-netbox

Request body is already encoded as UTF-8

Open
#24 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
14
Forks
19
Avg merge
1d 2h
Merged PRs (30d)
1

Description

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'

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at line 34 of sensors/netbox_webhooks.py and inspect how request.get_data() is passed to encode. Reproduce the Netbox webhook failure if possible, then verify that a default-templating webhook completes without the reported bytes AttributeError and silent failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Bug
Difficulty
1/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.