Skip to content

Commit

Permalink
Bytes.ofHex can fail
Browse files Browse the repository at this point in the history
If the input is odd number of elements or not hex alphabet
  • Loading branch information
singpolyma committed Oct 29, 2024
1 parent ffe951f commit e327dff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
44 changes: 23 additions & 21 deletions snikket/Client.hx
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class Client extends EventEmitter {
if (pubsubEvent != null && pubsubEvent.getFrom() != null && pubsubEvent.getNode() == "urn:xmpp:avatar:metadata" && pubsubEvent.getItems().length > 0) {
final item = pubsubEvent.getItems()[0];
final avatarSha1Hex = pubsubEvent.getItems()[0].attr.get("id");
final avatarSha1 = Bytes.ofHex(avatarSha1Hex).getData();
final avatarSha1 = Hash.fromHex("sha-1", avatarSha1Hex)?.hash;
final metadata = item.getChild("metadata", "urn:xmpp:avatar:metadata");
var mime = "image/png";
if (metadata != null) {
Expand All @@ -262,26 +262,28 @@ class Client extends EventEmitter {
mime = info.attr.get("type");
}
}
final chat = this.getDirectChat(JID.parse(pubsubEvent.getFrom()).asBare().asString(), false);
chat.setAvatarSha1(avatarSha1);
persistence.storeChat(accountId(), chat);
persistence.hasMedia("sha-1", avatarSha1, (has) -> {
if (has) {
this.trigger("chats/update", [chat]);
} else {
final pubsubGet = new PubsubGet(pubsubEvent.getFrom(), "urn:xmpp:avatar:data", avatarSha1Hex);
pubsubGet.onFinished(() -> {
final item = pubsubGet.getResult()[0];
if (item == null) return;
final dataNode = item.getChild("data", "urn:xmpp:avatar:data");
if (dataNode == null) return;
persistence.storeMedia(mime, Base64.decode(StringTools.replace(dataNode.getText(), "\n", "")).getData(), () -> {
this.trigger("chats/update", [chat]);
if (avatarSha1 != null) {
final chat = this.getDirectChat(JID.parse(pubsubEvent.getFrom()).asBare().asString(), false);
chat.setAvatarSha1(avatarSha1);
persistence.storeChat(accountId(), chat);
persistence.hasMedia("sha-1", avatarSha1, (has) -> {
if (has) {
this.trigger("chats/update", [chat]);
} else {
final pubsubGet = new PubsubGet(pubsubEvent.getFrom(), "urn:xmpp:avatar:data", avatarSha1Hex);
pubsubGet.onFinished(() -> {
final item = pubsubGet.getResult()[0];
if (item == null) return;
final dataNode = item.getChild("data", "urn:xmpp:avatar:data");
if (dataNode == null) return;
persistence.storeMedia(mime, Base64.decode(StringTools.replace(dataNode.getText(), "\n", "")).getData(), () -> {
this.trigger("chats/update", [chat]);
});
});
});
sendQuery(pubsubGet);
}
});
sendQuery(pubsubGet);
}
});
}
}

if (pubsubEvent != null && pubsubEvent.getFrom() != null && JID.parse(pubsubEvent.getFrom()).asBare().asString() == accountId() && pubsubEvent.getNode() == "http://jabber.org/protocol/nick" && pubsubEvent.getItems().length > 0) {
Expand Down Expand Up @@ -439,7 +441,7 @@ class Client extends EventEmitter {
if (from.isBare()) {
final avatarSha1Hex = stanza.findText("{vcard-temp:x:update}x/photo#");
if (avatarSha1Hex != null) {
final avatarSha1 = Bytes.ofHex(avatarSha1Hex).getData();
final avatarSha1 = Hash.fromHex("sha-1", avatarSha1Hex)?.hash;
chat.setAvatarSha1(avatarSha1);
persistence.storeChat(accountId(), chat);
persistence.hasMedia("sha-1", avatarSha1, (has) -> {
Expand Down
8 changes: 6 additions & 2 deletions snikket/Hash.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ class Hash {
this.hash = hash;
}

public static function fromHex(algorithm: String, hash: String) {
return new Hash(algorithm, Bytes.ofHex(hash).getData());
public static function fromHex(algorithm: String, hash: String): Null<Hash> {
try {
return new Hash(algorithm, Bytes.ofHex(hash).getData());
} catch (e) {
return null;
}
}

public static function fromUri(uri: String): Null<Hash> {
Expand Down

0 comments on commit e327dff

Please sign in to comment.