From b83c970af432a9e2509753214872b529e49c0f0e Mon Sep 17 00:00:00 2001 From: Pavlo Tkach <3469726+ptkach@users.noreply.github.com> Date: Thu, 6 Oct 2022 09:54:43 -0400 Subject: [PATCH] Add fallback for Spec11 ThreatMatch parser (#1806) --- .../registry/beam/spec11/ThreatMatch.java | 8 +++++++- ...Spec11RegistrarThreatMatchesParserTest.java | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/google/registry/beam/spec11/ThreatMatch.java b/core/src/main/java/google/registry/beam/spec11/ThreatMatch.java index 1ef50f6a0..f4651cc87 100644 --- a/core/src/main/java/google/registry/beam/spec11/ThreatMatch.java +++ b/core/src/main/java/google/registry/beam/spec11/ThreatMatch.java @@ -26,6 +26,7 @@ public abstract class ThreatMatch implements Serializable { private static final String THREAT_TYPE_FIELD = "threatType"; private static final String DOMAIN_NAME_FIELD = "domainName"; + private static final String OUTDATED_NAME_FIELD = "fullyQualifiedDomainName"; /** Returns what kind of threat it is (malware, phishing etc.) */ public abstract String threatType(); @@ -46,7 +47,12 @@ public abstract class ThreatMatch implements Serializable { /** Parses a {@link JSONObject} and returns an equivalent {@link ThreatMatch}. */ public static ThreatMatch fromJSON(JSONObject threatMatch) throws JSONException { + // TODO: delete OUTDATED_NAME_FIELD once we no longer process reports saved with + // fullyQualifiedDomainName in them, likely 2023 return new AutoValue_ThreatMatch( - threatMatch.getString(THREAT_TYPE_FIELD), threatMatch.getString(DOMAIN_NAME_FIELD)); + threatMatch.getString(THREAT_TYPE_FIELD), + threatMatch.has(OUTDATED_NAME_FIELD) + ? threatMatch.getString(OUTDATED_NAME_FIELD) + : threatMatch.getString(DOMAIN_NAME_FIELD)); } } diff --git a/core/src/test/java/google/registry/reporting/spec11/Spec11RegistrarThreatMatchesParserTest.java b/core/src/test/java/google/registry/reporting/spec11/Spec11RegistrarThreatMatchesParserTest.java index 80bf1b50c..34abaa169 100644 --- a/core/src/test/java/google/registry/reporting/spec11/Spec11RegistrarThreatMatchesParserTest.java +++ b/core/src/test/java/google/registry/reporting/spec11/Spec11RegistrarThreatMatchesParserTest.java @@ -100,6 +100,24 @@ public class Spec11RegistrarThreatMatchesParserTest { assertThat(objectWithExtraFields).isEqualTo(objectWithoutExtraFields); } + @Test + void testSuccess_worksWithOutdatedField() throws Exception { + ThreatMatch objectWithOutdatedField = + ThreatMatch.fromJSON( + new JSONObject( + ImmutableMap.of( + "threatType", "MALWARE", + "fullyQualifiedDomainName", "c.com"))); + ThreatMatch objectWithoutOutdatedFields = + ThreatMatch.fromJSON( + new JSONObject( + ImmutableMap.of( + "threatType", "MALWARE", + "domainName", "c.com"))); + + assertThat(objectWithOutdatedField).isEqualTo(objectWithoutOutdatedFields); + } + /** The expected contents of the sample spec11 report file */ public static ImmutableSet sampleThreatMatches() throws Exception { return ImmutableSet.of(getMatchA(), getMatchB());