-
Notifications
You must be signed in to change notification settings - Fork 1
Release diff: master → previous_release #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| //! Shared authentication utilities for API routes. | ||
| //! | ||
| //! This module provides common auth functions used across routes to ensure | ||
| //! consistent security practices like constant-time comparison. | ||
|
|
||
| use axum::http::HeaderMap; | ||
| use sha2::Digest; | ||
| use subtle::ConstantTimeEq; | ||
|
|
||
| /// Extracts and parses a Bearer token from the Authorization header. | ||
| /// | ||
| /// Returns `Some(token)` if a valid "Bearer <token>" header is found, | ||
| /// `None` otherwise. | ||
| pub fn parse_bearer_token(headers: &HeaderMap) -> Option<String> { | ||
| let auth = headers | ||
| .get("authorization") | ||
| .and_then(|v| v.to_str().ok()) | ||
| .unwrap_or("") | ||
| .trim(); | ||
| let prefix = "bearer "; | ||
| if auth.len() <= prefix.len() { | ||
| return None; | ||
| } | ||
| if !auth[..prefix.len()].eq_ignore_ascii_case(prefix) { | ||
| return None; | ||
| } | ||
| Some(auth[prefix.len()..].trim().to_string()) | ||
| } | ||
|
|
||
| /// Computes SHA-256 hash of the input and returns it as a hex string. | ||
| pub fn sha256_hex(input: &str) -> String { | ||
| let mut h = sha2::Sha256::new(); | ||
| h.update(input.as_bytes()); | ||
| let out = h.finalize(); | ||
| hex::encode(out) | ||
| } | ||
|
|
||
| /// Performs a constant-time comparison of two strings to prevent timing attacks. | ||
| /// | ||
| /// Returns `true` if the strings are equal, `false` otherwise. | ||
| /// The comparison time is constant regardless of how many characters match. | ||
| pub fn constant_time_eq(a: &str, b: &str) -> bool { | ||
| let a_bytes = a.as_bytes(); | ||
| let b_bytes = b.as_bytes(); | ||
|
|
||
| // Length check is unavoidable, but we still do a comparison | ||
| // to maintain constant time behavior | ||
| let len_match = a_bytes.len() == b_bytes.len(); | ||
|
|
||
| if len_match { | ||
| a_bytes.ct_eq(b_bytes).into() | ||
| } else { | ||
| // Do a dummy comparison to maintain constant time | ||
| let dummy = vec![0u8; b_bytes.len()]; | ||
| let _ = dummy.as_slice().ct_eq(b_bytes); | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /// Validates a token hash against a stored hash using constant-time comparison. | ||
| /// | ||
| /// Returns `true` if the hashes match, `false` otherwise. | ||
| pub fn validate_token_hash(provided_hash: &str, stored_hash: &str) -> bool { | ||
| constant_time_eq(provided_hash, stored_hash) | ||
| } | ||
|
|
||
| /// Extract server address for dashboard ping feature. | ||
| /// Priority: | ||
| /// 1. X-Server-Address header (explicit config from plugin) | ||
| /// 2. X-Forwarded-For header (if behind a proxy) | ||
| /// 3. X-Real-IP header (common proxy header) | ||
| pub fn extract_server_address(headers: &HeaderMap) -> Option<String> { | ||
| // 1. Explicit header from plugin config takes priority | ||
| if let Some(addr) = headers | ||
| .get("x-server-address") | ||
| .and_then(|v| v.to_str().ok()) | ||
| .map(|s| s.trim().to_string()) | ||
| .filter(|s| !s.is_empty()) | ||
| { | ||
| tracing::debug!(address = %addr, "using explicit X-Server-Address"); | ||
| return Some(addr); | ||
| } | ||
|
|
||
| // 2. X-Forwarded-For (first IP in the chain, closest to client) | ||
| if let Some(forwarded) = headers | ||
| .get("x-forwarded-for") | ||
| .and_then(|v| v.to_str().ok()) | ||
| { | ||
| // X-Forwarded-For can be comma-separated: "client, proxy1, proxy2" | ||
| if let Some(first_ip) = forwarded.split(',').next().map(|s| s.trim()) { | ||
| if !first_ip.is_empty() && !is_local_ip(first_ip) { | ||
| tracing::debug!(ip = %first_ip, "using X-Forwarded-For for server address"); | ||
| // Add default MC port | ||
| return Some(format!("{}:25565", first_ip)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 3. X-Real-IP (single IP header, common with nginx) | ||
| if let Some(real_ip) = headers | ||
| .get("x-real-ip") | ||
| .and_then(|v| v.to_str().ok()) | ||
| .map(|s| s.trim()) | ||
| .filter(|s| !s.is_empty() && !is_local_ip(s)) | ||
| { | ||
| tracing::debug!(ip = %real_ip, "using X-Real-IP for server address"); | ||
| return Some(format!("{}:25565", real_ip)); | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| /// Check if an IP string represents a local/loopback address. | ||
| fn is_local_ip(ip: &str) -> bool { | ||
| ip == "127.0.0.1" | ||
| || ip == "::1" | ||
| || ip == "localhost" | ||
| || ip.starts_with("10.") | ||
| || ip.starts_with("192.168.") | ||
| || ip.starts_with("172.16.") | ||
| || ip.starts_with("172.17.") | ||
| || ip.starts_with("172.18.") | ||
| || ip.starts_with("172.19.") | ||
| || ip.starts_with("172.2") | ||
| || ip.starts_with("172.30.") | ||
| || ip.starts_with("172.31.") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod auth; | ||
| pub mod builtin_modules; | ||
| pub mod config; | ||
| pub mod db; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Private IP detection incorrectly matches public 172.2.x.x addresses
The pattern
ip.starts_with("172.2")incorrectly matches public IP addresses in the 172.2.x.x range as local/private. RFC 1918 defines the private range as 172.16.0.0/12 (172.16.0.0 to 172.31.255.255), so 172.2.x.x addresses are actually public. This pattern was likely intended to match 172.20-172.29, but as written it also matches 172.2.x.x. Servers with public IPs like 172.2.1.1 would have their addresses rejected byextract_server_address, causing the dashboard ping feature to not work for those servers.