feat(secrets): Android signing passwords and Firebase keys; our gitleaks config only (#456)
This commit was merged in pull request #456.
This commit is contained in:
1 parent
d98cbbada4
commit
6c9756bebf
2 files changed
+210
-1
No files matched your search
@@ -0,0 +1,39 @@
|
||||
# Prüfwerk's gitleaks configuration: gitleaks' own rules plus the ones below.
|
||||
# Always passed with --config, so a `.gitleaks.toml` in a scanned repository
|
||||
# cannot switch rules off.
|
||||
|
||||
title = "Prüfwerk"
|
||||
|
||||
[extend]
|
||||
useDefault = true
|
||||
|
||||
# Android release signing: a keystore or key password written into a Gradle
|
||||
# build file signs every build with a secret everyone with the code has.
|
||||
[[rules]]
|
||||
id = "android-keystore-password"
|
||||
description = "Android signing password in a Gradle build file"
|
||||
regex = '''(?i)\b(?:storePassword|keyPassword)\b\s*(?:=|\()?\s*["']([^"'$\s]{4,})["']'''
|
||||
path = '''(?i)\.gradle(?:\.kts)?$'''
|
||||
secretGroup = 1
|
||||
keywords = ["storepassword", "keypassword"]
|
||||
|
||||
# The same, kept in gradle.properties (RELEASE_STORE_PASSWORD=…).
|
||||
[[rules]]
|
||||
id = "android-signing-password-property"
|
||||
description = "Android signing password in gradle.properties"
|
||||
regex = '''(?im)^\s*[\w.]*(?:store|key|keystore|signing)[\w.]*password\s*[=:]\s*([^\s$]{4,})\s*$'''
|
||||
path = '''(?i)(?:^|/)gradle\.properties$'''
|
||||
secretGroup = 1
|
||||
keywords = ["password"]
|
||||
|
||||
# The API key in a Firebase configuration (google-services.json,
|
||||
# GoogleService-Info.plist). It identifies the app rather than authorising
|
||||
# anything on its own, but it must be restricted to the app in the Google
|
||||
# Cloud console; reported at low severity.
|
||||
[[rules]]
|
||||
id = "firebase-config-api-key"
|
||||
description = "Firebase API key in the app's Firebase configuration"
|
||||
regex = '''(AIza[\w-]{35})'''
|
||||
path = '''(?i)(?:^|/)(?:google-services\.json|GoogleService-Info\.plist)$'''
|
||||
secretGroup = 1
|
||||
keywords = ["aiza"]
|
||||
@@ -8,6 +8,48 @@ use crate::pipeline::dedup;
|
||||
|
||||
pub struct GitleaksScanner;
|
||||
|
||||
/// gitleaks' own rules plus ours (Android signing passwords, Firebase API
|
||||
/// keys), passed with `--config` on every run.
|
||||
const RULES: &str = include_str!("../../rules/gitleaks.toml");
|
||||
|
||||
/// Our rule for the API key of a Firebase configuration file.
|
||||
const FIREBASE_RULE: &str = "firebase-config-api-key";
|
||||
|
||||
/// The Firebase configuration files [`FIREBASE_RULE`] covers; gitleaks' own
|
||||
/// `gcp-api-key` reports the same key there, once is enough.
|
||||
const FIREBASE_CONFIGS: [&str; 2] = ["google-services.json", "googleservice-info.plist"];
|
||||
|
||||
/// Whether a result only repeats what [`FIREBASE_RULE`] reports.
|
||||
fn repeats_firebase_rule(rule_id: &str, file: &str) -> bool {
|
||||
let name = file.rsplit('/').next().unwrap_or(file).to_lowercase();
|
||||
rule_id == "gcp-api-key" && FIREBASE_CONFIGS.contains(&name.as_str())
|
||||
}
|
||||
|
||||
/// The configuration of one run, in a temporary directory of its own (a
|
||||
/// shared file would let concurrent scans pull it from under each other),
|
||||
/// removed on drop.
|
||||
struct RunConfig(std::path::PathBuf);
|
||||
|
||||
impl RunConfig {
|
||||
fn write() -> std::io::Result<Self> {
|
||||
let dir = std::env::temp_dir().join(format!("cs-gitleaks-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&dir)?;
|
||||
let config = Self(dir);
|
||||
std::fs::write(config.file(), RULES)?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
fn file(&self) -> std::path::PathBuf {
|
||||
self.0.join("gitleaks.toml")
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RunConfig {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// How to point gitleaks at a tree. `detect` walks a git checkout's history;
|
||||
/// on a directory that is not a checkout it scans **nothing** and reports no
|
||||
/// leaks — a silent miss — so a plain tree (an uploaded source archive) is
|
||||
@@ -31,10 +73,19 @@ impl Scanner for GitleaksScanner {
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn scan(&self, repo_path: &Path, repo_id: &str) -> Result<ScanOutput, CoreError> {
|
||||
// Our configuration, always: without --config gitleaks reads a
|
||||
// `.gitleaks.toml` from the scanned repository, which could switch
|
||||
// rules off.
|
||||
let config = RunConfig::write().map_err(|e| CoreError::Scanner {
|
||||
scanner: "gitleaks".to_string(),
|
||||
source: Box::new(e),
|
||||
})?;
|
||||
let output = tokio::time::timeout(
|
||||
std::time::Duration::from_secs(300),
|
||||
crate::pipeline::tool_cmd::tool("gitleaks", &[])
|
||||
.args(source_args(repo_path))
|
||||
.arg("--config")
|
||||
.arg(config.file())
|
||||
.args([
|
||||
"--report-format",
|
||||
"json",
|
||||
@@ -67,8 +118,12 @@ impl Scanner for GitleaksScanner {
|
||||
let findings = results
|
||||
.into_iter()
|
||||
.filter(|r| !is_allowlisted(&r.file))
|
||||
.filter(|r| !repeats_firebase_rule(&r.rule_id, &r.file))
|
||||
.map(|r| {
|
||||
let severity = match r.rule_id.as_str() {
|
||||
// It identifies the app; the risk is a key left
|
||||
// unrestricted, not the key being known.
|
||||
FIREBASE_RULE => Severity::Low,
|
||||
s if s.contains("private-key") => Severity::Critical,
|
||||
s if s.contains("token") || s.contains("password") || s.contains("secret") => {
|
||||
Severity::High
|
||||
@@ -85,13 +140,22 @@ impl Scanner for GitleaksScanner {
|
||||
]);
|
||||
|
||||
let title = format!("Secret detected: {}", r.description);
|
||||
let description = format!(
|
||||
let mut description = format!(
|
||||
"Potential secret ({}) found in {}:{}. Match: {}",
|
||||
r.rule_id,
|
||||
r.file,
|
||||
r.start_line,
|
||||
r.r#match.chars().take(80).collect::<String>(),
|
||||
);
|
||||
if r.rule_id == FIREBASE_RULE {
|
||||
description.push_str(
|
||||
"\n\nA Firebase API key in the app's configuration is shipped with the \
|
||||
app and identifies it; it is not a secret on its own. Restrict it to the \
|
||||
app (Android package and signing certificate, iOS bundle id) and to the \
|
||||
APIs it needs in the Google Cloud console, so it cannot be used \
|
||||
elsewhere.",
|
||||
);
|
||||
}
|
||||
|
||||
let mut finding = Finding::new(
|
||||
repo_id.to_string(),
|
||||
@@ -151,6 +215,112 @@ struct GitleaksResult {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_firebase_key_is_reported_once() {
|
||||
assert!(repeats_firebase_rule(
|
||||
"gcp-api-key",
|
||||
"app/google-services.json"
|
||||
));
|
||||
assert!(repeats_firebase_rule(
|
||||
"gcp-api-key",
|
||||
"ios/PumpApp/GoogleService-Info.plist"
|
||||
));
|
||||
assert!(!repeats_firebase_rule("gcp-api-key", "src/maps.js"));
|
||||
assert!(!repeats_firebase_rule(
|
||||
FIREBASE_RULE,
|
||||
"app/google-services.json"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn our_rules_extend_gitleaks_own() {
|
||||
let config: toml::Table = RULES.parse().expect("valid TOML");
|
||||
assert_eq!(config["extend"]["useDefault"].as_bool(), Some(true));
|
||||
let ids: Vec<&str> = config["rules"]
|
||||
.as_array()
|
||||
.expect("rules")
|
||||
.iter()
|
||||
.filter_map(|r| r["id"].as_str())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
ids,
|
||||
[
|
||||
"android-keystore-password",
|
||||
"android-signing-password-property",
|
||||
FIREBASE_RULE
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// With the real gitleaks, where installed: signing passwords and
|
||||
/// Firebase keys are found, and a repository's own `.gitleaks.toml`
|
||||
/// changes nothing. (A `.gitleaksignore` in the repository is honoured
|
||||
/// by gitleaks whatever it is told, like an inline `gitleaks:allow`.)
|
||||
#[tokio::test]
|
||||
async fn mobile_secrets_are_found_whatever_the_repository_configures() {
|
||||
if std::process::Command::new("gitleaks")
|
||||
.arg("version")
|
||||
.output()
|
||||
.is_err()
|
||||
{
|
||||
eprintln!("SKIP: gitleaks not installed");
|
||||
return;
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!("cs-leaks-{}", uuid::Uuid::new_v4()));
|
||||
let files = [
|
||||
(
|
||||
"app/build.gradle",
|
||||
"signingConfigs { release {\n storePassword \"Pump-Release-2024!\"\n keyPassword System.getenv(\"KEY_PASSWORD\")\n} }\n",
|
||||
),
|
||||
("gradle.properties", "RELEASE_KEY_PASSWORD=Pump-Key-2024!\n"),
|
||||
(
|
||||
"app/google-services.json",
|
||||
"{ \"api_key\": [{ \"current_key\": \"AIzaSyD3x9mK2pQ7rT1vW4yZ8bC6nF0hJ5lL2aB\" }] }\n",
|
||||
),
|
||||
// A repository trying to switch everything off.
|
||||
(".gitleaks.toml", "[allowlist]\npaths = [\".*\"]\n"),
|
||||
];
|
||||
for (path, content) in files {
|
||||
let full = dir.join(path);
|
||||
std::fs::create_dir_all(full.parent().expect("parent")).expect("mkdir");
|
||||
std::fs::write(full, content).expect("write");
|
||||
}
|
||||
let output = GitleaksScanner.scan(&dir, "t").await.expect("scan");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let mut found: Vec<(String, String, Severity)> = output
|
||||
.findings
|
||||
.into_iter()
|
||||
.map(|f| {
|
||||
(
|
||||
f.rule_id.unwrap_or_default(),
|
||||
f.file_path.unwrap_or_default(),
|
||||
f.severity,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
found.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
assert_eq!(
|
||||
found,
|
||||
[
|
||||
(
|
||||
"android-keystore-password".to_string(),
|
||||
"app/build.gradle".to_string(),
|
||||
Severity::High
|
||||
),
|
||||
(
|
||||
"android-signing-password-property".to_string(),
|
||||
"gradle.properties".to_string(),
|
||||
Severity::High
|
||||
),
|
||||
(
|
||||
FIREBASE_RULE.to_string(),
|
||||
"app/google-services.json".to_string(),
|
||||
Severity::Low
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// --- is_allowlisted tests ---
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user