Skip to content

Commit b731047

Browse files
karesclaude
andcommitted
[compat] Store#add_cert and add_crl handle wrong argument
CRuby raises TypeError with a descriptive message (e.g. "wrong argument type String (expected OpenSSL/X509)"). JRuby was raising StoreError with "No message available" because the null cert/CRL passed through to the store internals. Now validates type upfront. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c724bdd commit b731047

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

src/main/java/org/jruby/ext/openssl/X509Store.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,25 @@ public IRubyObject set_default_paths(final ThreadContext context) {
202202
}
203203

204204
@JRubyMethod
205-
public X509Store add_cert(final IRubyObject cert) {
206-
X509AuxCertificate auxCert = cert instanceof X509Cert ? ((X509Cert) cert).getAuxCert() : null;
205+
public X509Store add_cert(final ThreadContext context, final IRubyObject cert) {
206+
if (!(cert instanceof X509Cert)) {
207+
throw context.runtime.newTypeError(cert, _X509(context.runtime).getClass("Certificate"));
208+
}
209+
X509AuxCertificate auxCert = ((X509Cert) cert).getAuxCert();
207210
if ( store.addCertificate(auxCert) != 1 ) {
208-
throw newStoreError(getRuntime(), X509Error.getLastErrorMessage());
211+
throw newStoreError(context.runtime, X509Error.getLastErrorMessage());
209212
}
210213
return this;
211214
}
212215

213216
@JRubyMethod
214-
public X509Store add_crl(final IRubyObject crl) {
215-
java.security.cert.X509CRL jCRL = (crl instanceof X509CRL) ? ((X509CRL) crl).getCRL() : null;
217+
public X509Store add_crl(final ThreadContext context, final IRubyObject crl) {
218+
if (!(crl instanceof X509CRL)) {
219+
throw context.runtime.newTypeError(crl, _X509(context.runtime).getClass("CRL"));
220+
}
221+
java.security.cert.X509CRL jCRL = ((X509CRL) crl).getCRL();
216222
if ( store.addCRL(jCRL) != 1 ) {
217-
throw newStoreError(getRuntime(), X509Error.getLastErrorMessage());
223+
throw newStoreError(context.runtime, X509Error.getLastErrorMessage());
218224
}
219225
return this;
220226
}

src/test/ruby/x509/test_x509store.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ def test_store_context_verify_raises_on_reuse
121121
assert_raise(OpenSSL::X509::StoreError) { ctx.verify }
122122
end
123123

124+
# CRuby raises TypeError (not StoreError) for wrong argument types
125+
def test_add_cert_type_check
126+
store = OpenSSL::X509::Store.new
127+
assert_raise(TypeError) { store.add_cert("not a cert") }
128+
assert_raise(TypeError) { store.add_cert(nil) }
129+
end
130+
131+
def test_add_crl_type_check
132+
store = OpenSSL::X509::Store.new
133+
assert_raise(TypeError) { store.add_crl("not a crl") }
134+
assert_raise(TypeError) { store.add_crl(nil) }
135+
end
136+
124137
def test_use_non_existing_cert_file
125138
ENV['SSL_CERT_FILE'] = 'non-existing-file.crt'
126139
store = OpenSSL::X509::Store.new

0 commit comments

Comments
 (0)