Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/socket-patch-cli/src/commands/hosted_unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ pub(crate) async fn run_hosted_leg(
.collect();
out.warnings
.extend(crate::commands::vlt_heal::rollback_heal(common, &unwound).await);
// `rollback` and `remove` print these on stderr and store them in
// `warnings[]`. A URL one of them quotes from the project (the key of
// a `bun patch` holds the hosted tarball's URL, grant token and all)
// is redacted here, once, for both.
for (_, detail) in &mut out.warnings {
*detail = socket_patch_core::utils::redact::redact_message(std::mem::take(detail));
}
out
}

Expand Down
8 changes: 3 additions & 5 deletions crates/socket-patch-cli/src/commands/vex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2221,12 +2221,10 @@ fn vendored_out_of_sync_detail(purl: &str, copies: &[&Path], notes: &str) -> Str
/// with the others of its kind ([`flush_warnings`]): nothing prints while
/// the run works, so its status line stays whole, and forty advisories of
/// one kind are not forty lines. Under `--silent` only the envelope copy
/// survives (warnings are not errors).
/// survives (warnings are not errors). Built with [`RunWarning::new`], so
/// a discovery note that quotes a lock entry carries no credential of it.
fn note_warning(warnings: &mut Vec<RunWarning>, _common: &GlobalArgs, code: &str, detail: String) {
warnings.push(RunWarning {
code: code.to_string(),
detail,
});
warnings.push(RunWarning::new(code, detail));
}

/// How many advisories of one kind, and how many names, a human run
Expand Down
49 changes: 44 additions & 5 deletions crates/socket-patch-cli/src/json_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,17 @@ pub struct RunWarning {
}

impl RunWarning {
/// Every URL quoted in `detail` is redacted
/// ([`socket_patch_core::utils::redact::redact_urls_in`]): a run
/// warning is stored with the `--json` document, and the hosted scan
/// and `vex` print their stderr lines from the same values. One that
/// quotes a lock entry (a key, a `resolved`, a private index) would
/// carry the credentials that entry holds. A warning built as a
/// struct literal, or printed before it is built, does not pass here.
pub fn new(code: impl Into<String>, detail: impl Into<String>) -> Self {
Self {
code: code.into(),
detail: detail.into(),
detail: socket_patch_core::utils::redact::redact_message(detail.into()),
}
}
}
Expand All @@ -789,6 +796,37 @@ mod tests {
use super::*;
use socket_patch_core::patch::sidecars::{SidecarFile, SidecarFileAction};

/// #2287: a run warning quotes lock text (a yarn lock key, a Poetry
/// source, a requirements reference). Its URLs are redacted where the
/// warning is built.
#[test]
fn a_run_warning_never_quotes_a_credential() {
let mut env = Envelope::new(Command::Scan);
env.warn(
"redirect_poetry_lock_unsupported",
"poetry.lock: refusing to replace an existing Poetry source \
(legacy https://bot:S3CRET@pypi.corp.example/simple) for six",
);
env.warn(
"redirect_requirements_direct_reference",
"requirements.txt installs six@1.16.0 from the direct reference \
https://pypi.corp.example/six-1.16.0-py2.py3-none-any.whl?token=S3CRET; \
refusing to overwrite a user-authored source (not rewritten)",
);
let details: Vec<&str> = env.warnings.iter().map(|w| w.detail.as_str()).collect();
assert_eq!(
details,
[
"poetry.lock: refusing to replace an existing Poetry source \
(legacy https://<redacted>@pypi.corp.example/simple) for six",
"requirements.txt installs six@1.16.0 from the direct reference \
https://pypi.corp.example/six-1.16.0-py2.py3-none-any.whl?token=<redacted>; \
refusing to overwrite a user-authored source (not rewritten)",
]
);
assert!(!env.to_pretty_json().contains("S3CRET"));
}

const ACTIONS: [(PatchAction, &str); 10] = [
(PatchAction::Discovered, "discovered"),
(PatchAction::Downloaded, "downloaded"),
Expand Down Expand Up @@ -1183,10 +1221,11 @@ mod tests {
v["vex"]
);

env.vex.as_mut().unwrap().warnings.push(RunWarning {
code: "product_not_iri".into(),
detail: "product is not an IRI".into(),
});
env.vex
.as_mut()
.unwrap()
.warnings
.push(RunWarning::new("product_not_iri", "product is not an IRI"));
let v = env.to_value();
assert_eq!(v["vex"]["warnings"][0]["code"], "product_not_iri");
assert_eq!(v["vex"]["warnings"][0]["detail"], "product is not an IRI");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ fn add_user_patch_keyed_by(root: &Path, url: &str) -> String {
key
}

/// The grant token of the hosted URLs these projects pin (the path level
/// before the patch uuid): a credential, so no message may carry it.
const GRANT_TOKEN: &str = "55555555-5555-4555-8555-555555555555";

/// `key` as a message names it: the package and the tarball's URL, with
/// the grant token redacted (#2287). The name, the patch uuid and the
/// file still say which `patchedDependencies` entry is meant.
fn shown(key: &str) -> String {
assert!(key.contains(GRANT_TOKEN), "{key}");
key.replace(GRANT_TOKEN, "<redacted>")
}

/// The scan's `redirect_bun_patched_dependency_skipped` warning detail.
fn skipped_detail(env: &Value) -> Option<String> {
env["redirect"]["warnings"]
Expand Down Expand Up @@ -76,7 +88,8 @@ async fn bun_patch_keyed_by_hosted_url_survives_a_superseding_rerun() {
);
assert_eq!(lock, hosted_lock, "{env:#}");
let detail = skipped_detail(&env).unwrap_or_else(|| panic!("no skip warning: {env:#}"));
assert!(detail.contains(&key), "{detail}");
assert!(detail.contains(&shown(&key)), "{detail}");
assert!(!detail.contains(GRANT_TOKEN), "{detail}");
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -98,9 +111,13 @@ async fn bun_patch_keyed_by_hosted_url_blocks_the_vendored_takeover() {
let failed = find_event(&env, "failed", Some("vendor_lock_entry_unsupported"));
assert_eq!(failed["purl"], PURL, "{env:#}");
assert!(
failed.to_string().contains(&key),
failed.to_string().contains(&shown(&key)),
"the refusal names the key: {env:#}"
);
assert!(
!failed.to_string().contains(GRANT_TOKEN),
"the refusal carries no grant token: {env:#}"
);
assert_eq!(
read(root, "bun.lock"),
hosted_lock,
Expand Down Expand Up @@ -135,9 +152,35 @@ fn bun_rollback_warns_that_a_patch_keyed_by_the_hosted_url_stops_applying() {
let detail = run_warning(&env, "redirect_bun_patched_dependency_dropped")
.unwrap_or_else(|| panic!("no drop warning: {env:#}"));
assert!(
detail.contains(&key) && detail.contains("bun patch"),
detail.contains(&shown(&key)) && detail.contains("bun patch"),
"{detail}"
);
assert!(!detail.contains(GRANT_TOKEN), "{detail}");

// The human runs print the same warning on stderr, as redacted as the
// envelope's copy.
for command in [&["rollback", "--yes"][..], &["remove", PURL, "--yes"]] {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
write_lockfile_only_hosted_project(root, HOSTED_URL);
seed_manifest_and_blob(root);
let key = add_user_patch_keyed_by(root, HOSTED_URL);
let mut args = command.to_vec();
args.extend(["--cwd", root.to_str().unwrap()]);
let (code, stdout, stderr) = super::run_cli_env(root, &args, &[]);
assert_eq!(code, 0, "{command:?}: {stderr}");
assert!(
stderr.contains(&format!(
"Warning: package.json `patchedDependencies` has `{}`",
shown(&key)
)),
"{command:?}: {stderr}"
);
assert!(
!stdout.contains(GRANT_TOKEN) && !stderr.contains(GRANT_TOKEN),
"{command:?} prints the grant token\nstdout:\n{stdout}\nstderr:\n{stderr}"
);
}
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
1 change: 1 addition & 0 deletions crates/socket-patch-cli/tests/scan/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod hosted_symlinked_files;
mod hosted_wheel_metadata_order;
mod hosted_yarn_berry_manifest;
mod hosted_yarn_classic_sha1;
mod quoted_url_credentials;
mod scan_batch_sizing_e2e;
mod scan_ecosystems_scope_e2e;
mod scan_failure_paths;
Expand Down
Loading
Loading