Skip to content

Commit afb4647

Browse files
authored
Audit logs: truncate browser (#9417)
* Audit logs: truncate browser Closes #9315 * Lint
1 parent aa62be2 commit afb4647

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

readthedocs/audit/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class AuditLog(TimeStampedModel):
6666
and the deleted user/project/organization can be accessed via the ``log_*`` attributes.
6767
"""
6868

69+
# pylint: disable=too-many-instance-attributes
70+
6971
PAGEVIEW = 'pageview'
7072
DOWNLOAD = 'download'
7173
AUTHN = 'authentication'
@@ -196,8 +198,18 @@ def save(self, **kwargs):
196198
if self.organization:
197199
self.log_organization_id = self.organization.id
198200
self.log_organization_slug = self.organization.slug
201+
202+
self._truncate_browser()
203+
199204
super().save(**kwargs)
200205

206+
def _truncate_browser(self):
207+
browser_max_length = self._meta.get_field("browser").max_length
208+
if self.browser and len(self.browser) > browser_max_length:
209+
suffix = " - Truncated"
210+
truncated_at = browser_max_length - len(suffix)
211+
self.browser = self.browser[:truncated_at] + suffix
212+
201213
def auth_backend_display(self):
202214
"""
203215
Get a string representation for backends that aren't part of the normal login.

readthedocs/audit/tests/test_models.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,21 @@ def test_log_attached_to_organization_only(self):
7777
self.assertEqual(log.organization, self.organization)
7878
self.assertEqual(log.log_organization_id, self.organization.id)
7979
self.assertEqual(log.log_organization_slug, self.organization.slug)
80+
81+
def test_truncate_browser(self):
82+
text = "a" * 250
83+
log = get(
84+
AuditLog,
85+
user=self.user,
86+
browser=text,
87+
)
88+
self.assertEqual(log.browser, text)
89+
90+
text = "a" * 300
91+
log = get(
92+
AuditLog,
93+
user=self.user,
94+
browser=text,
95+
)
96+
self.assertNotEqual(log.browser, text)
97+
self.assertTrue(log.browser.endswith(" - Truncated"))

0 commit comments

Comments
 (0)