mirror of
https://github.com/usestrix/firewall.git
synced 2026-11-09 18:08:49 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ba37450b9 |
No files matched your search
@@ -1,6 +1,7 @@
|
|||||||
use crate::policies::registries::{Registry, BUILT_IN};
|
use crate::policies::registries::{Registry, BUILT_IN};
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
@@ -16,12 +17,22 @@ pub struct FirewallConfig {
|
|||||||
/// 0 turns the age check off.
|
/// 0 turns the age check off.
|
||||||
pub min_age_hours: u64,
|
pub min_age_hours: u64,
|
||||||
pub denylist: DenylistSource,
|
pub denylist: DenylistSource,
|
||||||
|
/// Packages never screened, by ecosystem: `{"npm": [{"name": "left-pad"}]}`.
|
||||||
|
pub allow: HashMap<String, Vec<AllowedPackage>>,
|
||||||
pub telemetry: bool,
|
pub telemetry: bool,
|
||||||
/// Not describable in the file yet, so this is always the built-in table.
|
/// Not describable in the file yet, so this is always the built-in table.
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub registries: Vec<&'static Registry>,
|
pub registries: Vec<&'static Registry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
pub struct AllowedPackage {
|
||||||
|
pub name: String,
|
||||||
|
/// Every version when absent.
|
||||||
|
pub version: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
#[serde(default, deny_unknown_fields)]
|
#[serde(default, deny_unknown_fields)]
|
||||||
pub struct DenylistSource {
|
pub struct DenylistSource {
|
||||||
@@ -47,6 +58,7 @@ impl Default for FirewallConfig {
|
|||||||
fail_open: true,
|
fail_open: true,
|
||||||
min_age_hours: 0,
|
min_age_hours: 0,
|
||||||
denylist: DenylistSource::default(),
|
denylist: DenylistSource::default(),
|
||||||
|
allow: HashMap::new(),
|
||||||
telemetry: true,
|
telemetry: true,
|
||||||
registries: BUILT_IN.to_vec(),
|
registries: BUILT_IN.to_vec(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ pub async fn consult(
|
|||||||
registry: &'static Registry,
|
registry: &'static Registry,
|
||||||
pkg: &Package,
|
pkg: &Package,
|
||||||
) -> Option<(&'static str, Verdict)> {
|
) -> Option<(&'static str, Verdict)> {
|
||||||
|
if allowed(firewall, registry, pkg) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
let mut unsure = None;
|
let mut unsure = None;
|
||||||
for filter in firewall.filters() {
|
for filter in firewall.filters() {
|
||||||
match filter.check(registry, pkg).await {
|
match filter.check(registry, pkg).await {
|
||||||
@@ -41,3 +44,18 @@ pub async fn consult(
|
|||||||
}
|
}
|
||||||
unsure
|
unsure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn allowed(firewall: &Firewall, registry: &Registry, pkg: &Package) -> bool {
|
||||||
|
let Some(entries) = firewall.config.allow.get(registry.eco) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
entries.iter().any(|entry| {
|
||||||
|
let name_matches = match registry.normalize {
|
||||||
|
Some(normalize) => normalize(&entry.name) == pkg.name,
|
||||||
|
None => entry.name == pkg.name,
|
||||||
|
};
|
||||||
|
// Go writes a leading v that the parsed version drops, and PEP 440 ignores one.
|
||||||
|
let version = entry.version.as_deref().map(|v| v.strip_prefix('v').unwrap_or(v));
|
||||||
|
name_matches && (version.is_none() || version == pkg.version.as_deref())
|
||||||
|
})
|
||||||
|
}
|
||||||
+14
-1
@@ -1,5 +1,7 @@
|
|||||||
use firewall::config::path;
|
use firewall::config::path;
|
||||||
use firewall::FirewallConfig;
|
use firewall::policies::filters::consult;
|
||||||
|
use firewall::policies::Package;
|
||||||
|
use firewall::{Firewall, FirewallConfig};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_file_only_has_to_set_what_it_changes() {
|
fn a_file_only_has_to_set_what_it_changes() {
|
||||||
@@ -19,3 +21,14 @@ fn the_config_lives_under_the_config_directory() {
|
|||||||
let path = path().expect("a home directory");
|
let path = path().expect("a home directory");
|
||||||
assert!(path.ends_with("config.json"), "{}", path.display());
|
assert!(path.ends_with("config.json"), "{}", path.display());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn an_allowed_version_skips_the_filters() {
|
||||||
|
// An unreachable denylist would otherwise leave the package unchecked.
|
||||||
|
let config = r#"{"allow": {"go": [{"name": "golang.org/x/net", "version": "v0.20.0"}]}, "denylist": {"base": "http://127.0.0.1:1"}}"#;
|
||||||
|
let firewall = Firewall::new(serde_json::from_str(config).expect("parses"));
|
||||||
|
let (registry, _) = firewall.match_on("/go/golang.org/x/net/@v/list", |r| r.prefix).expect("go");
|
||||||
|
let pkg = |version: &str| Package { name: "golang.org/x/net".into(), version: Some(version.into()) };
|
||||||
|
assert!(consult(&firewall, registry, &pkg("0.20.0")).await.is_none());
|
||||||
|
assert!(consult(&firewall, registry, &pkg("0.21.0")).await.is_some());
|
||||||
|
}
|
||||||
Reference in new issue
Block a user