windows: the component manager, verified programs, a Wine test

components.json pins sing-box, XRay, Flowseal's zapret, bol-van's
winws2 bundle with nfqws2-keenetic's strategies, and tg-ws-proxy (built
with the release): release assets by archive hash, commit snapshots by
the hash of every file taken. The service installs them (admin only),
staged and swapped in, refusing archive paths that leave the directory,
and checks each program against the hashes recorded at install before
running it. stage-components does the same for building a release.

The state root is created with its protected descriptor in one call,
or, existing, checked through a handle that does not follow links.

tools/wine-e2e.sh runs the Windows build under Wine: the service, the
named pipe, impersonation, the component check, rendering and the
supervisor all work; Wine lacks an iphlpapi call sing-box needs, so
proxying itself needs real Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 5.5 committed 2026-10-26 23:33:55 +03:00
1 parent b878dd93db
commit 68b496cafa
10 files changed
+709 -81

No files matched your search

+122
View File
@@ -0,0 +1,122 @@
//! The components: the backend programs the service runs, pinned by version and hash
//! (`windows/components.json`). Each is fetched from its project's own Windows release and
//! verified before anything from it runs.
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Manifest {
pub components: BTreeMap<String, Component>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Component {
pub version: String,
pub license: String,
pub homepage: String,
/// Built with the release, not fetched from a source of its own.
#[serde(default)]
pub bundled: bool,
pub sources: Vec<Source>,
}
/// One archive and what to take from it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Source {
pub url: String,
/// The archive's SHA-256, where the archive is stable (a release asset).
#[serde(default)]
pub sha256: Option<String>,
/// The leading path every taken entry has, removed.
#[serde(default)]
pub strip: String,
/// Where below the component's directory the entries go.
#[serde(default)]
pub dest: String,
/// Only these entries (exact paths, or directories ending in `/`).
#[serde(default)]
pub include: Option<Vec<String>>,
/// Not these.
#[serde(default)]
pub exclude: Vec<String>,
/// Exactly these entries, each with its SHA-256: for an archive that is not byte-stable
/// (a commit snapshot), what is verified is what is taken.
#[serde(default)]
pub files: Option<BTreeMap<String, String>>,
}
/// The manifest this build pins.
pub fn manifest() -> Manifest {
serde_json::from_str(include_str!("../../../components.json")).expect("components.json is valid")
}
fn matches(list: &[String], rel: &str) -> bool {
list.iter().any(|p| if p.ends_with('/') { rel.starts_with(p.as_str()) } else { rel == p })
}
/// A relative path made of plain names only: no root, no drive, no `..`, no `.`.
pub fn safe_relative(rel: &str) -> bool {
!rel.is_empty()
&& !rel.starts_with(['/', '\\'])
&& !rel.contains(':')
&& rel.split(['/', '\\']).all(|part| !part.is_empty() && part != "." && part != "..")
}
impl Source {
/// Where an archive entry goes below the component's directory, or `None` to leave it.
pub fn place(&self, entry: &str) -> Option<String> {
if entry.ends_with('/') {
return None;
}
let rel = entry.strip_prefix(self.strip.as_str())?;
if let Some(files) = &self.files {
if !files.contains_key(rel) {
return None;
}
}
if let Some(include) = &self.include {
if !matches(include, rel) {
return None;
}
}
if matches(&self.exclude, rel) {
return None;
}
let out = format!("{}{rel}", self.dest);
safe_relative(&out).then_some(out)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_manifest_reads_and_places_entries() {
let m = manifest();
let sb = &m.components["sing-box"].sources[0];
assert_eq!(sb.place("sing-box-1.14.4-windows-amd64/sing-box.exe").as_deref(), Some("sing-box.exe"));
assert_eq!(sb.place("other/sing-box.exe"), None);
let z = &m.components["zapret"].sources[0];
assert_eq!(z.place("zapret-discord-youtube-1.10.4/bin/winws.exe").as_deref(), Some("bin/winws.exe"));
assert_eq!(z.place("zapret-discord-youtube-1.10.4/service.bat"), None);
assert_eq!(z.place("zapret-discord-youtube-1.10.4/utils/targets.txt"), None);
let k = &m.components["zapret2"].sources[1];
assert_eq!(k.place(&format!("{}nfqws2.conf", k.strip)).as_deref(), Some("keenetic/nfqws2.conf"));
assert!(m.components["tg-ws-proxy"].bundled);
}
#[test]
fn archive_paths_cannot_leave_the_directory() {
for bad in ["../x", "a/../b", "/etc/x", "C:/x", "a\\..\\b", "", "a//b"] {
assert!(!safe_relative(bad), "{bad}");
}
assert!(safe_relative("bin/winws.exe"));
let s = Source { url: String::new(), sha256: None, strip: "p/".into(), dest: String::new(), include: None, exclude: vec![], files: None };
assert_eq!(s.place("p/../../evil.exe"), None);
}
}
+1
View File
@@ -4,6 +4,7 @@
//! subscription parsing, and the sing-box and XRay configs the service runs. Nothing here
//! touches the network, the file system or processes; the service does that.
pub mod components;
pub mod config;
pub mod derived;
pub mod duration;