-
-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): support user-configurable secrets for `oxc-security/api…
…-keys`
- Loading branch information
Showing
5 changed files
with
148 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
crates/oxc_linter/src/rules/security/api_keys/secrets/custom.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::{borrow::Cow, num::NonZeroU32}; | ||
|
||
use regex::Regex; | ||
|
||
use oxc_span::CompactStr; | ||
|
||
use super::{Secret, SecretScanner, SecretScannerMeta}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct CustomSecret { | ||
pub(crate) rule_name: CompactStr, | ||
pub(crate) message: CompactStr, | ||
pub(crate) entropy: f32, | ||
pub(crate) min_len: NonZeroU32, | ||
pub(crate) max_len: Option<NonZeroU32>, | ||
pub(crate) pattern: Regex, | ||
} | ||
|
||
// // This default impl doesn't make logical sense, but it's useful for building structs out of | ||
// // partial configs. | ||
// impl Default for CustomSecret { | ||
// fn default() -> Self { | ||
// Self { | ||
// rule_name: | ||
// entropy: DEFAULT_MIN_ENTROPY, | ||
// min_len: DEFAULT_MIN_LEN, | ||
// message: String, | ||
// max_len: None, | ||
// pattern: Regex::new("").unwrap(), | ||
// } | ||
// } | ||
// } | ||
|
||
impl SecretScannerMeta for CustomSecret { | ||
fn rule_name(&self) -> Cow<'static, str> { | ||
self.rule_name.clone().into() | ||
} | ||
fn message(&self) -> Cow<'static, str> { | ||
self.message.clone().into() | ||
} | ||
fn min_len(&self) -> NonZeroU32 { | ||
self.min_len | ||
} | ||
fn max_len(&self) -> Option<NonZeroU32> { | ||
self.max_len | ||
} | ||
fn min_entropy(&self) -> f32 { | ||
self.entropy | ||
} | ||
} | ||
|
||
impl SecretScanner for CustomSecret { | ||
fn detect(&self, candidate: &Secret<'_>) -> bool { | ||
self.pattern.is_match(candidate) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters