Skip to content

Commit

Permalink
Update CDF validator to accept x.com as a valid Twitter URL.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 636856706
  • Loading branch information
azuser authored and civics-copybara committed May 24, 2024
1 parent c2466ec commit 0cd6160
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
3 changes: 2 additions & 1 deletion rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2100,7 +2100,8 @@ def check_url(self, uri, annotation, platform):
parsed_url = urlparse(url)
# Ensure media platform name is in URL.
if (platform != "website" and platform not in parsed_url.netloc and
not (platform == "facebook" and "fb.com" in parsed_url.netloc)):
not (platform == "facebook" and "fb.com" in parsed_url.netloc) and

Check failure

Code scanning / CodeQL

Incomplete URL substring sanitization High

The string
fb.com
may be at an arbitrary position in the sanitized URL.
not (platform == "twitter" and "x.com" in parsed_url.netloc)):

Check failure

Code scanning / CodeQL

Incomplete URL substring sanitization High

The string
x.com
may be at an arbitrary position in the sanitized URL.
# Note that the URL is encoded for printing purposes
raise loggers.ElectionError.from_message(
"Annotation '{}' is incorrect for URI {}.".format(
Expand Down
8 changes: 4 additions & 4 deletions samples/officeholder_sample_feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
</InternationalizedAbbreviation>
<ContactInformation>
<Uri Annotation="wikipedia">https://en.wikipedia.org/wiki/Republican_Party_(United_States)</Uri>
<Uri Annotation="official-twitter">https://twitter.com/GOP</Uri>
<Uri Annotation="official-twitter">https://x.com/GOP</Uri>
</ContactInformation>
<ExternalIdentifiers>
<ExternalIdentifier>
Expand All @@ -162,7 +162,7 @@
</InternationalizedAbbreviation>
<ContactInformation>
<Uri Annotation="wikipedia">https://en.wikipedia.org/wiki/Democratic_Party_(United_States)</Uri>
<Uri Annotation="official-twitter">https://twitter.com/TheDemocrats</Uri>
<Uri Annotation="official-twitter">https://x.com/TheDemocrats</Uri>
</ContactInformation>
<ExternalIdentifiers>
<ExternalIdentifier>
Expand Down Expand Up @@ -199,7 +199,7 @@
</ContactInformation>
<ContactInformation>
<Uri Annotation="personal-facebook">https://www.facebook.com/tedcruzpage</Uri>
<Uri Annotation="personal-twitter">https://twitter.com/tedcruz</Uri>
<Uri Annotation="personal-twitter">https://x.com/tedcruz</Uri>
<Uri Annotation="personal-website">https://www.tedcruz.org/</Uri>
<Uri Annotation="wikipedia">https://en.wikipedia.org/wiki/Ted_Cruz</Uri>
</ContactInformation>
Expand Down Expand Up @@ -247,7 +247,7 @@
<!-- Party leader referenced by party-leader-id. Does not require a linked office. -->
<ContactInformation>
<Uri Annotation="personal-facebook">https://www.facebook.com/leaderofparty</Uri>
<Uri Annotation="personal-twitter">https://twitter.com/partyleader</Uri>
<Uri Annotation="personal-twitter">https://x.com/partyleader</Uri>
<Uri Annotation="personal-website">https://www.partyleader.org/</Uri>
<Uri Annotation="wikipedia">https://en.wikipedia.org/wiki/Party_Leader</Uri>
</ContactInformation>
Expand Down
2 changes: 1 addition & 1 deletion samples/post_election_sample_feed_precincts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7810,7 +7810,7 @@
<Phone annotation="TDD">202-456-6213</Phone>
<Uri Annotation="official-website">http://www.whitehouse.gov</Uri>
<Uri Annotation="official-facebook">https://www.facebook.com/WhiteHouse</Uri>
<Uri Annotation="official-twitter">https://twitter.com/WhiteHouse</Uri>
<Uri Annotation="official-twitter">https://x.com/WhiteHouse</Uri>
</ContactInformation>
<ExternalIdentifiers>
<ExternalIdentifier>
Expand Down
2 changes: 1 addition & 1 deletion samples/post_election_sample_feed_summary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,7 @@
<Phone annotation="TDD">202-456-6213</Phone>
<Uri Annotation="official-website">http://www.whitehouse.gov</Uri>
<Uri Annotation="official-facebook">https://www.facebook.com/WhiteHouse</Uri>
<Uri Annotation="official-twitter">https://twitter.com/WhiteHouse</Uri>
<Uri Annotation="official-twitter">https://x.com/WhiteHouse</Uri>
</ContactInformation>
<ExternalIdentifiers>
<ExternalIdentifier>
Expand Down
2 changes: 1 addition & 1 deletion samples/pre_election_sample_feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@
<Phone annotation="TDD">202-456-6213</Phone>
<Uri Annotation="official-website">http://www.whitehouse.gov</Uri>
<Uri Annotation="official-facebook">https://www.facebook.com/WhiteHouse</Uri>
<Uri Annotation="official-twitter">https://twitter.com/WhiteHouse</Uri>
<Uri Annotation="official-twitter">https://x.com/WhiteHouse</Uri>
</ContactInformation>
<ExternalIdentifiers>
<ExternalIdentifier>
Expand Down
27 changes: 27 additions & 0 deletions tests/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5791,6 +5791,33 @@ def testIncorrectFBAnnotationFails(self):
("'official-fb' is not a valid annotation."))
self.assertEqual(cm.exception.log_entry[0].elements[0].tag, "Uri")

def testXAnnotation(self):
root_string = """
<ContactInformation label="ci_par_at_1">
<Uri Annotation="personal-twitter">
<![CDATA[https://www.x.com/juanjomalvinas]]>
</Uri>
</ContactInformation>
"""
self.valid_annotation.check(etree.fromstring(root_string))

def testIncorrectXAnnotationFails(self):
root_string = """
<ContactInformation label="ci_par_at_1">
<Uri Annotation="official-x">
<![CDATA[https://www.x.com]]>
</Uri>
<Uri Annotation="personal-x">
<![CDATA[http://www.twitter.com]]>
</Uri>
</ContactInformation>
"""
with self.assertRaises(loggers.ElectionWarning) as cm:
self.valid_annotation.check(etree.fromstring(root_string))
self.assertEqual(cm.exception.log_entry[0].message,
("'official-x' is not a valid annotation."))
self.assertEqual(cm.exception.log_entry[0].elements[0].tag, "Uri")


class OfficesHaveJurisdictionIDTest(absltest.TestCase):

Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
See https://packaging.python.org/guides/single-sourcing-package-version/
"""

__version__ = '1.43.dev7'
__version__ = '1.43.dev8'

0 comments on commit 0cd6160

Please sign in to comment.