Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
3 changes: 1 addition & 2 deletions test-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions test-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
android:name=".NotificationService"
android:exported="false" />

<!-- Launcher activity of the test app: it must be exported (required on Android 12+) so the
system can start it. It takes no input from other apps. -->
<!-- nosemgrep -->
<activity
android:name=".SplashScreenActivity"
android:exported="true"
Expand Down
Loading