-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Set a user agent suffix when used as a mcp server #3395
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
47bc5dd
Add user agent
gpeal 0a341ba
env var -> mutex
gpeal b28d8c5
Add sanitization
gpeal 22d0a0a
Fix test
gpeal 3f0c631
Use os_info
gpeal 8d9ca7f
test
gpeal eb64b89
Fix test
gpeal b5e87dc
Add comment and reduce lock
gpeal 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 +1,17 @@ | ||
| use reqwest::header::HeaderValue; | ||
| use std::sync::LazyLock; | ||
| use std::sync::Mutex; | ||
|
|
||
| /// Set this to add a suffix to the User-Agent string. | ||
| /// | ||
| /// This is primarily designed to differentiate MCP clients (or the lack thereof) from each other. | ||
| /// Because there can only be one MCP server per process, it should be safe for this to be a global static. | ||
| /// However, future users of this should use this with caution as a result. | ||
| /// | ||
| /// A space is automatically added between the suffix and the rest of the User-Agent string. | ||
| /// The full user agent string is returned from the mcp initialize response. | ||
| /// Parenthesis will be added by Codex. This should only specify what goes inside of the parenthesis. | ||
| pub static USER_AGENT_SUFFIX: LazyLock<Mutex<Option<String>>> = LazyLock::new(|| Mutex::new(None)); | ||
|
|
||
| pub const CODEX_INTERNAL_ORIGINATOR_OVERRIDE_ENV_VAR: &str = "CODEX_INTERNAL_ORIGINATOR_OVERRIDE"; | ||
|
|
||
|
|
@@ -32,14 +44,62 @@ pub static ORIGINATOR: LazyLock<Originator> = LazyLock::new(|| { | |
| pub fn get_codex_user_agent() -> String { | ||
| let build_version = env!("CARGO_PKG_VERSION"); | ||
| let os_info = os_info::get(); | ||
| format!( | ||
| let prefix = format!( | ||
| "{}/{build_version} ({} {}; {}) {}", | ||
| ORIGINATOR.value.as_str(), | ||
| os_info.os_type(), | ||
| os_info.version(), | ||
| os_info.architecture().unwrap_or("unknown"), | ||
| crate::terminal::user_agent() | ||
| ) | ||
| ); | ||
| let suffix = USER_AGENT_SUFFIX | ||
| .lock() | ||
| .ok() | ||
| .and_then(|guard| guard.clone()) | ||
| .and_then(|value| { | ||
| let value = value.trim(); | ||
| if value.is_empty() { | ||
| None | ||
| } else { | ||
| Some(value.to_string()) | ||
| } | ||
| }) | ||
| .map_or_else(String::new, |value| format!(" ({value})")); | ||
|
|
||
| let candidate = format!("{prefix}{suffix}"); | ||
| sanitize_user_agent(candidate, &prefix) | ||
| } | ||
|
|
||
| /// Sanitize the user agent string. | ||
| /// | ||
| /// Invalid characters are replaced with an underscore. | ||
| /// | ||
| /// If the user agent fails to parse, it falls back to fallback and then to ORIGINATOR. | ||
| fn sanitize_user_agent(candidate: String, fallback: &str) -> String { | ||
| if HeaderValue::from_str(candidate.as_str()).is_ok() { | ||
| return candidate; | ||
| } | ||
|
|
||
| let sanitized: String = candidate | ||
| .chars() | ||
| .map(|ch| if matches!(ch, ' '..='~') { ch } else { '_' }) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure we want to allow symbols and spaces? Can't hurt, I guess? |
||
| .collect(); | ||
| if !sanitized.is_empty() && HeaderValue::from_str(sanitized.as_str()).is_ok() { | ||
| tracing::warn!( | ||
| "Sanitized Codex user agent because provided suffix contained invalid header characters" | ||
| ); | ||
| sanitized | ||
| } else if HeaderValue::from_str(fallback).is_ok() { | ||
| tracing::warn!( | ||
| "Falling back to base Codex user agent because provided suffix could not be sanitized" | ||
| ); | ||
| fallback.to_string() | ||
| } else { | ||
| tracing::warn!( | ||
| "Falling back to default Codex originator because base user agent string is invalid" | ||
| ); | ||
| ORIGINATOR.value.clone() | ||
| } | ||
| } | ||
|
|
||
| /// Create a reqwest client with default `originator` and `User-Agent` headers set. | ||
|
|
@@ -114,6 +174,28 @@ mod tests { | |
| assert_eq!(ua_header.to_str().unwrap(), expected_ua); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_invalid_suffix_is_sanitized() { | ||
| let prefix = "codex_cli_rs/0.0.0"; | ||
| let suffix = "bad\rsuffix"; | ||
|
|
||
| assert_eq!( | ||
| sanitize_user_agent(format!("{prefix} ({suffix})"), prefix), | ||
| "codex_cli_rs/0.0.0 (bad_suffix)" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_invalid_suffix_is_sanitized2() { | ||
| let prefix = "codex_cli_rs/0.0.0"; | ||
| let suffix = "bad\0suffix"; | ||
|
|
||
| assert_eq!( | ||
| sanitize_user_agent(format!("{prefix} ({suffix})"), prefix), | ||
| "codex_cli_rs/0.0.0 (bad_suffix)" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| #[cfg(target_os = "macos")] | ||
| fn test_macos() { | ||
|
|
||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.