gdc: raise APIError on unloadable content (#61254)

This commit is contained in:
Benjamin Dauvergne 2022-01-31 12:20:40 +01:00
parent 4baad9742b
commit 7472aba9c6
2 changed files with 15 additions and 1 deletions

View File

@ -27,6 +27,7 @@ from django.utils.translation import ugettext_lazy as _
from passerelle.base.models import BaseResource
from passerelle.utils.api import endpoint
from passerelle.utils.jsonresponse import APIError
def deep_bytes2str(obj):
@ -48,7 +49,11 @@ def deep_bytes2str(obj):
def phpserialize_loads(s):
return deep_bytes2str(phpserialize.loads(s.encode('utf-8')))
try:
return deep_bytes2str(phpserialize.loads(s.encode('utf-8')))
except ValueError:
truncated = s[:128] if isinstance(s, str) else s
raise APIError(f'Could not deserialize GDC response {truncated!r}', data={'content': s})
class Gdc(BaseResource):

9
tests/test_gdc.py Normal file
View File

@ -0,0 +1,9 @@
import pytest
from passerelle.apps.gdc import models
from passerelle.utils.jsonresponse import APIError
def test_phpserialize_loads():
with pytest.raises(APIError, match='Could not deserialize GDC response'):
models.phpserialize_loads('aaa')