1
0
mirror of https://github.com/google/nomulus synced 2026-04-23 17:50:49 +00:00

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 a9b35c163d
commit a299df3005
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());