Skip to content

Conversation

haramj
Copy link
Contributor

@haramj haramj commented Aug 4, 2025

This PR implements the TODO(joyeecheung) note to support X509Certificate output in tls.getCACertificates(). It introduces a new format option, enhancing the function's flexibility and aligning its API with Node.js's broader crypto module.

The function's behavior is now extended as follows:
API Enhancement: Adds an optional format parameter to tls.getCACertificates() to specify the output format.

Output Options:

  • 'pem' (default): Returns an array of PEM-encoded certificate strings. This is the new default to align with the name used in other Node.js crypto APIs.
  • 'der': Returns an array of certificate data as Buffer objects in DER format.
  • 'x509': Returns an array of crypto.X509Certificate instances, providing direct access to certificate properties.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. tls Issues and PRs related to the tls subsystem. labels Aug 4, 2025
doc/api/tls.md Outdated
@@ -2331,11 +2335,14 @@ Returns an array containing the CA certificates from various sources, depending
trusted store.
* When [`NODE_EXTRA_CA_CERTS`][] is used, this would also include certificates loaded from the specified
file.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

Copy link
Member

Choose a reason for hiding this comment

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

It seems the white spaces are still there? Can you remove 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.

Okay. I'll remove the white space

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joyeecheung Removing that white space will cause a format error in the document..

@haramj
Copy link
Contributor Author

haramj commented Aug 4, 2025

@jasnell Thank you very much for the thorough and detailed reviews.
I really appreciate the time and effort you’re putting into this.
I’ll carefully go through all the feedback and address them step by step.

Additionally,
When building the docs, the tooling failed to recognize the X509Certificate type and composite return types like Array<Buffer|X509Certificate>, causing errors.
I tried fixing type annotations, references, and escape characters, but I'd appreciate guidance on the correct way to document such composite types officially.

Copy link

codecov bot commented Aug 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.26%. Comparing base (7535aa1) to head (e2879a5).
⚠️ Report is 155 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #59349      +/-   ##
==========================================
- Coverage   89.88%   88.26%   -1.62%     
==========================================
  Files         667      701      +34     
  Lines      195217   206815   +11598     
  Branches    38325    39788    +1463     
==========================================
+ Hits       175472   182553    +7081     
- Misses      12210    16289    +4079     
- Partials     7535     7973     +438     
Files with missing lines Coverage Δ
lib/tls.js 96.57% <100.00%> (+0.32%) ⬆️

... and 182 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@haramj
Copy link
Contributor Author

haramj commented Aug 11, 2025

The CI is failing due to a missing documentation anchor:
link not found: all_tls_tlsgetcacertificatestype
This seems unrelated to the changes in this PR — I didn’t modify that anchor or the associated section..

@haramj haramj changed the title tls: add 'as' option to getCACertificates() for X509Certificate output tls: use options in getCACertificates() with X509Certificate Aug 20, 2025
@haramj haramj force-pushed the haramjeong-patch-250805 branch from 2e19981 to d978079 Compare August 20, 2025 15:29
@haramj
Copy link
Contributor Author

haramj commented Aug 25, 2025

Hi, feedback has been addressed.
Is there anything else needed from me before this can move forward?

@haramj
Copy link
Contributor Author

haramj commented Aug 31, 2025

If you have some time, could you please review this? @nodejs/net, @joyeecheung

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

LGTM! Great job. Let's ask @joyeecheung to also take a look since it was her TODO :-)

@haramj
Copy link
Contributor Author

haramj commented Sep 7, 2025

LGTM! Great job. Let's ask @joyeecheung to also take a look since it was her TODO :-)

Thank you so much for the review and the LGTM! I really appreciate your time.

During my final testing, I discovered an unexpected failure related to caching, and also found a minor inconsistency with the documentation. I would like to push a fix for those before the PR is ready to merge.

I'll push the updated changes shortly.

@haramj
Copy link
Contributor Author

haramj commented Sep 7, 2025

@jasnell @joyeecheung
I've confirmed that the logic using the map() method in the getCACertificates function is causing the caching-related assert.strictEqual test to fail, because it creates a new copy every time.

So I added the caching logic, but now other tests are failing.

@nodejs-github-bot
Copy link
Collaborator

@haramj
Copy link
Contributor Author

haramj commented Sep 9, 2025

@jasnell @joyeecheung

I've successfully implemented support for new formats by clearly separating the logic for object input while maintaining the existing function's behavior.

All tests have passed, and I believe the PR is ready for review.

Thank you.

lib/tls.js Outdated

validateString(type, 'type');

switch (type) {
Copy link
Member

Choose a reason for hiding this comment

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

It seems to me this can just reuse the original function; keep that as something like function getCACertificatesAsStrings(type = 'default'), and in function getCACertificates(options = undefined) you just normalize the options first, then get the array of strings, then process it from there.

lib/tls.js Outdated
Comment on lines 210 to 215
return certs.map((cert) => {
if (typeof cert === 'string') {
return cert;
}
return `-----BEGIN CERTIFICATE-----\n${cert.toString('base64').match(/.{1,64}/g).join('\n')}\n-----END CERTIFICATE-----`;
});
Copy link
Member

@joyeecheung joyeecheung Sep 10, 2025

Choose a reason for hiding this comment

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

I don't think this branch is needed? It should just return the certs. They are already pem-encoded.

lib/tls.js Outdated
return buffers;
}

return buffers.map((buf) => new X509Certificate(buf));
Copy link
Member

Choose a reason for hiding this comment

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

Parsing it after a base64 decoding from JS land is a lot of detour, this can just directly return certs.map(cert => new X509Certificate(buf)) first if the type is x509, the native land would just do the decoding and parsing at once, and it would be simpler in that case because the X509Certificate parser would actually attempt to decode it as PEM first before retrying with d2i_X509_bio anyway.

@haramj
Copy link
Contributor Author

haramj commented Sep 11, 2025

@joyeecheung I've applied the feedback. Could you take a look?

@haramj haramj requested a review from joyeecheung September 13, 2025 02:01
@haramj
Copy link
Contributor Author

haramj commented Sep 16, 2025

Could you please take a look at this when you have a moment? Thanks! @joyeecheung

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. tls Issues and PRs related to the tls subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants