Skip to content
Merged
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
8 changes: 8 additions & 0 deletions .github/actions/unit-test-setup/action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
name: Setup unit test
description: Download Maven caches needed for unit tests

inputs:
java-version:
description: The Java version that is going to be set up.
required: false
default: "25"

runs:
using: composite
steps:
- name: Setup Java
uses: ./.github/actions/java-setup
with:
java-version: ${{ inputs.java-version }}

- id: maven-cache
name: Maven cache
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,31 @@ jobs:
run: |
./mvnw test -am -pl "$(.github/scripts/find-modules-with-unit-tests.sh)"

sast-check:
name: SAST Check
runs-on: ubuntu-latest
needs: build
timeout-minutes: 60
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- id: unit-test-setup
name: Unit test setup
uses: ./.github/actions/unit-test-setup
with:
# JDK 22+ eagerly resolves types referenced by RuntimeVisibleTypeAnnotations (JDK-8225377).
# This breaks the Checker Framework, which writes type annotations referencing types
# that may not be on the downstream module's classpath.
# https://github.com/typetools/checker-framework/issues/8346
java-version: "21"

- name: Run Checker Framework ResourceLeakChecker
run: |
# Select production dependencies via the distribution. Quarkus, model/infinispan,
# admin-cli, db-compatibility-verifier, src/test sources and skipDefs classes are not checked.
# Always do a clean build to ensure all resources are checked for leaks, as the Checker Framework plugin only checks resources that are compiled in the current build.
./mvnw clean package -e --fail-at-end -am -DskipTests -Pcheckerframework -pl quarkus/dist

base-integration-tests:
name: Base IT
needs: build
Expand Down Expand Up @@ -1355,6 +1380,7 @@ jobs:
- conditional
- build
- unit-tests
- sast-check
- base-integration-tests
- adapter-integration-tests
- quarkus-unit-tests
Expand Down
79 changes: 79 additions & 0 deletions .mvn/checkerframework/jdk.astub
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import org.checkerframework.checker.mustcall.qual.MustCall;
import org.checkerframework.checker.mustcall.qual.NotOwning;
import org.checkerframework.checker.mustcall.qual.Owning;

package org.infinispan.manager;

// Incremental-checking exemption: the shared cache manager is stopped by its provider factory.
// Replace with explicit ownership/stop contracts when enabling model/infinispan checking.
@MustCall({})
interface EmbeddedCacheManager extends CacheContainer {}

package org.jgroups;

// Lifecycle checking is deferred with model/infinispan; channels still require close().
// Replace this blanket exemption with transport ownership/close contracts when enabling that module.
@MustCall({})
class JChannel extends Channel {}

package org.hibernate.engine.jdbc.connections.spi;

interface JdbcConnectionAccess {
void releaseConnection(@Owning Connection connection) throws SQLException;
}

package jakarta.persistence;

interface ConnectionFunction<C, T> {
T apply(@NotOwning C connection) throws Exception;
}

interface ConnectionConsumer<C> {
void accept(@NotOwning C connection) throws Exception;
}

package liquibase.database;

interface Database {
@NotOwning DatabaseConnection getConnection();
}

package liquibase.database.jvm;

class JdbcConnection {
@NotOwning Connection getWrappedConnection();
}

package io.quarkus.runtime;

abstract class Application {
@NotOwning static Application currentApplication();
}

package org.apache.http.impl.client;

class AbstractResponseHandler<T> implements ResponseHandler<T> {
T handleResponse(@NotOwning HttpResponse response) throws HttpResponseException, IOException;
}

package com.fasterxml.jackson.core;

abstract class JsonGenerator {
@NotOwning JsonGenerator useDefaultPrettyPrinter();
}

package com.fasterxml.jackson.databind;

class JsonSerializer<T> {
void serialize(T value, @NotOwning JsonGenerator gen, SerializerProvider serializers) throws IOException;
}

class SerializerProvider {
@NotOwning JsonGenerator getGenerator();
}

package tools.jackson.databind;

class ValueSerializer<T> {
void serialize(T value, @NotOwning JsonGenerator gen, SerializationContext ctxt);
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,17 @@ public static String replaceProperties(final String string, PropertyResolver res
if (index == -1) {
return string;
}
try {
return string.substring(0, index).concat(StreamUtil
.readString(replaceProperties(new ByteArrayInputStream(string.substring(index).getBytes(StandardCharsets.UTF_8)),
resolver), StandardCharsets.UTF_8));
try (InputStream is = replaceProperties(new ByteArrayInputStream(string.substring(index).getBytes(StandardCharsets.UTF_8)),
resolver)) {
return string.substring(0, index).concat(StreamUtil.readString(is, StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e); // not expected
}
}

// Ownership: the returned InputStream takes responsibility for closing source via its close() method.
// The recursive call at line 172 reuses the same source non-owningly — only the outermost close() closes it.
@SuppressWarnings("required.method.not.called")
public static InputStream replaceProperties(final InputStream source, PropertyResolver resolver) {
return replaceProperties(source, false, resolver);
}
Expand Down Expand Up @@ -167,6 +169,7 @@ public int read() throws IOException {
}
// determine the key
int keyChar = -1;
@SuppressWarnings("required.method.not.called") // non-owning view into same source; must not close
InputStream keyStream = replaceProperties(source, true, resolver);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
while ((keyChar = keyStream.read()) > -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public void serialize(MultivaluedHashMap<String, String> map, JsonGenerator gen,
@Override
public boolean isEmpty(SerializerProvider provider, MultivaluedHashMap<String, String> value) {
// if all properties are ignored, consider the map as empty
return getIgnoredProperties(provider.getGenerator()).containsAll(value.keySet());
JsonGenerator gen = provider.getGenerator();
return getIgnoredProperties(gen).containsAll(value.keySet());
}

private static Set<String> getIgnoredProperties(JsonGenerator gen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public byte[] concatenatedRSToASN1DER(final byte[] signature, int signLength) th
public byte[] asn1derToConcatenatedRS(final byte[] derEncodedSignatureValue, int signLength) throws IOException {
int len = signLength / 2;

ASN1InputStream asn1InputStream = new ASN1InputStream(derEncodedSignatureValue);
ASN1Primitive asn1Primitive = asn1InputStream.readObject();
asn1InputStream.close();
ASN1Primitive asn1Primitive;
try (ASN1InputStream asn1InputStream = new ASN1InputStream(derEncodedSignatureValue)) {
asn1Primitive = asn1InputStream.readObject();
}

ASN1Sequence asn1Sequence = (ASN1Sequence.getInstance(asn1Primitive));
ASN1Integer rASN1 = (ASN1Integer) asn1Sequence.getObjectAt(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.keycloak.crypto.def;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -430,8 +431,8 @@ protected List<String> getResponderURIs(X509Certificate cert) throws Certificate
JcaX509CertificateHolder holder = new JcaX509CertificateHolder(cert);
Extension aia = holder.getExtension(Extension.authorityInfoAccess);
if (aia != null) {
try {
ASN1InputStream in = new ASN1InputStream(aia.getExtnValue().getOctetStream());
try (InputStream octetStream = aia.getExtnValue().getOctetStream();
ASN1InputStream in = new ASN1InputStream(octetStream)) {
Comment thread
stianst marked this conversation as resolved.
Resolved
ASN1Sequence seq = (ASN1Sequence)in.readObject();
AuthorityInformationAccess authorityInfoAccess = AuthorityInformationAccess.getInstance(seq);
for (AccessDescription ad : authorityInfoAccess.getAccessDescriptions()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ protected String encode(Object obj) {

try {
StringWriter writer = new StringWriter();
JcaPEMWriter pemWriter = new JcaPEMWriter(writer);
pemWriter.writeObject(obj);
pemWriter.flush();
pemWriter.close();
String s = writer.toString();
return removeBeginEnd(s);
try (JcaPEMWriter pemWriter = new JcaPEMWriter(writer)) {
pemWriter.writeObject(obj);
pemWriter.flush();
}
return removeBeginEnd(writer.toString());
} catch (Exception e) {
throw new PemException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ public Object extractUserIdentity(X509Certificate[] certs) {
if (obj instanceof byte[]) {
byte[] otherNameBytes = (byte[]) obj;

try {
ASN1InputStream asn1Stream = new ASN1InputStream(new ByteArrayInputStream(otherNameBytes));
try (ASN1InputStream asn1Stream = new ASN1InputStream(new ByteArrayInputStream(otherNameBytes))) {
ASN1Encodable asn1otherName = asn1Stream.readObject();
asn1otherName = unwrap(asn1otherName);

Expand Down
6 changes: 6 additions & 0 deletions crypto/fips1402/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<description>Keycloak Crypto FIPS 140-2 Integration</description>

<dependencies>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
<version>${checkerframework.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

import org.checkerframework.checker.calledmethods.qual.EnsuresCalledMethods;
import org.checkerframework.checker.mustcall.qual.MustCallAlias;
import org.checkerframework.checker.mustcall.qual.NotOwning;
import org.checkerframework.checker.mustcall.qual.Owning;

/**
* Forked from wildfly-elytron
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
abstract class AbstractDelegatingSSLSocket extends SSLSocket {

private final SSLSocket delegate;
private final @Owning SSLSocket delegate;

AbstractDelegatingSSLSocket(final SSLSocket delegate) {
@MustCallAlias
AbstractDelegatingSSLSocket(final @MustCallAlias SSLSocket delegate) {
this.delegate = delegate;
}

Expand All @@ -54,10 +60,12 @@ public String getHandshakeApplicationProtocol() {
return delegate.getHandshakeApplicationProtocol();
}

@SuppressWarnings("type.argument") // Resource types in generic arguments are not supported (manual section 8.10).
public void setHandshakeApplicationProtocolSelector(BiFunction<SSLSocket, List<String>, String> selector) {
delegate.setHandshakeApplicationProtocolSelector(selector);
}

@SuppressWarnings("type.argument") // Resource types in generic arguments are not supported (manual section 8.10).
public BiFunction<SSLSocket, List<String>, String> getHandshakeApplicationProtocolSelector() {
return delegate.getHandshakeApplicationProtocolSelector();
}
Expand Down Expand Up @@ -270,6 +278,7 @@ public boolean getReuseAddress() throws SocketException {
return delegate.getReuseAddress();
}

@EnsuresCalledMethods(value = "delegate", methods = "close")
public void close() throws IOException {
delegate.close();
}
Expand All @@ -294,6 +303,7 @@ public boolean isBound() {
return delegate.isBound();
}

@SuppressWarnings("contracts.conditional.postcondition") // Closing the delegate also fulfills this wrapper's close obligation.
public boolean isClosed() {
return delegate.isClosed();
}
Expand All @@ -310,6 +320,7 @@ public void setPerformancePreferences(final int connectionTime, final int latenc
delegate.setPerformancePreferences(connectionTime, latency, bandwidth);
}

@NotOwning
protected SSLSocket getDelegate() {
return delegate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public byte[] concatenatedRSToASN1DER(final byte[] signature, int signLength) th
public byte[] asn1derToConcatenatedRS(final byte[] derEncodedSignatureValue, int signLength) throws IOException {
int len = signLength / 2;

ASN1InputStream asn1InputStream = new ASN1InputStream(derEncodedSignatureValue);
ASN1Primitive asn1Primitive = asn1InputStream.readObject();
asn1InputStream.close();
ASN1Primitive asn1Primitive;
try (ASN1InputStream asn1InputStream = new ASN1InputStream(derEncodedSignatureValue)) {
asn1Primitive = asn1InputStream.readObject();
}

ASN1Sequence asn1Sequence = (ASN1Sequence.getInstance(asn1Primitive));
ASN1Integer rASN1 = (ASN1Integer) asn1Sequence.getObjectAt(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.keycloak.crypto.fips;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -429,8 +430,8 @@ protected List<String> getResponderURIs(X509Certificate cert) throws Certificate
JcaX509CertificateHolder holder = new JcaX509CertificateHolder(cert);
Extension aia = holder.getExtension(Extension.authorityInfoAccess);
if (aia != null) {
try {
ASN1InputStream in = new ASN1InputStream(aia.getExtnValue().getOctetStream());
try (InputStream octetStream = aia.getExtnValue().getOctetStream();
ASN1InputStream in = new ASN1InputStream(octetStream)) {
Comment thread
stianst marked this conversation as resolved.
Resolved
ASN1Sequence seq = (ASN1Sequence)in.readObject();
AuthorityInformationAccess authorityInfoAccess = AuthorityInformationAccess.getInstance(seq);
for (AccessDescription ad : authorityInfoAccess.getAccessDescriptions()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ protected String encode(Object obj) {

try {
StringWriter writer = new StringWriter();
JcaPEMWriter pemWriter = new JcaPEMWriter(writer);
pemWriter.writeObject(obj);
pemWriter.flush();
pemWriter.close();
String s = writer.toString();
return removeBeginEnd(s);
try (JcaPEMWriter pemWriter = new JcaPEMWriter(writer)) {
pemWriter.writeObject(obj);
pemWriter.flush();
}
return removeBeginEnd(writer.toString());
} catch (Exception e) {
throw new PemException(e);
}
Expand Down Expand Up @@ -129,8 +128,9 @@ public PrivateKey decodePrivateKey(String pem) {
}

private Object readPrivateKeyObject(String pemWithBeginEnd) throws IOException {
PEMParser parser = new PEMParser(new StringReader(pemWithBeginEnd));
return parser.readObject();
try (PEMParser parser = new PEMParser(new StringReader(pemWithBeginEnd))) {
return parser.readObject();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ public Object extractUserIdentity(X509Certificate[] certs) {
if (obj instanceof byte[]) {
byte[] otherNameBytes = (byte[]) obj;

try {
ASN1InputStream asn1Stream = new ASN1InputStream(new ByteArrayInputStream(otherNameBytes));
try (ASN1InputStream asn1Stream = new ASN1InputStream(new ByteArrayInputStream(otherNameBytes))) {
ASN1Encodable asn1otherName = asn1Stream.readObject();
asn1otherName = unwrap(asn1otherName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ public Signature getSignature(String sigAlgName) throws NoSuchAlgorithmException
}

@Override
// Socket ownership is returned directly or through AbstractDelegatingSSLSocket, but the anonymous
// factory's inherited contracts do not model these transfers. Revisit when socket-factory ownership
// and alias contracts are modeled.
@SuppressWarnings({"override.return", "required.method.not.called", "reset.not.owning"})
public SSLSocketFactory wrapFactoryForTruststore(SSLSocketFactory delegate) {
// See https://downloads.bouncycastle.org/fips-java/BC-FJA-(D)TLSUserGuide-1.0.9.pdf - Section 3.5.2 (Endpoint identification)
return new CustomSSLSocketFactory(delegate) {
Expand Down
Loading
Loading