-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put some validation on to profile version strings
Co-authored-by: Jon Chambers <[email protected]>
- Loading branch information
1 parent
ca05df5
commit 36e7772
Showing
3 changed files
with
156 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
service/src/main/java/org/whispersystems/textsecuregcm/util/ValidHexString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2024 Signal Messenger, LLC | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
package org.whispersystems.textsecuregcm.util; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.util.HexFormat; | ||
import java.util.Objects; | ||
import javax.validation.Constraint; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import javax.validation.Payload; | ||
|
||
/** | ||
* Constraint annotation that requires annotated entity is a valid hex encoded string. | ||
*/ | ||
@Target({ FIELD, PARAMETER, METHOD }) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = ValidHexString.Validator.class) | ||
@Documented | ||
public @interface ValidHexString { | ||
|
||
String message() default "value is not a valid hex string"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
|
||
class Validator implements ConstraintValidator<ValidHexString, String> { | ||
|
||
@Override | ||
public boolean isValid(final String value, final ConstraintValidatorContext context) { | ||
if (Objects.isNull(value)) { | ||
return true; | ||
} | ||
try { | ||
HexFormat.of().parseHex(value); | ||
return true; | ||
} catch (IllegalArgumentException e) { | ||
return false; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.