Add fallback for Spec11 ThreatMatch parser (#1806)

This commit is contained in:
Pavlo Tkach 2022-10-06 09:54:43 -04:00 committed by GitHub
parent fe3472d31d
commit b83c970af4
2 changed files with 25 additions and 1 deletions

View file

@ -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));
}
}

View file

@ -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<RegistrarThreatMatches> sampleThreatMatches() throws Exception {
return ImmutableSet.of(getMatchA(), getMatchB());