Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dfe2456a authored by Brad Ebinger's avatar Brad Ebinger
Browse files

Validate SipMessages have a correct Via branch parameter

SIP messages are required to have a transaction ID, which is based
on the Via header's unique branch parameter. For processing, we require
this parameter to be set in order to send state updates about the message.

Also, as per council guidelines in b/184151053, throw a NPE instaed of
IllegalArgumentException for null params.

Fixes: 181087377
Test: atest CtsTelephonyTestCases
Change-Id: Ic4a637e780d04e9ac5d8ba01d7ba6dfae92307b1
parent dedc39bc
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -11988,7 +11988,7 @@ package android.telephony.ims {
    method @NonNull public byte[] getEncodedMessage();
    method @NonNull public String getHeaderSection();
    method @NonNull public String getStartLine();
    method @Nullable public String getViaBranchParameter();
    method @NonNull public String getViaBranchParameter();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.telephony.ims.SipMessage> CREATOR;
  }
+11 −7
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.annotation.SystemApi;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.android.internal.telephony.SipMessageParsingUtils;

@@ -60,14 +61,19 @@ public final class SipMessage implements Parcelable {
     */
    public SipMessage(@NonNull String startLine, @NonNull String headerSection,
            @NonNull byte[] content) {
        if (startLine == null || headerSection == null || content == null) {
            throw new IllegalArgumentException("One or more null parameters entered");
        }
        Objects.requireNonNull(startLine, "Required parameter is null: startLine");
        Objects.requireNonNull(headerSection, "Required parameter is null: headerSection");
        Objects.requireNonNull(content, "Required parameter is null: content");

        mStartLine = startLine;
        mHeaderSection = headerSection;
        mContent = content;

        mViaBranchParam = SipMessageParsingUtils.getTransactionId(mHeaderSection);
        if (TextUtils.isEmpty(mViaBranchParam)) {
            throw new IllegalArgumentException("header section MUST contain a branch parameter "
                    + "inside of the Via header.");
        }
        mCallIdParam = SipMessageParsingUtils.getCallId(mHeaderSection);
    }

@@ -107,11 +113,9 @@ public final class SipMessage implements Parcelable {

    /**
     * @return the branch parameter enclosed in the Via header key's value. See RFC 3261 section
     * 20.42 for more information on the Via header. If {@code null}, then there was either no
     * Via parameter found in this SIP message's headers or no branch parameter found in the
     * Via header.
     * 20.42 for more information on the Via header.
     */
    public @Nullable String getViaBranchParameter() {
    public @NonNull String getViaBranchParameter() {
        return mViaBranchParam;
    }

+0 −7
Original line number Diff line number Diff line
@@ -28,8 +28,6 @@ import android.telephony.ims.SipDelegateImsConfiguration;
import android.telephony.ims.SipDelegateManager;
import android.telephony.ims.SipMessage;
import android.telephony.ims.stub.SipDelegate;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.Set;
@@ -187,11 +185,6 @@ public class SipDelegateAidlWrapper implements DelegateStateCallback, DelegateMe

    private void notifyLocalMessageFailedToBeReceived(SipMessage m, int reason) {
        String transactionId = m.getViaBranchParameter();
        if (TextUtils.isEmpty(transactionId)) {
            Log.w(LOG_TAG, "failure to parse SipMessage.");
            throw new IllegalArgumentException("Malformed SipMessage, can not determine "
                    + "transaction ID.");
        }
        SipDelegate d = mDelegate;
        if (d != null) {
            mExecutor.execute(() -> d.notifyMessageReceiveError(transactionId, reason));
+1 −8
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import android.telephony.ims.SipMessage;
import android.telephony.ims.stub.DelegateConnectionMessageCallback;
import android.telephony.ims.stub.DelegateConnectionStateCallback;
import android.telephony.ims.stub.SipDelegate;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Log;

@@ -267,12 +266,6 @@ public class SipDelegateConnectionAidlWrapper implements SipDelegateConnection,

    private void notifyLocalMessageFailedToSend(SipMessage m, int reason) {
        String transactionId = m.getViaBranchParameter();
        if (TextUtils.isEmpty(transactionId)) {
            Log.w(LOG_TAG, "sendMessage detected a malformed SipMessage and can not get a "
                    + "transaction ID.");
            throw new IllegalArgumentException("Could not send SipMessage due to malformed header");
        }
        mExecutor.execute(() ->
                mMessageCallback.onMessageSendFailure(transactionId, reason));
        mExecutor.execute(() -> mMessageCallback.onMessageSendFailure(transactionId, reason));
    }
}