Skip to content

Commit

Permalink
Refactor protobuf validation exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Aug 6, 2021
1 parent 570b4d7 commit 0762a93
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.whispersystems.libsignal.protocol.DecryptionErrorMessage;
import org.whispersystems.libsignal.state.SignalProtocolStore;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.InvalidMessageStructureException;
import org.whispersystems.signalservice.api.crypto.ContentHint;
import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
import org.whispersystems.signalservice.api.messages.SignalServiceContent;
Expand Down Expand Up @@ -110,7 +111,7 @@ private MessageDecryptionUtil() {}
} catch (ProtocolDuplicateMessageException e) {
Log.w(TAG, String.valueOf(envelope.getTimestamp()), e);
return DecryptionResult.forError(MessageState.DUPLICATE_MESSAGE, toExceptionMetadata(e), jobs);
} catch (InvalidMetadataVersionException | InvalidMetadataMessageException e) {
} catch (InvalidMetadataVersionException | InvalidMetadataMessageException | InvalidMessageStructureException e) {
Log.w(TAG, String.valueOf(envelope.getTimestamp()), e);
return DecryptionResult.forNoop(jobs);
} catch (SelfSendException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.whispersystems.signalservice.api;

import org.whispersystems.libsignal.util.guava.Optional;

/**
* An exception thrown when something about the proto is malformed. e.g. one of the fields has an invalid value.
*/
public final class InvalidMessageStructureException extends Exception {

private final Optional<String> sender;
private final Optional<Integer> device;

public InvalidMessageStructureException(String message) {
super(message);
this.sender = Optional.absent();
this.device = Optional.absent();
}

public InvalidMessageStructureException(String message, String sender, int device) {
super(message);
this.sender = Optional.fromNullable(sender);
this.device = Optional.of(device);
}

public InvalidMessageStructureException(Exception e, String sender, int device) {
super(e);
this.sender = Optional.fromNullable(sender);
this.device = Optional.of(device);
}

public InvalidMessageStructureException(Exception e) {
super(e);
this.sender = Optional.absent();
this.device = Optional.absent();
}

public Optional<String> getSender() {
return sender;
}

public Optional<Integer> getDevice() {
return device;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.whispersystems.libsignal.protocol.SignalMessage;
import org.whispersystems.libsignal.state.SignalProtocolStore;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.InvalidMessageStructureException;
import org.whispersystems.signalservice.api.SignalSessionLock;
import org.whispersystems.signalservice.api.messages.SignalServiceContent;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
Expand Down Expand Up @@ -135,7 +136,7 @@ public SignalServiceContent decrypt(SignalServiceEnvelope envelope)
ProtocolUntrustedIdentityException, ProtocolNoSessionException,
ProtocolInvalidVersionException, ProtocolInvalidMessageException,
ProtocolInvalidKeyException, ProtocolDuplicateMessageException,
SelfSendException, UnsupportedDataMessageException
SelfSendException, UnsupportedDataMessageException, InvalidMessageStructureException
{
try {
if (envelope.hasLegacyMessage()) {
Expand Down Expand Up @@ -174,15 +175,15 @@ private Plaintext decrypt(SignalServiceEnvelope envelope, byte[] ciphertext)
ProtocolLegacyMessageException, ProtocolInvalidKeyException,
ProtocolInvalidVersionException, ProtocolInvalidMessageException,
ProtocolInvalidKeyIdException, ProtocolNoSessionException,
SelfSendException
SelfSendException, InvalidMessageStructureException
{
try {

byte[] paddedMessage;
SignalServiceMetadata metadata;

if (!envelope.hasSource() && !envelope.isUnidentifiedSender()) {
throw new ProtocolInvalidMessageException(new InvalidMessageException("Non-UD envelope is missing a source!"), null, 0);
throw new InvalidMessageStructureException("Non-UD envelope is missing a source!");
}

if (envelope.isPreKeySignalMessage()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.signal.libsignal.metadata.ProtocolInvalidMessageException;
import org.whispersystems.libsignal.InvalidMessageException;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.InvalidMessageStructureException;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos.AttachmentPointer;

/**
Expand Down Expand Up @@ -43,14 +44,14 @@ public String toString() {
}
}

public static SignalServiceAttachmentRemoteId from(AttachmentPointer attachmentPointer) throws ProtocolInvalidMessageException {
public static SignalServiceAttachmentRemoteId from(AttachmentPointer attachmentPointer) throws InvalidMessageStructureException {
switch (attachmentPointer.getAttachmentIdentifierCase()) {
case CDNID:
return new SignalServiceAttachmentRemoteId(attachmentPointer.getCdnId());
case CDNKEY:
return new SignalServiceAttachmentRemoteId(attachmentPointer.getCdnKey());
case ATTACHMENTIDENTIFIER_NOT_SET:
throw new ProtocolInvalidMessageException(new InvalidMessageException("AttachmentPointer CDN location not set"), null, 0);
throw new InvalidMessageStructureException("AttachmentPointer CDN location not set");
}
return null;
}
Expand Down

0 comments on commit 0762a93

Please sign in to comment.