Skip to content

Commit

Permalink
Disallow POST application/json (only x-www-form-urlencoded is allowed)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderlukanin13 committed Dec 6, 2017
1 parent bce66be commit 26c60e2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Get QR code using [HTTPie](https://httpie.org/):
http -v --form POST http://localhost:9001 text=abracadabra --download -o qrcode.png
http -v --form POST http://localhost:9001 base64=YWJyYWNhZGFicmE= --download -o qrcode.png

Note that only `application/x-www-form-urlencoded` is allowed.

## Testing

Using tox:
Expand Down
2 changes: 2 additions & 0 deletions pozetron_barcode/barcode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class BarcodeResource:

@staticmethod
def on_post(req, resp):
if not req.content_type or req.content_type.split(';')[0] != 'application/x-www-form-urlencoded':
raise falcon.HTTPUnsupportedMediaType(description='Use application/x-www-form-urlencoded')
# Get data (bytes) from request
try:
if len(req.params) != 1:
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def simulate_get_png(self, *args, **kw):

def simulate_post_png(self, *args, **kw):
self._add_file_wrapper(kw)
# If Content-Type is not specified explicitly, set application/x-www-form-urlencoded
try:
headers = kw['headers']
except KeyError:
headers = kw['headers'] = {}
if 'Content-Type' not in headers:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
return super().simulate_post(*args, **kw)

def _add_file_wrapper(self, kw):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_barcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ def test_get_barcode(client):
assert response.content == b''


def test_post_barcode_invalid(client):
response = client.simulate_post_png('/',
body='{"text":"abracadabra"}',
headers={'Content-Type': 'application/json'})
assert response.status_code == 415
assert response.json == {
'title': 'Unsupported media type',
'description': 'Use application/x-www-form-urlencoded'
}


def test_post_barcode(client, abracadabra_png):
# No params
response = client.simulate_post_png('/')
Expand Down

0 comments on commit 26c60e2

Please sign in to comment.