Skip to content

Commit 015ad42

Browse files
committed
fix: use StandardCharsets.UTF_8 for byte array to string conversions
1 parent 3b6ed46 commit 015ad42

5 files changed

Lines changed: 22 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- fix: replace assert statements with explicit null checks in `Federation` class to ensure validation is not bypassed when assertions are disabled.
88
- fix: add overflow check in `TimeBounds.expiresAfter()` to prevent integer overflow when timeout is too large.
99
- fix: add validation for `ManageDataOperation` value length to ensure it does not exceed 64 bytes.
10+
- fix: use `StandardCharsets.UTF_8` explicitly when converting byte arrays to strings to ensure consistent behavior across different platforms.
1011

1112
## 2.2.1
1213

src/main/java/org/stellar/sdk/Sep10Challenge.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.stellar.sdk;
22

33
import java.math.BigInteger;
4+
import java.nio.charset.StandardCharsets;
45
import java.security.SecureRandom;
56
import java.util.ArrayList;
67
import java.util.Arrays;
@@ -316,7 +317,9 @@ public static ChallengeTransaction readChallengeTransaction(
316317

317318
byte[] nonce;
318319
try {
319-
nonce = Base64Factory.getInstance().decode(new String(manageDataOperation.getValue()));
320+
nonce =
321+
Base64Factory.getInstance()
322+
.decode(new String(manageDataOperation.getValue(), StandardCharsets.UTF_8));
320323
} catch (IllegalArgumentException e) {
321324
throw new InvalidSep10ChallengeException(
322325
"Failed to decode random nonce provided in ManageData operation.", e);

src/main/java/org/stellar/sdk/Sep45Challenge.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.stellar.sdk;
22

33
import java.io.IOException;
4+
import java.nio.charset.StandardCharsets;
45
import java.security.SecureRandom;
56
import java.util.ArrayList;
67
import java.util.Arrays;
@@ -371,25 +372,25 @@ public static ChallengeAuthorizationEntries readChallengeAuthorizationEntries(
371372

372373
switch (key) {
373374
case "account":
374-
account = new String(Scv.fromString(valueVal));
375+
account = new String(Scv.fromString(valueVal), StandardCharsets.UTF_8);
375376
break;
376377
case "home_domain":
377-
homeDomain = new String(Scv.fromString(valueVal));
378+
homeDomain = new String(Scv.fromString(valueVal), StandardCharsets.UTF_8);
378379
break;
379380
case "nonce":
380-
nonce = new String(Scv.fromString(valueVal));
381+
nonce = new String(Scv.fromString(valueVal), StandardCharsets.UTF_8);
381382
break;
382383
case "web_auth_domain":
383-
webAuthDomainValue = new String(Scv.fromString(valueVal));
384+
webAuthDomainValue = new String(Scv.fromString(valueVal), StandardCharsets.UTF_8);
384385
break;
385386
case "web_auth_domain_account":
386-
webAuthDomainAccount = new String(Scv.fromString(valueVal));
387+
webAuthDomainAccount = new String(Scv.fromString(valueVal), StandardCharsets.UTF_8);
387388
break;
388389
case "client_domain":
389-
clientDomain = new String(Scv.fromString(valueVal));
390+
clientDomain = new String(Scv.fromString(valueVal), StandardCharsets.UTF_8);
390391
break;
391392
case "client_domain_account":
392-
clientDomainAccount = new String(Scv.fromString(valueVal));
393+
clientDomainAccount = new String(Scv.fromString(valueVal), StandardCharsets.UTF_8);
393394
break;
394395
default:
395396
// Unknown argument, ignore

src/main/java/org/stellar/sdk/Util.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.math.BigInteger;
5+
import java.nio.charset.StandardCharsets;
56
import java.security.MessageDigest;
67
import java.security.NoSuchAlgorithmException;
78
import java.util.Arrays;
@@ -95,7 +96,7 @@ public static byte[] paddedByteArray(String string, int length) {
9596
* @return trimmed byte array
9697
*/
9798
static String paddedByteArrayToString(byte[] bytes) {
98-
String[] strArray = new String(bytes).split("\0");
99+
String[] strArray = new String(bytes, StandardCharsets.UTF_8).split("\0");
99100
if (strArray.length > 0) {
100101
return strArray[0];
101102
}

src/main/java/org/stellar/sdk/operations/AllowTrustOperation.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.stellar.sdk.operations;
22

3+
import java.nio.charset.StandardCharsets;
34
import lombok.AllArgsConstructor;
45
import lombok.EqualsAndHashCode;
56
import lombok.Getter;
@@ -57,10 +58,14 @@ public static AllowTrustOperation fromXdr(AllowTrustOp op) {
5758
String assetCode;
5859
switch (op.getAsset().getDiscriminant()) {
5960
case ASSET_TYPE_CREDIT_ALPHANUM4:
60-
assetCode = new String(op.getAsset().getAssetCode4().getAssetCode4()).trim();
61+
assetCode =
62+
new String(op.getAsset().getAssetCode4().getAssetCode4(), StandardCharsets.UTF_8)
63+
.trim();
6164
break;
6265
case ASSET_TYPE_CREDIT_ALPHANUM12:
63-
assetCode = new String(op.getAsset().getAssetCode12().getAssetCode12()).trim();
66+
assetCode =
67+
new String(op.getAsset().getAssetCode12().getAssetCode12(), StandardCharsets.UTF_8)
68+
.trim();
6469
break;
6570
default:
6671
throw new IllegalArgumentException("Unknown asset code");

0 commit comments

Comments
 (0)