remove control and format characters from paynym names, and omit directory entries whose payment code has no valid key
What changed, and why it matters
This commit hardens how Sparrow Wallet handles PayNym contacts. It strips invisible or display-manipulating characters from contact names, and it drops directory entries whose payment codes contain invalid cryptographic keys instead of keeping them. These changes reduce the risk of a malicious or malformed PayNym entry tricking the user or causing downstream errors.
Treat as a defensive hardening patch. Review whether the same sanitization should be applied to any other user-facing name fields in the PayNym/contact UI, and verify that payment-code validation is consistently enforced wherever PayNym objects are constructed.
Security signals we found
Sanitization of Unicode control/format characters in user-supplied display names
Eager validation of BIP47 payment-code public key via notification-address derivation
Omission of malformed/invalid directory entries rather than failing the whole contact list
Addition of unit tests demonstrating the intended security-relevant behavior
Evidence from the diff
The patch makes two defensive changes in the PayNym (BIP47 payment-code contact) code path. First, PayNym names are sanitized with a regex that removes Unicode control (Cc) and format (Cf) characters, then trimmed; if the result is empty, the abbreviated payment code is used as the name. Second, PayNym.fromString now eagerly computes the notification address, which causes IllegalArgumentException to be thrown for payment codes whose public key is not on the secp256k1 curve; that exception is caught and causes the entry to be omitted. PayNymService’s directory parsing comment is updated to reflect the new behavior. Unit tests cover both the character stripping and the invalid-key omission cases.
Changed components
src/main/java/com/sparrowwallet/sparrow/paynym/PayNym.javasrc/main/java/com/sparrowwallet/sparrow/paynym/PayNymService.javasrc/test/java/com/sparrowwallet/sparrow/paynym/PayNymTest.javaInspect captured patch +42 / −5
### src/main/java/com/sparrowwallet/sparrow/paynym/PayNym.java
@@ -1,5 +1,6 @@
package com.sparrowwallet.sparrow.paynym;
+import com.sparrowwallet.drongo.address.Address;
import com.sparrowwallet.drongo.bip47.InvalidPaymentCodeException;
import com.sparrowwallet.drongo.bip47.PaymentCode;
import com.sparrowwallet.drongo.protocol.ScriptType;
@@ -25,11 +26,13 @@ public class PayNym {
private final List<PayNym> followers;
private boolean collaborativeSend;
+ private Address notificationAddress;
public PayNym(PaymentCode paymentCode, String nymId, String nymName, boolean segwit, List<PayNym> following, List<PayNym> followers) {
this.paymentCode = paymentCode;
this.nymId = nymId;
- this.nymName = nymName;
+ String strippedName = nymName == null ? null : nymName.replaceAll("[\\p{Cc}\\p{Cf}]", "").trim();
+ this.nymName = strippedName != null && strippedName.isEmpty() ? paymentCode.toAbbreviatedString() : strippedName;
this.segwit = segwit;
this.following = following;
this.followers = followers;
@@ -39,6 +42,14 @@ public PaymentCode paymentCode() {
return paymentCode;
}
+ public Address notificationAddress() {
+ if(notificationAddress == null) {
+ notificationAddress = paymentCode.getNotificationAddress();
+ }
+
+ return notificationAddress;
+ }
+
public String nymId() {
return nymId;
}
@@ -73,8 +84,10 @@ public static List<ScriptType> getV1ScriptTypes() {
public static Optional<PayNym> fromString(String strPaymentCode, String nymId, String nymName, boolean segwit, List<PayNym> following, List<PayNym> followers) {
try {
- return Optional.of(new PayNym(new PaymentCode(strPaymentCode), nymId, nymName, segwit, following, followers));
- } catch(InvalidPaymentCodeException e) {
+ PayNym payNym = new PayNym(new PaymentCode(strPaymentCode), nymId, nymName, segwit, following, followers);
+ payNym.notificationAddress();
+ return Optional.of(payNym);
+ } catch(InvalidPaymentCodeException | IllegalArgumentException e) {
log.error("Error creating PayNym from payment code " + strPaymentCode, e);
return Optional.empty();
}
### src/main/java/com/sparrowwallet/sparrow/paynym/PayNymService.java
@@ -1,5 +1,6 @@
package com.sparrowwallet.sparrow.paynym;
+import com.sparrowwallet.drongo.address.Address;
import com.sparrowwallet.drongo.bip47.InvalidPaymentCodeException;
import com.sparrowwallet.drongo.bip47.PaymentCode;
import com.sparrowwallet.drongo.crypto.ChildNumber;
@@ -203,7 +204,8 @@ public static Observable<PayNym> getPayNym(String nymIdentifier, boolean compact
//A payment code identifies itself, so a PayNym looked up by payment code must share its key and chain code, differing at most in the segwit feature bit
PaymentCode requestedCode = getRequestedPaymentCode(nymIdentifier);
- if(requestedCode != null && !requestedCode.getNotificationAddress().equals(code.getNotificationAddress())) {
+ Address notificationAddress = code.getNotificationAddress();
+ if(requestedCode != null && !requestedCode.getNotificationAddress().equals(notificationAddress)) {
throw new IllegalStateException("PayNym server returned payment code " + code + " for requested payment code " + requestedCode);
}
@@ -212,7 +214,7 @@ public static Observable<PayNym> getPayNym(String nymIdentifier, boolean compact
}
List<Map<String, Object>> followingMaps = (List<Map<String, Object>>)nymMap.get("following");
- //An entry whose payment code does not parse is omitted rather than failing the whole PayNym
+ //An entry whose payment code does not parse or has no valid key is omitted rather than failing the whole PayNym
List<PayNym> following = followingMaps.stream().flatMap(followingMap -> {
return PayNym.fromString((String)followingMap.get("code"), (String)followingMap.get("nymId"), (String)followingMap.get("nymName"), (Boolean)followingMap.get("segwit"), Collections.emptyList(), Collections.emptyList()).stream();
}).collect(Collectors.toList());
### src/test/java/com/sparrowwallet/sparrow/paynym/PayNymTest.java
@@ -30,4 +30,26 @@ public void omitsAnUnparseablePaymentCode() {
String corrupted = PAYMENT_CODE.substring(0, PAYMENT_CODE.length() - 1) + (PAYMENT_CODE.endsWith("A") ? "B" : "A");
assertTrue(PayNym.fromString(corrupted, "id", "+name", true, Collections.emptyList(), Collections.emptyList()).isEmpty());
}
+
+ @Test
+ public void omitsAPaymentCodeWithAnInvalidKey() {
+ //Parses and passes its checksum, but the key's x coordinate is not on the curve
+ String invalidKey = "PM8TJWnHNrXcofmpzddV4aoaZMMN3C7QhbymGxWYBQVKjs1fochBZk1dZ7ZnoqMkg2YwtACR3eRMipK7KqvQ6cG9dyc2eLFuwsA8SSuUHxRERKBFNx5G";
+ assertTrue(PayNym.fromString(invalidKey, "id", "+name", true, Collections.emptyList(), Collections.emptyList()).isEmpty());
+ }
+
+ @Test
+ public void removesControlCharactersFromName() {
+ PayNym payNym = PayNym.fromString(PAYMENT_CODE, "id", " +name\u0000\u001f\u007f\u0085 ", true, Collections.emptyList(), Collections.emptyList()).orElseThrow();
+ assertEquals("+name", payNym.nymName());
+
+ PayNym formatted = PayNym.fromString(PAYMENT_CODE, "id", "\u202e+na\u200bme\ufeff\u2066", true, Collections.emptyList(), Collections.emptyList()).orElseThrow();
+ assertEquals("+name", formatted.nymName());
+ }
+
+ @Test
+ public void nameOfOnlyControlCharactersUsesPaymentCode() {
+ PayNym payNym = PayNym.fromString(PAYMENT_CODE, "id", "\u0007\u0008", true, Collections.emptyList(), Collections.emptyList()).orElseThrow();
+ assertEquals(payNym.paymentCode().toAbbreviatedString(), payNym.nymName());
+ }
}Why this scored 50/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.