Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Null Reference Exceptions on non-configured certificate stores #3023

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

romanett
Copy link
Contributor

Proposed changes

If issuer store or rejected store are not configured null reference exceptions could occur.
Add Null checks to avoid these exceptions

Related Issues

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • Enhancement (non-breaking change which adds functionality)
  • Test enhancement (non-breaking change to increase test coverage)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected, requires version increase of Nuget packages)
  • Documentation Update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc.
  • I have signed the CLA.
  • I ran tests locally with my changes, all passed.
  • I fixed all failing tests in the CI pipelines.
  • I fixed all introduced issues with CodeQL and LGTM.
  • I have added tests that prove my fix is effective or that my feature works and increased code coverage.
  • I have added necessary documentation (if appropriate).
  • Any dependent changes have been merged and published in downstream modules.

Further comments

@romanett romanett requested a review from mrsuciu February 28, 2025 15:00
Copy link

codecov bot commented Feb 28, 2025

Codecov Report

Attention: Patch coverage is 71.15385% with 15 lines in your changes missing coverage. Please review.

Project coverage is 56.63%. Comparing base (ef9387b) to head (0c6ad7f).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
Libraries/Opc.Ua.Server/Configuration/TrustList.cs 70.96% 4 Missing and 5 partials ⚠️
...a.Server/Configuration/ConfigurationNodeManager.cs 76.92% 2 Missing and 1 partial ⚠️
...Core/Security/Certificates/CertificateTrustList.cs 0.00% 2 Missing ⚠️
...Core/Security/Certificates/CertificateValidator.cs 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3023      +/-   ##
==========================================
- Coverage   56.70%   56.63%   -0.07%     
==========================================
  Files         356      356              
  Lines       68110    68445     +335     
  Branches    13950    14082     +132     
==========================================
+ Hits        38619    38764     +145     
- Misses      25339    25499     +160     
- Partials     4152     4182      +30     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@romanett romanett self-assigned this Feb 28, 2025
@romanett romanett added the bug A bug was identified and should be fixed. label Feb 28, 2025
@EthanChangAED EthanChangAED requested a review from salihgoncu March 6, 2025 14:44
@@ -578,7 +578,7 @@ private ServiceResult UpdateCertificate(
try
{
Utils.LogCertificate(Utils.TraceMasks.Security, "Add new issuer certificate: ", issuer);
issuerStore.Add(issuer).Wait();
issuerStore?.Add(issuer).Wait();
Copy link

@wbtoms wbtoms Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ? will prevent the NullReferenceException to be thrown, which then leads to an inconsitency with the log message logged just before and gives no indication that this add actually failed. Wouldn't it be better to have an explicit null check with a clear failing excection.
This method is actually really huge, maybe such test should be part of some preliminary conditions before appling changes to stores.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NonConfigured Issuer store should not cause any error messages.
Good Point with the inconsistent logging, cahanged to explicit null in newest push.

@@ -144,20 +144,23 @@ private ServiceResult Open(
ICertificateStore store = m_trustedStore.OpenStore();
try
{
if ((masks & TrustListMasks.TrustedCertificates) != 0)
if (store != null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar question here and below in this method about the case of store being null and the return status code Good. When one of the store is null, shall the result still be good? And wouldn't it be good to test all stores prior to any modification to one of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho a store with just issuer or just trusted certs should be okay. E.g. for user certs you only want to allow specific certs and not trust all certs from a ca, then UserIssuerStore would be unconfigured, but still a valid trust list.

var certCollection = store.FindByThumbprint(thumbprint).GetAwaiter().GetResult();

if (certCollection.Count == 0)
if (store != null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here, result is considered good if the store is null, is this what we want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho a store with just issuer or just trusted certs should be okay. E.g. for user certs you only want to allow specific certs and not trust all certs from a ca, then UserIssuerStore would be unconfigured, but still a valid trust list.

@wbtoms
Copy link

wbtoms commented Mar 7, 2025

There are some other places where a store is check to be not null, but what is the result status code supposed to be in case the store is actually null. It is a general comment to other places where this has been modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug was identified and should be fixed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Null Reference exception after upgrading from 1.5.374.78 to 1.5.374.158
2 participants