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

WIP: Initialize nss explicitly #6596

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jo
Copy link
Contributor

@jo jo commented Feb 14, 2025

Initialize NSS explicitly, rather than implicitly in NSS-using functions.

⚠️ depends on #6607 ⚠️

To have control over NSS initialization, it must be done explicitly. This pull request removes implicit initialization from the relevant methods and instead checks whether NSS has been initialized and throws an appropriate error if it has not.
Initialization can be done either directly via NSS.

BREAKING CHANGE:
you now need to call nss::ensure_initialized() before any calls to functions which depend on NSS. Watch out for CryptoError(Error(NSS error: NSS has not been initialized)).

Pull Request checklist

  • Breaking changes: This PR follows our breaking change policy
    • This PR follows the breaking change policy:
      • This PR has no breaking API changes, or
      • There are corresponding PRs for our consumer applications that resolve the breaking changes and have been approved
  • Quality: This PR builds and tests run cleanly
    • Note:
      • For changes that need extra cross-platform testing, consider adding [ci full] to the PR title.
      • If this pull request includes a breaking change, consider cutting a new release after merging.
  • Tests: This PR includes thorough tests or an explanation of why it does not
  • Changelog: This PR includes a changelog entry in CHANGELOG.md or an explanation of why it does not need one
    • Any breaking changes to Swift or Kotlin binding APIs are noted explicitly
  • Dependencies: This PR follows our dependency management guidelines
    • Any new dependencies are accompanied by a summary of the due diligence applied in selecting them.

Branch builds: add [firefox-android: branch-name] to the PR title.

@jo jo force-pushed the explicit-nss-initialization branch 11 times, most recently from e23748c to 6a74eca Compare February 18, 2025 14:15
Copy link
Contributor

@bendk bendk left a comment

Choose a reason for hiding this comment

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

I like this idea in general, the only question for me is how applications should initialize NSS.

Copy link
Member

@mhammond mhammond left a comment

Choose a reason for hiding this comment

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

Part of what I don't understand here is what initialization looks like when the "keydb" feature is enabled? Something like logins is eventually going to be used in both contexts, so how would that look? (Sorry if I missed this!)

@jo jo force-pushed the explicit-nss-initialization branch 12 times, most recently from 578d4e7 to 8b539bb Compare February 21, 2025 14:00
Comment on lines +32 to +40
// TODO: would be nice if we could write
// #[uniffi::export]
// pub use nss::ensure_initialized as ensure_nss_initialized;
// instead.
#[uniffi::export]
pub fn ensure_nss_initialized() {
ensure_initialized()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bendk is there a way to do something like

#[uniffi::export]
pub use nss::ensure_initialized as ensure_nss_initialized;

or a way to avoid wrapping the function?

Copy link
Member

Choose a reason for hiding this comment

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

There is https://mozilla.github.io/uniffi-rs/latest/proc_macro/functions.html#renaming-functions-methods-and-constructors, but I don't think that will actually work here :( So I don't think there is a good way, but it seems like a reasonable feature request!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doesn't look like there's already an issue filed for this, will do that

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for filing that. IMO, we could extend the concept of "external types" to also support "external functions".

@jo jo force-pushed the explicit-nss-initialization branch 3 times, most recently from 8a4bea3 to d6ef0ab Compare February 21, 2025 14:54
Comment on lines +32 to +40
// TODO: would be nice if we could write
// #[uniffi::export]
// pub use nss::ensure_initialized as ensure_nss_initialized;
// instead.
#[uniffi::export]
pub fn ensure_nss_initialized() {
ensure_initialized()
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for filing that. IMO, we could extend the concept of "external types" to also support "external functions".

BREAKING CHANGE:
you now need to call `nss::ensure_initialized()` before any calls to
NSS functions, including dependants like rc_crypto or jwcrypto.
@jo jo force-pushed the explicit-nss-initialization branch from d6ef0ab to b1cd271 Compare February 21, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants