use std::collections::BTreeSet;
use super::super::files::Lock;
use super::super::{Change, Hit, INSTALL_SCRIPT, Level, PACKAGE_NEAR_MISS, shown};
use crate::packages::near_miss;
pub(super) fn name(lock: Lock, text: &str) -> Option<String> {
let t = text.trim();
let name = match lock {
Lock::Npm => {
let key = t.strip_prefix('"')?.split('"').next()?;
let (_, last) = key.rsplit_once("node_modules/")?;
t[key.len() + 2..]
.trim_start()
.starts_with(':')
.then_some(last)?
}
Lock::Pnpm => {
let indent = text.len() - text.trim_start().len();
let key = t.strip_suffix(':')?.trim_matches(['\'', '"']);
let key = key.strip_prefix('/').unwrap_or(key);
(indent == 2).then_some(())?;
before_version(key)?
}
Lock::Yarn => {
(!text.starts_with([' ', '\t', '#'])).then_some(())?;
let first = t
.strip_suffix(':')?
.split(',')
.next()?
.trim()
.trim_matches('"');
before_version(first)?
}
Lock::Cargo | Lock::Python => {
let value = t.strip_prefix("name = \"")?.strip_suffix('"')?;
(!value.is_empty()).then_some(value)?
}
Lock::Go => {
let mut words = t.split_whitespace();
let module = words.next()?;
words.next()?.starts_with('v').then_some(module)?
}
};
Some(name.to_owned())
}
fn before_version(key: &str) -> Option<&str> {
let at = key.get(1..)?.find('@')? + 1;
Some(&key[..at])
}
pub(super) fn scan(lock: Lock, change: &Change<'_>, hits: &mut Vec<Hit>) {
let ecosystem = lock.ecosystem();
let mut seen = BTreeSet::new();
let mut entry: Option<String> = None;
let mut previous = None;
for (line, text) in change.added {
if previous.is_none_or(|p: usize| p + 1 != *line) {
entry = None;
}
previous = Some(*line);
if let Some(name) = name(lock, text) {
if seen.insert(name.clone())
&& let Some(popular) = near_miss(ecosystem, &name)
{
hits.push(Hit::new(
PACKAGE_NEAR_MISS,
Level::Review,
*line,
format!(
"locks the {} package `{}`, one typo or trick away from the popular \
`{popular}`: a dependency may pull in a look-alike that no manifest \
names; check which one is meant",
ecosystem.id(),
shown(&name)
),
));
}
entry = Some(name);
}
if runs_script(lock, text) {
let what = entry.as_deref().map_or_else(
|| "a locked package".to_owned(),
|n| format!("`{}`", shown(n)),
);
hits.push(Hit::new(
INSTALL_SCRIPT,
Level::Review,
*line,
format!(
"records an install script for {what}: the next install runs it, for a \
package a dependency pulls in too"
),
));
}
}
}
fn runs_script(lock: Lock, text: &str) -> bool {
let t = text.trim().trim_end_matches(',');
match lock {
Lock::Npm => t == "\"hasInstallScript\": true",
Lock::Pnpm => t == "requiresBuild: true",
Lock::Yarn | Lock::Cargo | Lock::Python | Lock::Go => false,
}
}