Skip to content

Commit

Permalink
events: add better fallback for sanitize_item to ensure everything ca…
Browse files Browse the repository at this point in the history
…n be saved as JSON (cherry-pick #7694) (#7937)

events: add better fallback for sanitize_item to ensure everything can be saved as JSON (#7694)

* events: fix events sanitizing not handling all types



* remove some leftover prints



---------

Signed-off-by: Jens Langhammer <[email protected]>
Co-authored-by: Jens L <[email protected]>
  • Loading branch information
gcp-cherry-pick-bot[bot] and BeryJu authored Dec 19, 2023
1 parent d9d5ac1 commit acc3b59
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
11 changes: 9 additions & 2 deletions authentik/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from datetime import date, datetime, time, timedelta
from enum import Enum
from pathlib import Path
from types import GeneratorType
from types import GeneratorType, NoneType
from typing import Any, Optional
from uuid import UUID

from django.contrib.auth.models import AnonymousUser
from django.core.handlers.wsgi import WSGIRequest
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django.db.models.base import Model
from django.http.request import HttpRequest
Expand Down Expand Up @@ -159,7 +160,13 @@ def sanitize_item(value: Any) -> Any:
"name": value.__name__,
"module": value.__module__,
}
return value
# List taken from the stdlib's JSON encoder (_make_iterencode, encoder.py:415)
if isinstance(value, (bool, int, float, NoneType, list, tuple, dict)):
return value
try:
return DjangoJSONEncoder.default(value)
finally:
return str(value)


def sanitize_dict(source: dict[Any, Any]) -> dict[Any, Any]:
Expand Down
1 change: 0 additions & 1 deletion authentik/stages/authenticator_sms/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def validate_and_send(self, phone_number: str):
stage: AuthenticatorSMSStage = self.executor.current_stage
hashed_number = hash_phone_number(phone_number)
query = Q(phone_number=hashed_number) | Q(phone_number=phone_number)
print(SMSDevice.objects.filter(query, stage=stage.pk))
if SMSDevice.objects.filter(query, stage=stage.pk).exists():
raise ValidationError(_("Invalid phone number"))
# No code yet, but we have a phone number, so send a verification message
Expand Down
2 changes: 0 additions & 2 deletions authentik/stages/authenticator_sms/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,9 @@ def test_stage_context_data_duplicate(self):
sms_send_mock,
),
):
print(self.client.session[SESSION_KEY_PLAN])
response = self.client.get(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
)
print(response.content.decode())
self.assertStageResponse(
response,
self.flow,
Expand Down
1 change: 0 additions & 1 deletion authentik/stages/email/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def get_full_url(self, **kwargs) -> str:
query_params = QueryDict(self.request.GET.get(QS_QUERY), mutable=True)
query_params.pop(QS_KEY_TOKEN, None)
query_params.update(kwargs)
print(query_params)
full_url = base_url
if len(query_params) > 0:
full_url = f"{full_url}?{query_params.urlencode()}"
Expand Down

0 comments on commit acc3b59

Please sign in to comment.