Skip to content

Commit

Permalink
encode certain characters that Cinder chokes on (#21866)
Browse files Browse the repository at this point in the history
  • Loading branch information
eviljeff committed Feb 15, 2024
1 parent ffd0037 commit eb06c8a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 49 deletions.
15 changes: 10 additions & 5 deletions src/olympia/abuse/cinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CinderEntity:
type = None # Needs to be defined by subclasses
# Number of relationships to send by default in each Cinder request.
RELATIONSHIPS_BATCH_SIZE = 25
CINDER_PROHBITED_FIRST_CHARACTERS = {'@': r'\@', '-': r'\-', '=': r'\=', '_': r'\_'}

@property
def queue(self):
Expand All @@ -31,7 +32,11 @@ def id(self):
return self.get_str(self.get_attributes().get('id', ''))

def get_str(self, field_content):
return str(field_content or '')
out = str(field_content or '').strip()
return out and (
out[0].translate(str.maketrans(self.CINDER_PROHBITED_FIRST_CHARACTERS))
+ out[1:]
)

def get_attributes(self):
raise NotImplementedError
Expand Down Expand Up @@ -93,7 +98,7 @@ def build_report_payload(self, *, report, reporter):
'queue_slug': self.queue,
'entity_type': self.type,
'entity': entity_attributes,
'reasoning': message,
'reasoning': self.get_str(message),
'context': context,
}

Expand Down Expand Up @@ -134,7 +139,7 @@ def appeal(self, *, decision_id, appeal_text, appealer):
'queue_slug': self.queue,
'appealer_entity_type': appealer.type,
'appealer_entity': appealer.get_attributes(),
'reasoning': appeal_text,
'reasoning': self.get_str(appeal_text),
'decision_to_appeal_id': decision_id,
}
response = requests.post(url, json=data, headers=self.get_cinder_http_headers())
Expand All @@ -152,7 +157,7 @@ def create_decision(self, *, reasoning, policy_uuids):
'queue_slug': self.queue,
'entity_type': self.type,
'entity': self.get_attributes(),
'reasoning': reasoning,
'reasoning': self.get_str(reasoning),
'policy_uuids': policy_uuids,
}
response = requests.post(url, json=data, headers=self.get_cinder_http_headers())
Expand Down Expand Up @@ -478,7 +483,7 @@ def get_attributes(self):
'reason': self.abuse_report.get_reason_display()
if self.abuse_report.reason
else None,
'message': self.abuse_report.message,
'message': self.get_str(self.abuse_report.message),
'locale': self.abuse_report.application_locale,
# We need a boolean to expose specifically if the reporter
# considered the content illegal, as that needs to be reflected in
Expand Down

0 comments on commit eb06c8a

Please sign in to comment.