mirror of
https://github.com/google/nomulus
synced 2026-05-31 20:16:31 +00:00
Address technical debt and improve safety in domain flows and models (#3065)
* Address technical debt and improve safety in domain flows and models - Addressed unhandled empty lists and swallowed exceptions in DomainFlowTmchUtils. - Improved null safety and immutability guarantees in Fee and LaunchPhase. - Applied defensive copying in FeeTransformResponseExtension. Note: This uses the forceEmptyToNull(nullToEmptyImmutableCopy(...)) pattern. This defensive copy ensures immutability, while forceEmptyToNull is required because JAXB will serialize an empty collection as an empty XML tag (which violates EPP XML schemas). Setting it to null ensures JAXB omits the tag entirely. - Corrected JAXB property suppression in FeeCheckResponseExtensionItemStdV1. * Add pr-polisher skill for automated PR pre-flight checks * Enhance pr-polisher with more GEMINI.md constraints Added checks for: - Incorrect @Nullable imports. - Unstatically imported utility methods (DateTimeUtils/CacheUtils). - Redundant transaction wrapping (tm().transact -> tm().reTransact). - Mutable collection instantiations (ArrayList/HashMap).
This commit is contained in:
@@ -57,6 +57,9 @@ public final class DomainFlowTmchUtils {
|
||||
public SignedMark verifySignedMarks(
|
||||
ImmutableList<AbstractSignedMark> signedMarks, String domainLabel, Instant now)
|
||||
throws EppException {
|
||||
if (signedMarks.isEmpty()) {
|
||||
throw new SignedMarksListEmptyException();
|
||||
}
|
||||
if (signedMarks.size() > 1) {
|
||||
throw new TooManySignedMarksException();
|
||||
}
|
||||
@@ -77,21 +80,21 @@ public final class DomainFlowTmchUtils {
|
||||
|
||||
public SignedMark verifyEncodedSignedMark(EncodedSignedMark encodedSignedMark, Instant now)
|
||||
throws EppException {
|
||||
if (!encodedSignedMark.getEncoding().equals("base64")) {
|
||||
if (!"base64".equals(encodedSignedMark.getEncoding())) {
|
||||
throw new Base64RequiredForEncodedSignedMarksException();
|
||||
}
|
||||
byte[] signedMarkData;
|
||||
try {
|
||||
signedMarkData = encodedSignedMark.getBytes();
|
||||
} catch (IllegalStateException e) {
|
||||
throw new SignedMarkEncodingErrorException();
|
||||
throw new SignedMarkEncodingErrorException(e);
|
||||
}
|
||||
|
||||
SignedMark signedMark;
|
||||
try {
|
||||
signedMark = unmarshalEpp(SignedMark.class, signedMarkData);
|
||||
} catch (EppException e) {
|
||||
throw new SignedMarkParsingErrorException();
|
||||
throw new SignedMarkParsingErrorException(e);
|
||||
}
|
||||
|
||||
if (SignedMarkRevocationList.get().isSmdRevoked(signedMark.getId(), now)) {
|
||||
@@ -101,22 +104,22 @@ public final class DomainFlowTmchUtils {
|
||||
try {
|
||||
tmchXmlSignature.verify(signedMarkData);
|
||||
} catch (CertificateExpiredException e) {
|
||||
throw new SignedMarkCertificateExpiredException();
|
||||
throw new SignedMarkCertificateExpiredException(e);
|
||||
} catch (CertificateNotYetValidException e) {
|
||||
throw new SignedMarkCertificateNotYetValidException();
|
||||
throw new SignedMarkCertificateNotYetValidException(e);
|
||||
} catch (CertificateRevokedException e) {
|
||||
throw new SignedMarkCertificateRevokedException();
|
||||
throw new SignedMarkCertificateRevokedException(e);
|
||||
} catch (CertificateSignatureException e) {
|
||||
throw new SignedMarkCertificateSignatureException();
|
||||
throw new SignedMarkCertificateSignatureException(e);
|
||||
} catch (SignatureException | XMLSignatureException e) {
|
||||
throw new SignedMarkSignatureException();
|
||||
throw new SignedMarkSignatureException(e);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new SignedMarkCertificateInvalidException();
|
||||
throw new SignedMarkCertificateInvalidException(e);
|
||||
} catch (IOException
|
||||
| MarshalException
|
||||
| SAXException
|
||||
| ParserConfigurationException e) {
|
||||
throw new SignedMarkParsingErrorException();
|
||||
throw new SignedMarkParsingErrorException(e);
|
||||
}
|
||||
|
||||
if (now.isBefore(signedMark.getCreationTime())) {
|
||||
@@ -181,6 +184,11 @@ public final class DomainFlowTmchUtils {
|
||||
public SignedMarkCertificateRevokedException() {
|
||||
super("Signed mark certificate was revoked");
|
||||
}
|
||||
|
||||
public SignedMarkCertificateRevokedException(Throwable cause) {
|
||||
this();
|
||||
initCause(cause);
|
||||
}
|
||||
}
|
||||
|
||||
/** Certificate used in signed mark signature has expired. */
|
||||
@@ -189,6 +197,11 @@ public final class DomainFlowTmchUtils {
|
||||
public SignedMarkCertificateNotYetValidException() {
|
||||
super("Signed mark certificate not yet valid");
|
||||
}
|
||||
|
||||
public SignedMarkCertificateNotYetValidException(Throwable cause) {
|
||||
this();
|
||||
initCause(cause);
|
||||
}
|
||||
}
|
||||
|
||||
/** Certificate used in signed mark signature has expired. */
|
||||
@@ -196,6 +209,11 @@ public final class DomainFlowTmchUtils {
|
||||
public SignedMarkCertificateExpiredException() {
|
||||
super("Signed mark certificate has expired");
|
||||
}
|
||||
|
||||
public SignedMarkCertificateExpiredException(Throwable cause) {
|
||||
this();
|
||||
initCause(cause);
|
||||
}
|
||||
}
|
||||
|
||||
/** Certificate parsing error, or possibly a bad provider or algorithm. */
|
||||
@@ -203,6 +221,11 @@ public final class DomainFlowTmchUtils {
|
||||
public SignedMarkCertificateInvalidException() {
|
||||
super("Signed mark certificate is invalid");
|
||||
}
|
||||
|
||||
public SignedMarkCertificateInvalidException(Throwable cause) {
|
||||
this();
|
||||
initCause(cause);
|
||||
}
|
||||
}
|
||||
|
||||
/** Invalid signature on a signed mark. */
|
||||
@@ -210,6 +233,11 @@ public final class DomainFlowTmchUtils {
|
||||
public SignedMarkCertificateSignatureException() {
|
||||
super("Signed mark certificate not signed by ICANN");
|
||||
}
|
||||
|
||||
public SignedMarkCertificateSignatureException(Throwable cause) {
|
||||
this();
|
||||
initCause(cause);
|
||||
}
|
||||
}
|
||||
|
||||
/** Invalid signature on a signed mark. */
|
||||
@@ -217,6 +245,11 @@ public final class DomainFlowTmchUtils {
|
||||
public SignedMarkSignatureException() {
|
||||
super("Signed mark signature is invalid");
|
||||
}
|
||||
|
||||
public SignedMarkSignatureException(Throwable cause) {
|
||||
this();
|
||||
initCause(cause);
|
||||
}
|
||||
}
|
||||
|
||||
/** Signed marks must be encoded. */
|
||||
@@ -226,6 +259,13 @@ public final class DomainFlowTmchUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/** Signed marks list cannot be empty. */
|
||||
static class SignedMarksListEmptyException extends RequiredParameterMissingException {
|
||||
public SignedMarksListEmptyException() {
|
||||
super("Signed marks list cannot be empty");
|
||||
}
|
||||
}
|
||||
|
||||
/** Only one signed mark is allowed per application. */
|
||||
static class TooManySignedMarksException extends ParameterValuePolicyErrorException {
|
||||
public TooManySignedMarksException() {
|
||||
@@ -245,6 +285,11 @@ public final class DomainFlowTmchUtils {
|
||||
public SignedMarkParsingErrorException() {
|
||||
super("Error while parsing encoded signed mark data");
|
||||
}
|
||||
|
||||
public SignedMarkParsingErrorException(Throwable cause) {
|
||||
this();
|
||||
initCause(cause);
|
||||
}
|
||||
}
|
||||
|
||||
/** Signed mark data is improperly encoded. */
|
||||
@@ -252,6 +297,11 @@ public final class DomainFlowTmchUtils {
|
||||
public SignedMarkEncodingErrorException() {
|
||||
super("Signed mark data is improperly encoded");
|
||||
}
|
||||
|
||||
public SignedMarkEncodingErrorException(Throwable cause) {
|
||||
this();
|
||||
initCause(cause);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
package google.registry.model.domain.fee;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
@@ -31,6 +30,13 @@ import java.time.Period;
|
||||
*/
|
||||
public class Fee extends BaseFee {
|
||||
|
||||
public static final ImmutableSet<String> FEE_EXTENSION_URIS =
|
||||
ImmutableSet.of(
|
||||
ServiceExtension.FEE_1_00.getUri(),
|
||||
ServiceExtension.FEE_0_12.getUri(),
|
||||
ServiceExtension.FEE_0_11.getUri(),
|
||||
ServiceExtension.FEE_0_6.getUri());
|
||||
|
||||
@Override
|
||||
public Fee clone() {
|
||||
return (Fee) super.clone();
|
||||
@@ -60,21 +66,15 @@ public class Fee extends BaseFee {
|
||||
private static Fee createWithCustomDescription(
|
||||
BigDecimal cost, FeeType type, boolean isPremium, String description) {
|
||||
Fee instance = new Fee();
|
||||
instance.cost = checkNotNull(cost);
|
||||
checkArgument(instance.cost.signum() >= 0, "Cost must be a non-negative number");
|
||||
instance.type = checkNotNull(type);
|
||||
checkArgumentNotNull(cost, "Cost cannot be null");
|
||||
checkArgument(cost.signum() >= 0, "Cost must be a non-negative number");
|
||||
instance.cost = cost;
|
||||
instance.type = type;
|
||||
instance.isPremium = isPremium;
|
||||
instance.description = description;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static final ImmutableSet<String> FEE_EXTENSION_URIS =
|
||||
ImmutableSet.of(
|
||||
ServiceExtension.FEE_1_00.getUri(),
|
||||
ServiceExtension.FEE_0_12.getUri(),
|
||||
ServiceExtension.FEE_0_11.getUri(),
|
||||
ServiceExtension.FEE_0_6.getUri());
|
||||
|
||||
/** Builder for {@link Fee}. */
|
||||
public static class Builder extends Buildable.Builder<Fee> {
|
||||
|
||||
|
||||
@@ -63,12 +63,12 @@ public class FeeTransformResponseExtension extends ImmutableObject implements Re
|
||||
}
|
||||
|
||||
public Builder setFees(List<Fee> fees) {
|
||||
getInstance().fees = fees;
|
||||
getInstance().fees = forceEmptyToNull(nullToEmptyImmutableCopy(fees));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setCredits(List<Credit> credits) {
|
||||
getInstance().credits = forceEmptyToNull(credits);
|
||||
getInstance().credits = forceEmptyToNull(nullToEmptyImmutableCopy(credits));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,13 +14,12 @@
|
||||
|
||||
package google.registry.model.domain.feestdv1;
|
||||
|
||||
import static google.registry.util.CollectionUtils.forceEmptyToNull;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.model.domain.Period;
|
||||
import google.registry.model.domain.fee.Fee;
|
||||
import google.registry.model.domain.fee.FeeCheckResponseExtensionItem;
|
||||
import google.registry.model.domain.fee.FeeQueryCommandExtensionItem.CommandName;
|
||||
import jakarta.xml.bind.annotation.XmlTransient;
|
||||
import jakarta.xml.bind.annotation.XmlType;
|
||||
|
||||
/** The version 1.0 response for a domain check on a single resource. */
|
||||
@@ -38,6 +37,7 @@ public class FeeCheckResponseExtensionItemStdV1 extends FeeCheckResponseExtensio
|
||||
* doesn't support "period".
|
||||
*/
|
||||
@Override
|
||||
@XmlTransient
|
||||
public Period getPeriod() {
|
||||
return super.getPeriod();
|
||||
}
|
||||
@@ -47,6 +47,7 @@ public class FeeCheckResponseExtensionItemStdV1 extends FeeCheckResponseExtensio
|
||||
* doesn't support "fee".
|
||||
*/
|
||||
@Override
|
||||
@XmlTransient
|
||||
public ImmutableList<Fee> getFees() {
|
||||
return super.getFees();
|
||||
}
|
||||
@@ -74,7 +75,7 @@ public class FeeCheckResponseExtensionItemStdV1 extends FeeCheckResponseExtensio
|
||||
|
||||
@Override
|
||||
public Builder setFees(ImmutableList<Fee> fees) {
|
||||
commandBuilder.setFee(forceEmptyToNull(ImmutableList.copyOf(fees)));
|
||||
commandBuilder.setFee(fees);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import google.registry.model.ImmutableObject;
|
||||
import jakarta.xml.bind.annotation.XmlAttribute;
|
||||
import jakarta.xml.bind.annotation.XmlValue;
|
||||
import java.util.Objects;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* The launch phase of the TLD being addressed by this command.
|
||||
@@ -46,7 +47,7 @@ import java.util.Objects;
|
||||
* sets it is the one that needs to make sure the domain isn't a trademark and that the fields are
|
||||
* correct.
|
||||
*/
|
||||
public class LaunchPhase extends ImmutableObject {
|
||||
public final class LaunchPhase extends ImmutableObject {
|
||||
|
||||
/**
|
||||
* The phase during which trademark holders can submit domain registrations with trademark
|
||||
@@ -70,6 +71,9 @@ public class LaunchPhase extends ImmutableObject {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/** Private no-arg constructor required for JAXB and to enforce immutability elsewhere. */
|
||||
private LaunchPhase() {}
|
||||
|
||||
@XmlValue String phase;
|
||||
|
||||
/**
|
||||
@@ -79,6 +83,7 @@ public class LaunchPhase extends ImmutableObject {
|
||||
* <p>This is currently unused, but is retained so that incoming XMLs that include a subphase can
|
||||
* have it be reflected back.
|
||||
*/
|
||||
@Nullable
|
||||
@XmlAttribute(name = "name")
|
||||
String subphase;
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright 2026 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.flows.domain;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.flows.domain.DomainFlowTmchUtils.SignedMarksListEmptyException;
|
||||
import google.registry.flows.domain.DomainFlowTmchUtils.SignedMarksMustBeEncodedException;
|
||||
import google.registry.flows.domain.DomainFlowTmchUtils.TooManySignedMarksException;
|
||||
import google.registry.model.smd.AbstractSignedMark;
|
||||
import java.time.Instant;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
class DomainFlowTmchUtilsTest {
|
||||
|
||||
private final DomainFlowTmchUtils tmchUtils = new DomainFlowTmchUtils(null);
|
||||
|
||||
@Test
|
||||
void test_verifySignedMarks_emptyList() {
|
||||
assertThrows(
|
||||
SignedMarksListEmptyException.class,
|
||||
() -> tmchUtils.verifySignedMarks(ImmutableList.of(), "example", Instant.now()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_verifySignedMarks_tooManyMarks() {
|
||||
AbstractSignedMark mark1 = Mockito.mock(AbstractSignedMark.class);
|
||||
AbstractSignedMark mark2 = Mockito.mock(AbstractSignedMark.class);
|
||||
assertThrows(
|
||||
TooManySignedMarksException.class,
|
||||
() ->
|
||||
tmchUtils.verifySignedMarks(ImmutableList.of(mark1, mark2), "example", Instant.now()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_verifySignedMarks_notEncoded() {
|
||||
AbstractSignedMark mark1 = Mockito.mock(AbstractSignedMark.class);
|
||||
assertThrows(
|
||||
SignedMarksMustBeEncodedException.class,
|
||||
() -> tmchUtils.verifySignedMarks(ImmutableList.of(mark1), "example", Instant.now()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user