Repository navigation
Port scanner caller contract check from Ruby to Rust #713
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e3df3c
Port scanner caller contract check from Ruby to Rust
hyperpolymath f5b9169
Merge main into chore/port-scanner-caller-check-to-rust
claude 0c4fdcc
fix(ci): gate scripts/check-scanner-caller with rust-ci and Dependabot
claude 5e36723
fix(ci): make check-scanner-caller pass rustfmt, rustdoc and the bina…
claude 91a781b
Merge main into chore/port-scanner-caller-check-to-rust
claude e331ba2
Merge main into chore/port-scanner-caller-check-to-rust
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,10 @@ | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
| [package] | ||
|
hyperpolymath marked this conversation as resolved.
Resolved
|
||
| name = "check-scanner-caller" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| publish = false | ||
| description = "Fail closed on changes to the fleet scanner's deployed security contract." | ||
|
|
||
| [dependencies] | ||
| yaml-rust2 = "0.13.2" | ||
|
hyperpolymath marked this conversation as resolved.
Resolved
|
||
This file contains hidden or 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,199 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| //! Fail closed on changes to the fleet scanner's deployed security contract. | ||
| //! | ||
| //! Usage: check-scanner-caller \[WORKFLOW\] (default: .github/workflows/secret-scanner.yml) | ||
| //! Exits 0 when the caller matches the reviewed contract, 1 otherwise. | ||
|
|
||
| use std::collections::HashSet; | ||
| use std::process::ExitCode; | ||
|
|
||
| use yaml_rust2::parser::{Event, MarkedEventReceiver, Parser}; | ||
| use yaml_rust2::scanner::Marker; | ||
| use yaml_rust2::{Yaml, YamlLoader}; | ||
|
|
||
| const EXPECTED_PIN: &str = "hyperpolymath/proven-cicd-and-workflows/.github/workflows/secret-scanner-reusable.yml@1fd5e6371a50f0fc3687206a5adca0cdf7b39aa6"; | ||
|
|
||
| /// Walks parser events before loading. Rejects aliases, non-scalar mapping | ||
| /// keys and duplicate keys within a mapping. Keeps the first error only. | ||
| #[derive(Default)] | ||
| struct Validator { | ||
| frames: Vec<Frame>, | ||
| error: Option<String>, | ||
| } | ||
|
|
||
| enum Frame { | ||
| Seq, | ||
| Map { | ||
| keys: HashSet<String>, | ||
| expect_key: bool, | ||
| }, | ||
| } | ||
|
|
||
| impl Validator { | ||
| fn fail(&mut self, msg: &str) { | ||
| if self.error.is_none() { | ||
| self.error = Some(msg.to_string()); | ||
| } | ||
| } | ||
|
|
||
| /// Called at the start of every node. `scalar` is Some(text) for a scalar. | ||
| fn enter_node(&mut self, scalar: Option<&str>) { | ||
| let mut problem: Option<&'static str> = None; | ||
| if let Some(Frame::Map { keys, expect_key }) = self.frames.last_mut() { | ||
| if *expect_key { | ||
| match scalar { | ||
| Some(text) => { | ||
| if !keys.insert(text.to_string()) { | ||
| problem = Some("duplicate YAML key"); | ||
| } | ||
| } | ||
| None => problem = Some("non-scalar YAML key"), | ||
| } | ||
| } | ||
| *expect_key = !*expect_key; | ||
| } | ||
| if let Some(p) = problem { | ||
| self.fail(p); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl MarkedEventReceiver for Validator { | ||
| fn on_event(&mut self, ev: Event, _mark: Marker) { | ||
| if self.error.is_some() { | ||
| return; | ||
| } | ||
| match ev { | ||
| Event::Alias(_) => self.fail("YAML aliases are not supported"), | ||
| Event::Scalar(text, ..) => self.enter_node(Some(&text)), | ||
| Event::SequenceStart(..) => { | ||
| self.enter_node(None); | ||
| self.frames.push(Frame::Seq); | ||
| } | ||
| Event::MappingStart(..) => { | ||
| self.enter_node(None); | ||
| self.frames.push(Frame::Map { | ||
| keys: HashSet::new(), | ||
| expect_key: true, | ||
| }); | ||
| } | ||
| Event::SequenceEnd | Event::MappingEnd => { | ||
| self.frames.pop(); | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Order-insensitive structural equality. Scalars must match exactly, so | ||
| /// `false` does not equal `"false"`. | ||
| fn same(a: &Yaml, b: &Yaml) -> bool { | ||
| match (a, b) { | ||
| (Yaml::Hash(x), Yaml::Hash(y)) => { | ||
| x.len() == y.len() && x.iter().all(|(k, v)| y.get(k).is_some_and(|w| same(v, w))) | ||
| } | ||
| (Yaml::Array(x), Yaml::Array(y)) => { | ||
| x.len() == y.len() && x.iter().zip(y).all(|(p, q)| same(p, q)) | ||
| } | ||
| _ => a == b, | ||
| } | ||
| } | ||
|
|
||
| fn expected(text: &str) -> Yaml { | ||
| YamlLoader::load_from_str(text) | ||
| .ok() | ||
| .and_then(|mut docs| { | ||
| if docs.is_empty() { | ||
| None | ||
| } else { | ||
| Some(docs.remove(0)) | ||
| } | ||
| }) | ||
| .expect("built-in contract literal parses") | ||
| } | ||
|
|
||
| fn require_equal(actual: Option<&Yaml>, want: &Yaml, label: &str) -> Result<(), String> { | ||
| match actual { | ||
| Some(a) if same(a, want) => Ok(()), | ||
| _ => Err(format!("{label} does not match the reviewed contract")), | ||
| } | ||
| } | ||
|
|
||
| fn check(path: &str) -> Result<(), String> { | ||
| let text = std::fs::read_to_string(path).map_err(|e| format!("{path}: {e}"))?; | ||
|
|
||
| let mut validator = Validator::default(); | ||
| let mut parser = Parser::new_from_str(&text); | ||
| parser | ||
| .load(&mut validator, true) | ||
| .map_err(|e| e.to_string())?; | ||
| if let Some(e) = validator.error { | ||
| return Err(e); | ||
| } | ||
|
|
||
| let docs = YamlLoader::load_from_str(&text).map_err(|e| e.to_string())?; | ||
| let workflow = docs.first().ok_or("empty YAML")?; | ||
| let map = workflow.as_hash().ok_or("workflow must be a mapping")?; | ||
| let get = |m: &yaml_rust2::yaml::Hash, k: &str| m.get(&Yaml::String(k.to_string())).cloned(); | ||
|
|
||
| // Quoted or unquoted, the key is the string "on" here. A YAML 1.1 loader | ||
| // would read a bare `on` as true, so accept that spelling too. | ||
| let events = get(map, "on").or_else(|| map.get(&Yaml::Boolean(true)).cloned()); | ||
| require_equal( | ||
| events.as_ref(), | ||
| &expected("pull_request:\npush:\n branches: [main]\n"), | ||
| "triggers", | ||
| )?; | ||
| let perms = expected("contents: read\n"); | ||
| require_equal( | ||
| get(map, "permissions").as_ref(), | ||
| &perms, | ||
| "workflow permissions", | ||
| )?; | ||
|
|
||
| let jobs = get(map, "jobs") | ||
| .and_then(|j| j.into_hash()) | ||
| .filter(|j| j.len() == 1 && j.contains_key(&Yaml::String("scan".into()))) | ||
| .ok_or("only the scan job is expected")?; | ||
| let scan = get(&jobs, "scan") | ||
| .and_then(|s| s.into_hash()) | ||
| .filter(|s| { | ||
| let mut keys: Vec<&str> = s.keys().filter_map(|k| k.as_str()).collect(); | ||
| keys.sort_unstable(); | ||
| s.len() == 3 && keys == ["permissions", "uses", "with"] | ||
| }) | ||
| .ok_or("unexpected scan job fields")?; | ||
|
|
||
| require_equal( | ||
| get(&scan, "permissions").as_ref(), | ||
| &perms, | ||
| "job permissions", | ||
| )?; | ||
| require_equal( | ||
| get(&scan, "uses").as_ref(), | ||
| &Yaml::String(EXPECTED_PIN.into()), | ||
| "suite pin", | ||
| )?; | ||
| require_equal( | ||
| get(&scan, "with").as_ref(), | ||
| &expected("fast-pr: false\n"), | ||
| "scan policy", | ||
| )?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn main() -> ExitCode { | ||
| let path = std::env::args() | ||
| .nth(1) | ||
| .unwrap_or_else(|| ".github/workflows/secret-scanner.yml".into()); | ||
| match check(&path) { | ||
| Ok(()) => { | ||
| println!("scanner caller contract: exact suite pin, full-history policy, triggers, least privilege and no secrets verified"); | ||
| ExitCode::SUCCESS | ||
| } | ||
| Err(e) => { | ||
| eprintln!("scanner caller contract: {e}"); | ||
| ExitCode::FAILURE | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.