feat(api): require an admin token for configuration changes
Group CRUD decides what the scanner sweeps, yet any client that reached the API could change it. Writes now need XEXEU_ADMIN_TOKEN (or a token generated at startup and printed as a ?token= link, like sabia ui), compared in constant time, and a browser Origin must match Host. The dashboard keeps the token for the tab and asks for it on 401. Reads are unchanged. Also track .env.example: '.env*' in .gitignore had kept it out of the repository, although the README tells operators to copy it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
36eadabc7c
commit
90ece32dac
17 files changed
+369
-28
No files matched your search
@@ -15,7 +15,21 @@ impl Drop for ChildGuard {
|
||||
}
|
||||
}
|
||||
|
||||
const ADMIN_TOKEN: &str = "e2e-admin-token-0123456789";
|
||||
|
||||
fn http_request(addr: &str, method: &str, path: &str, body: Option<&str>) -> (u16, String, String) {
|
||||
let auth = format!("Authorization: Bearer {ADMIN_TOKEN}\r\n");
|
||||
http_request_with(addr, method, path, body, &auth)
|
||||
}
|
||||
|
||||
/// `extra_headers` is a raw block of `Name: value\r\n` lines.
|
||||
fn http_request_with(
|
||||
addr: &str,
|
||||
method: &str,
|
||||
path: &str,
|
||||
body: Option<&str>,
|
||||
extra_headers: &str,
|
||||
) -> (u16, String, String) {
|
||||
let mut stream = TcpStream::connect(addr).expect("connect to server");
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||
@@ -23,7 +37,7 @@ fn http_request(addr: &str, method: &str, path: &str, body: Option<&str>) -> (u1
|
||||
|
||||
let payload = body.unwrap_or("");
|
||||
let request = format!(
|
||||
"{method} {path} HTTP/1.1\r\nHost: {addr}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{payload}",
|
||||
"{method} {path} HTTP/1.1\r\nHost: {addr}\r\n{extra_headers}Content-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{payload}",
|
||||
payload.len()
|
||||
);
|
||||
stream.write_all(request.as_bytes()).expect("write request");
|
||||
@@ -87,6 +101,7 @@ alvos = ["127.0.0.1/32"]
|
||||
"--data-dir",
|
||||
data_dir.to_str().expect("data dir"),
|
||||
])
|
||||
.env("XEXEU_ADMIN_TOKEN", ADMIN_TOKEN)
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::inherit())
|
||||
.spawn()
|
||||
@@ -118,6 +133,46 @@ alvos = ["127.0.0.1/32"]
|
||||
assert!(body.contains("\"escopo\""));
|
||||
assert!(body.contains("\"inicial\""));
|
||||
|
||||
// Writes need the admin token; a browser Origin must match Host.
|
||||
let group = r#"{"nome": "sem-token", "alvos": ["127.0.0.1"]}"#;
|
||||
let (code, headers, _) =
|
||||
http_request_with(&addr, "POST", "/api/config/groups", Some(group), "");
|
||||
assert_eq!(code, 401);
|
||||
assert!(headers.contains("www-authenticate: Bearer"));
|
||||
let wrong = "Authorization: Bearer e2e-admin-token-0123456788\r\n";
|
||||
let (code, _, _) = http_request_with(&addr, "POST", "/api/config/groups", Some(group), wrong);
|
||||
assert_eq!(code, 401);
|
||||
let (code, _, _) = http_request_with(&addr, "DELETE", "/api/config/groups/inicial", None, "");
|
||||
assert_eq!(code, 401);
|
||||
let cross_site =
|
||||
format!("Authorization: Bearer {ADMIN_TOKEN}\r\nOrigin: https://evil.example\r\n");
|
||||
let (code, _, _) = http_request_with(
|
||||
&addr,
|
||||
"POST",
|
||||
"/api/config/groups",
|
||||
Some(group),
|
||||
&cross_site,
|
||||
);
|
||||
assert_eq!(code, 403);
|
||||
let same_site = format!("X-Xexeu-Token: {ADMIN_TOKEN}\r\nOrigin: http://{addr}\r\n");
|
||||
let (code, _, _) = http_request_with(
|
||||
&addr,
|
||||
"POST",
|
||||
"/api/config/groups",
|
||||
Some(r#"{"nome": "fora", "alvos": ["192.168.1.1"]}"#),
|
||||
&same_site,
|
||||
);
|
||||
assert_eq!(
|
||||
code, 400,
|
||||
"same-origin request with the custom header reaches the handler"
|
||||
);
|
||||
let (code, _, body) = http_request(&addr, "GET", "/api/config", None);
|
||||
assert_eq!(code, 200);
|
||||
assert!(
|
||||
!body.contains("sem-token"),
|
||||
"rejected writes must not change the config"
|
||||
);
|
||||
|
||||
// 3. Test POST /api/config/groups - out of scope rejection
|
||||
let (code, _, body) = http_request(
|
||||
&addr,
|
||||
|
||||
Reference in new issue
Block a user