From ab6c1554833c652a3de2e77f3bcca3952a2ca495 Mon Sep 17 00:00:00 2001 From: Ramzi Sabra Date: Sat, 8 Mar 2025 20:28:36 +0200 Subject: [PATCH 1/2] feat: add JSON Schema support --- Cargo.lock | 18 ++++++++++++++++++ secrecy/Cargo.toml | 28 +++++++++++++++++----------- secrecy/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33e1de69..c001550f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -222,6 +222,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "dyn-clone" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c7a8fb8a9fbf66c1f703fe16184d10ca0ee9d23be5b4436400408ba54a95005" + [[package]] name = "ecdsa" version = "0.16.9" @@ -993,6 +999,17 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "schemars" +version = "0.8.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fbf2ae1b8bc8e02df939598064d22402220cd5bbcca1c76f7d6a310974d5615" +dependencies = [ + "dyn-clone", + "serde", + "serde_json", +] + [[package]] name = "sct" version = "0.6.1" @@ -1021,6 +1038,7 @@ dependencies = [ name = "secrecy" version = "0.10.3" dependencies = [ + "schemars", "serde", "zeroize", ] diff --git a/secrecy/Cargo.toml b/secrecy/Cargo.toml index f1b60c2e..3c2360d2 100644 --- a/secrecy/Cargo.toml +++ b/secrecy/Cargo.toml @@ -1,28 +1,34 @@ [package] -name = "secrecy" +name = "secrecy" description = """ Wrapper types and traits for secret management which help ensure they aren't accidentally copied, logged, or otherwise exposed (as much as possible), and also ensure secrets are securely wiped from memory when dropped. """ -version = "0.10.3" -authors = ["Tony Arcieri "] -license = "Apache-2.0 OR MIT" -homepage = "https://github.com/iqlusioninc/crates/" -repository = "https://github.com/iqlusioninc/crates/tree/main/secrecy" -readme = "README.md" -categories = ["cryptography", "memory-management", "no-std", "os"] -keywords = ["clear", "memory", "secret", "secure", "wipe"] -edition = "2021" +version = "0.10.3" +authors = ["Tony Arcieri "] +license = "Apache-2.0 OR MIT" +homepage = "https://github.com/iqlusioninc/crates/" +repository = "https://github.com/iqlusioninc/crates/tree/main/secrecy" +readme = "README.md" +categories = ["cryptography", "memory-management", "no-std", "os"] +keywords = ["clear", "memory", "secret", "secure", "wipe"] +edition = "2021" rust-version = "1.60" [dependencies] zeroize = { version = "1.6", default-features = false, features = ["alloc"] } # optional dependencies -serde = { version = "1", optional = true, default-features = false, features = ["alloc"] } +serde = { version = "1", optional = true, default-features = false, features = [ + "alloc", +] } +schemars = { version = "0.8.22", optional = true, default-features = false } [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] + +[features] +jsonschema = ["dep:schemars"] diff --git a/secrecy/src/lib.rs b/secrecy/src/lib.rs index 2f1866fd..e7b4d71a 100644 --- a/secrecy/src/lib.rs +++ b/secrecy/src/lib.rs @@ -48,6 +48,16 @@ use zeroize::{Zeroize, ZeroizeOnDrop}; #[cfg(feature = "serde")] use serde::{de, ser, Deserialize, Serialize}; +#[cfg(feature = "jsonschema")] +use alloc::{ + borrow::{Cow, ToOwned}, + format, +}; +#[cfg(feature = "jsonschema")] +use core::{concat, module_path}; +#[cfg(feature = "jsonschema")] +use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema}; + pub use zeroize; /// Wrapper type for values that contains secrets, which attempts to limit @@ -335,6 +345,38 @@ where } } +#[cfg(feature = "jsonschema")] +impl JsonSchema for SecretString { + fn schema_name() -> String { + "SecretString".to_owned() + } + fn schema_id() -> Cow<'static, str> { + Cow::Borrowed(concat!(module_path!(), "::", "SecretString")) + } + fn json_schema(generator: &mut SchemaGenerator) -> Schema { + generator.subschema_for::() + } +} + +#[cfg(feature = "jsonschema")] +impl JsonSchema for SecretBox +where + T: Zeroize + JsonSchema, +{ + fn schema_name() -> String { + format!("SecretBox_for_{}", T::schema_name()) + } + fn schema_id() -> Cow<'static, str> { + Cow::Owned(alloc::format!( + concat!(module_path!(), "::", "SecretBox_for_{}"), + T::schema_id() + )) + } + fn json_schema(generator: &mut SchemaGenerator) -> Schema { + generator.subschema_for::() + } +} + #[cfg(test)] mod tests { use crate::{ExposeSecret, SecretString}; From 3545f9fd0bdbb7da7213fe676c1c120166a66edc Mon Sep 17 00:00:00 2001 From: Ramzi Sabra Date: Sat, 8 Mar 2025 22:25:59 +0200 Subject: [PATCH 2/2] feat(jsonschema): add documentation for feature --- secrecy/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/secrecy/src/lib.rs b/secrecy/src/lib.rs index e7b4d71a..199d6f6b 100644 --- a/secrecy/src/lib.rs +++ b/secrecy/src/lib.rs @@ -28,6 +28,14 @@ //! does *not* receive a corresponding [`Serialize`] impl. If you would like //! types of `SecretBox` to be serializable with `serde`, you will need to impl //! the [`SerializableSecret`] marker trait on `T`. +//! +//! # [JSON Schema](https://json-schema.org) support +//! +//! When the `jsonschema` feature of this crate is enabled, the [`SecretBox`] type +//! will receive a [`JsonSchema`] +//! ([schemars](https://docs.rs/schemars/latest/schemars/)) impl for all `SecretBox` +//! types where `T: JsonSchema`. This allows generating JSON Schema documents from +//! types that are composed of `SecretBox`, such as a configuration struct. #![no_std] #![cfg_attr(docsrs, feature(doc_auto_cfg))]