Skip to content

Commit d22829c

Browse files
committed
feat(header): make HeaderValue::set_sensitive const
Some applications like OAuth clients for GitHub or Forgejo are forced to embed a client password into the application, even if the client is considered public. Make `HeaderValue::set_sensitive` available in const contexts to allow applications to mark embedded headers as sensitive. Warn developers in `set_sensitive`'s documentation that embedded secrets are trivial to dump and should not be considered secure. Add a unit test that ensures both `HeaderValue::from_static` and `HeaderValue::set_sensitive` can be used in const contexts and that they continue to work as expected. Closes: #807
1 parent bc71780 commit d22829c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/header/value.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ impl HeaderValue {
298298

299299
/// Mark that the header value represents sensitive information.
300300
///
301+
/// This method is `const` to allow marking constants created with
302+
/// [`HeaderValue::from_static`] as sensitive. Note that sensitive values
303+
/// that are embedded into an application are trivial to dump and cannot be
304+
/// considered secure.
305+
///
301306
/// # Examples
302307
///
303308
/// ```
@@ -311,7 +316,7 @@ impl HeaderValue {
311316
/// assert!(!val.is_sensitive());
312317
/// ```
313318
#[inline]
314-
pub fn set_sensitive(&mut self, val: bool) {
319+
pub const fn set_sensitive(&mut self, val: bool) {
315320
self.is_sensitive = val;
316321
}
317322

@@ -768,3 +773,18 @@ fn test_debug() {
768773
sensitive.set_sensitive(true);
769774
assert_eq!("Sensitive", format!("{:?}", sensitive));
770775
}
776+
777+
#[test]
778+
fn test_const_context() {
779+
let val = const { HeaderValue::from_static("content") };
780+
assert_eq!("content", val.to_str().unwrap());
781+
assert_eq!("\"content\"", format!("{val:?}"));
782+
783+
let val = const {
784+
let mut val = HeaderValue::from_static("secret");
785+
val.set_sensitive(true);
786+
val
787+
};
788+
assert_eq!("secret", val.to_str().unwrap());
789+
assert_eq!("Sensitive", format!("{val:?}"));
790+
}

0 commit comments

Comments
 (0)