diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 16312c5a..4d94e947 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -17,6 +17,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + persist-credentials: false - name: Set up Ruby uses: ruby/setup-ruby@v1 with: @@ -35,6 +37,9 @@ jobs: with: # You should create a personal access token and store it in your repository token: ${{ secrets.CI_USER_TOKEN }} + # The token is only needed for this checkout. Later steps pass it explicitly via env + # and do not use git, so do not leave it in .git/config. + persist-credentials: false repository: 'optimizely/ci-helper-tools' path: 'home/runner/ci-helper-tools' ref: 'master' @@ -97,6 +102,8 @@ jobs: steps: - name: checkout uses: actions/checkout@v4 + with: + persist-credentials: false - name: set up JDK 17 uses: actions/setup-java@v4 with: diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 659a2b5b..dd4611aa 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,6 +23,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + persist-credentials: false - name: set up JDK 17 uses: actions/setup-java@v4 with: diff --git a/shared/src/main/java/com/optimizely/ab/android/shared/TLSSocketFactory.java b/shared/src/main/java/com/optimizely/ab/android/shared/TLSSocketFactory.java index d458bde0..47146c91 100644 --- a/shared/src/main/java/com/optimizely/ab/android/shared/TLSSocketFactory.java +++ b/shared/src/main/java/com/optimizely/ab/android/shared/TLSSocketFactory.java @@ -34,7 +34,12 @@ public class TLSSocketFactory extends SSLSocketFactory { private SSLSocketFactory sslSocketFactory; public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException { - SSLContext context = SSLContext.getInstance("TLS"); + this(SSLContext.getInstance("TLSv1.2")); + } + + // Visible for testing: lets a test pass a context that allows more than TLSv1.2, to check that + // enableTLSOnSocket pins the sockets on its own. + TLSSocketFactory(SSLContext context) throws KeyManagementException { context.init(null, null, null); sslSocketFactory = context.getSocketFactory(); } diff --git a/shared/src/test/java/com/optimizely/ab/android/shared/TLSSocketFactoryTest.java b/shared/src/test/java/com/optimizely/ab/android/shared/TLSSocketFactoryTest.java new file mode 100644 index 00000000..b274a6b8 --- /dev/null +++ b/shared/src/test/java/com/optimizely/ab/android/shared/TLSSocketFactoryTest.java @@ -0,0 +1,62 @@ +/**************************************************************************** + * Copyright 2026, Optimizely, Inc. and contributors * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ***************************************************************************/ + +package com.optimizely.ab.android.shared; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import java.net.Socket; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocket; + +public class TLSSocketFactoryTest { + + @Test + public void defaultFactoryOnlyEnablesTLSv12() throws Exception { + assertOnlyTLSv12(new TLSSocketFactory()); + } + + @Test + public void socketsArePinnedToTLSv12EvenWhenTheContextAllowsMore() throws Exception { + // "TLS" allows several protocol versions (TLSv1.3 and TLSv1.2 on the JVM running this test), so this + // only passes if enableTLSOnSocket itself limits the sockets to TLSv1.2. + SSLContext permissive = SSLContext.getInstance("TLS"); + permissive.init(null, null, null); + SSLSocket unpinned = (SSLSocket) permissive.getSocketFactory().createSocket(); + try { + assertTrue(unpinned.getEnabledProtocols().length > 1); + } finally { + unpinned.close(); + } + + assertOnlyTLSv12(new TLSSocketFactory(SSLContext.getInstance("TLS"))); + } + + private static void assertOnlyTLSv12(TLSSocketFactory factory) throws Exception { + // An unconnected socket is enough to check which protocols are enabled. + Socket socket = factory.createSocket(); + try { + assertTrue(socket instanceof SSLSocket); + assertArrayEquals(new String[] {"TLSv1.2"}, ((SSLSocket) socket).getEnabledProtocols()); + } finally { + socket.close(); + } + } +} diff --git a/test-app/build.gradle b/test-app/build.gradle index f242fc60..86d264f1 100644 --- a/test-app/build.gradle +++ b/test-app/build.gradle @@ -71,9 +71,8 @@ dependencies { implementation "androidx.appcompat:appcompat:1.2.0" implementation "com.google.android.material:material:1.2.1" - // required by API-16 + // JSON library for the SDK (it picks one from the classpath) implementation "com.google.code.gson:gson:$gson_ver" - implementation 'com.google.code.findbugs:annotations:3.0.0' testImplementation "junit:junit:$junit_ver" testImplementation "org.mockito:mockito-core:$mockito_ver" diff --git a/test-app/src/main/AndroidManifest.xml b/test-app/src/main/AndroidManifest.xml index 0fe3f090..13c1c4a1 100644 --- a/test-app/src/main/AndroidManifest.xml +++ b/test-app/src/main/AndroidManifest.xml @@ -38,6 +38,9 @@ android:name=".NotificationService" android:exported="false" /> + +