Skip to content

Commit fc517bf

Browse files
Add native OAuth callbacks and bundled Builderlab login
Native code owns external-browser startup, loopback authorization-code callbacks, and default state verification. Keep provider code exchange and credentials separate from the temporary Builderlab Settings integration. Derive the bundled exact-origin grant from BUZZ_BUILDERLAB_URL at build time using local or process configuration. Share native dotenv record selection to tolerate unrelated development settings, including the helper in isolated build fixtures. Include lifecycle cleanup, protocol documentation, and focused regression coverage. Signed-off-by: Jarrod Sibbison <[email protected]>
1 parent 0a8521e commit fc517bf

36 files changed

Lines changed: 2456 additions & 124 deletions

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/agent-controller/build.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn main() {
99
let local = root.join("../../.env.local");
1010
println!("cargo:rerun-if-changed={}", local.display());
1111
println!("cargo:rerun-if-changed=build_config.rs");
12+
println!("cargo:rerun-if-changed=../build_env.rs");
1213
for key in build_config::KEYS {
1314
println!("cargo:rerun-if-env-changed={key}");
1415
}

‎crates/agent-controller/build_config.rs‎

Lines changed: 5 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,110 +6,15 @@ pub const KEYS: [&str; 3] = [
66
"BUZZ_BUILD_AGENT_ACCESS_OWNER_ONLY",
77
];
88

9-
// Parse without mutating the process environment. Never propagate dotenv errors:
10-
// they can contain complete lines, including unrelated local credentials.
9+
// Both native builds select complete dotenv records before decoding them.
10+
#[path = "../build_env.rs"]
11+
mod build_env;
12+
1113
pub fn load(
1214
path: &Path,
1315
process: impl Fn(&str) -> Option<String>,
1416
) -> Result<BTreeMap<String, String>, &'static str> {
15-
let mut values: BTreeMap<_, _> = KEYS
16-
.into_iter()
17-
.filter_map(|key| process(key).map(|value| (key.to_owned(), value)))
18-
.collect();
19-
if values.len() == KEYS.len() {
20-
return Ok(values);
21-
}
22-
let text = match std::fs::read_to_string(path) {
23-
Ok(text) => text,
24-
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(values),
25-
Err(_) => return Err("Could not read native build .env.local"),
26-
};
27-
let mut local = BTreeMap::new();
28-
let mut rest = text.trim_start_matches('\u{feff}');
29-
while !rest.is_empty() {
30-
let line_end = rest.find('\n').map_or(rest.len(), |end| end + 1);
31-
let line = rest[..line_end].trim_start();
32-
if line.is_empty() || line.starts_with('#') {
33-
rest = &rest[line_end..];
34-
continue;
35-
}
36-
let assignment = line
37-
.strip_prefix("export")
38-
.filter(|tail| tail.starts_with(char::is_whitespace))
39-
.unwrap_or(line)
40-
.trim_start();
41-
let key = assignment
42-
.split(|c: char| c == '=' || c.is_whitespace())
43-
.next()
44-
.unwrap_or("");
45-
let selected = KEYS.contains(&key) && !values.contains_key(key);
46-
// Only a value starting with a backtick uses alternate-loader framing.
47-
// Mid-value backticks are literal characters in dotenvy.
48-
let backtick_start = assignment
49-
.strip_prefix(key)
50-
.and_then(|tail| tail.trim_start().strip_prefix('='))
51-
.map(str::trim_start)
52-
.filter(|value| value.starts_with('`'))
53-
.map(|value| line_end - value.len());
54-
// Follow dotenvy's whole-record quote/escape/comment boundaries, not
55-
// just the first quoted segment. Also conservatively contain backtick
56-
// records used by other dev loaders (dotenvy does not decode them).
57-
let mut end = rest.len();
58-
let mut quote = None;
59-
let mut escaped = false;
60-
let mut whitespace = false;
61-
let mut backtick = false;
62-
let mut comment = false;
63-
for (i, c) in rest.char_indices() {
64-
// Backtick containment must never mask dotenvy's quote state.
65-
// A comment can block opening a frame, never closing an existing one.
66-
if c == '`' && !escaped && (backtick || (!comment && backtick_start == Some(i))) {
67-
backtick = !backtick;
68-
}
69-
if comment {
70-
comment = c != '\n';
71-
} else if escaped {
72-
escaped = false;
73-
whitespace = false;
74-
} else if c == '\\' {
75-
escaped = true;
76-
whitespace = false;
77-
} else if let Some(open) = quote {
78-
if c == open {
79-
quote = None;
80-
}
81-
} else if whitespace && c == '#' {
82-
comment = true;
83-
whitespace = false;
84-
} else if matches!(c, '\'' | '"') {
85-
quote = Some(c);
86-
whitespace = false;
87-
} else {
88-
// Match dotenvy's one-character WhiteSpace state.
89-
whitespace = !whitespace && c.is_whitespace() && !matches!(c, '\n' | '\r');
90-
}
91-
if c == '\n' && quote.is_none() && !escaped && !backtick {
92-
end = i + 1;
93-
break;
94-
}
95-
}
96-
if backtick {
97-
return Err("Invalid native build .env.local");
98-
}
99-
if selected {
100-
for entry in dotenvy::from_read_iter(&rest.as_bytes()[..end]) {
101-
let (key, value) = entry.map_err(|_| "Invalid native build .env.local")?;
102-
if KEYS.contains(&key.as_str()) {
103-
local.insert(key, value);
104-
}
105-
}
106-
}
107-
rest = &rest[end..];
108-
}
109-
for (key, value) in local {
110-
values.entry(key).or_insert(value);
111-
}
112-
Ok(values)
17+
build_env::load(path, &KEYS, process)
11318
}
11419

11520
pub fn selector(value: &str, limit: usize) -> Result<(), &'static str> {

‎crates/build_env.rs‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
use std::{collections::BTreeMap, path::Path};
2+
3+
// Parse without mutating the process environment. Never propagate dotenv errors:
4+
// they can contain complete lines, including unrelated local credentials.
5+
pub fn load(
6+
path: &Path,
7+
keys: &[&str],
8+
process: impl Fn(&str) -> Option<String>,
9+
) -> Result<BTreeMap<String, String>, &'static str> {
10+
let mut values: BTreeMap<_, _> = keys
11+
.iter()
12+
.copied()
13+
.filter_map(|key| process(key).map(|value| (key.to_owned(), value)))
14+
.collect();
15+
if values.len() == keys.len() {
16+
return Ok(values);
17+
}
18+
let text = match std::fs::read_to_string(path) {
19+
Ok(text) => text,
20+
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(values),
21+
Err(_) => return Err("Could not read native build .env.local"),
22+
};
23+
let mut local = BTreeMap::new();
24+
let mut rest = text.trim_start_matches('\u{feff}');
25+
while !rest.is_empty() {
26+
let line_end = rest.find('\n').map_or(rest.len(), |end| end + 1);
27+
let line = rest[..line_end].trim_start();
28+
if line.is_empty() || line.starts_with('#') {
29+
rest = &rest[line_end..];
30+
continue;
31+
}
32+
let assignment = line
33+
.strip_prefix("export")
34+
.filter(|tail| tail.starts_with(char::is_whitespace))
35+
.unwrap_or(line)
36+
.trim_start();
37+
let key = assignment
38+
.split(|c: char| c == '=' || c.is_whitespace())
39+
.next()
40+
.unwrap_or("");
41+
let selected = keys.contains(&key) && !values.contains_key(key);
42+
// Only a value starting with a backtick uses alternate-loader framing.
43+
// Mid-value backticks are literal characters in dotenvy.
44+
let backtick_start = assignment
45+
.strip_prefix(key)
46+
.and_then(|tail| tail.trim_start().strip_prefix('='))
47+
.map(str::trim_start)
48+
.filter(|value| value.starts_with('`'))
49+
.map(|value| line_end - value.len());
50+
// Follow dotenvy's whole-record quote/escape/comment boundaries, not
51+
// just the first quoted segment. Also conservatively contain backtick
52+
// records used by other dev loaders (dotenvy does not decode them).
53+
let mut end = rest.len();
54+
let mut quote = None;
55+
let mut escaped = false;
56+
let mut whitespace = false;
57+
let mut backtick = false;
58+
let mut comment = false;
59+
for (i, c) in rest.char_indices() {
60+
// Backtick containment must never mask dotenvy's quote state.
61+
// A comment can block opening a frame, never closing an existing one.
62+
if c == '`' && !escaped && (backtick || (!comment && backtick_start == Some(i))) {
63+
backtick = !backtick;
64+
}
65+
if comment {
66+
comment = c != '\n';
67+
} else if escaped {
68+
escaped = false;
69+
whitespace = false;
70+
} else if c == '\\' {
71+
escaped = true;
72+
whitespace = false;
73+
} else if let Some(open) = quote {
74+
if c == open {
75+
quote = None;
76+
}
77+
} else if whitespace && c == '#' {
78+
comment = true;
79+
whitespace = false;
80+
} else if matches!(c, '\'' | '"') {
81+
quote = Some(c);
82+
whitespace = false;
83+
} else {
84+
// Match dotenvy's one-character WhiteSpace state.
85+
whitespace = !whitespace && c.is_whitespace() && !matches!(c, '\n' | '\r');
86+
}
87+
if c == '\n' && quote.is_none() && !escaped && !backtick {
88+
end = i + 1;
89+
break;
90+
}
91+
}
92+
if backtick {
93+
return Err("Invalid native build .env.local");
94+
}
95+
if selected {
96+
for entry in dotenvy::from_read_iter(&rest.as_bytes()[..end]) {
97+
let (key, value) = entry.map_err(|_| "Invalid native build .env.local")?;
98+
if keys.contains(&key.as_str()) {
99+
local.insert(key, value);
100+
}
101+
}
102+
}
103+
rest = &rest[end..];
104+
}
105+
for (key, value) in local {
106+
values.entry(key).or_insert(value);
107+
}
108+
Ok(values)
109+
}

‎crates/plugin-manager/Cargo.toml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ rust-version = "1.89"
88
name = "buzzodz"
99
path = "src/main.rs"
1010

11+
[build-dependencies]
12+
dotenvy = "0.15"
13+
url = "2"
14+
15+
[dev-dependencies]
16+
dotenvy = "0.15"
17+
1118
[dependencies]
1219
serde = { version = "1", features = ["derive"] }
1320
serde_json = "1"

‎crates/plugin-manager/build.rs‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use std::path::{Path, PathBuf};
2+
3+
#[path = "../build_env.rs"]
4+
mod build_env;
5+
6+
const BUILDERLAB_URL_KEY: &str = "BUZZ_BUILDERLAB_URL";
7+
8+
fn builderlab_origin(local: &Path, process: Option<String>) -> Result<String, &'static str> {
9+
let values = build_env::load(local, &[BUILDERLAB_URL_KEY], |_| process.clone())?;
10+
let raw = values
11+
.get(BUILDERLAB_URL_KEY)
12+
.map(String::as_str)
13+
.unwrap_or("");
14+
if raw.is_empty() {
15+
return Ok(String::new());
16+
}
17+
let url = url::Url::parse(raw).map_err(|_| "Invalid Builderlab build URL")?;
18+
if url.scheme() != "https"
19+
|| !url.username().is_empty()
20+
|| url.password().is_some()
21+
|| url.query().is_some()
22+
|| url.fragment().is_some()
23+
{
24+
return Err("Builderlab build URL must use HTTPS without credentials, query, or fragment");
25+
}
26+
Ok(url.origin().ascii_serialization())
27+
}
28+
29+
#[cfg_attr(test, allow(dead_code))]
30+
fn main() {
31+
let root = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("../..");
32+
let local = root.join(".env.local");
33+
println!("cargo:rerun-if-changed={}", local.display());
34+
println!("cargo:rerun-if-env-changed={BUILDERLAB_URL_KEY}");
35+
println!(
36+
"cargo:rerun-if-changed={}",
37+
root.join("crates/build_env.rs").display()
38+
);
39+
let process = match std::env::var(BUILDERLAB_URL_KEY) {
40+
Ok(value) => Some(value),
41+
Err(std::env::VarError::NotPresent) => None,
42+
Err(std::env::VarError::NotUnicode(_)) => panic!("Native build inputs must be UTF-8"),
43+
};
44+
let origin =
45+
builderlab_origin(&local, process).expect("Invalid Builderlab build configuration");
46+
println!("cargo:rustc-env=BUZZ_BUILDERLAB_ORIGIN={origin}");
47+
}
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
#[test]
54+
fn local_url_and_process_overrides_produce_an_exact_origin() {
55+
let directory = tempfile::tempdir().unwrap();
56+
let local = directory.path().join(".env.local");
57+
assert_eq!(builderlab_origin(&local, None).unwrap(), "");
58+
std::fs::write(
59+
&local,
60+
"\u{feff}export BUZZ_BUILDERLAB_URL=\"https://APP.BUILDERLAB.XYZ:443/deployment/\" # comment\n",
61+
)
62+
.unwrap();
63+
assert_eq!(
64+
builderlab_origin(&local, None).unwrap(),
65+
"https://app.builderlab.xyz"
66+
);
67+
assert_eq!(
68+
builderlab_origin(&local, Some("https://login.example:8443/path/".into())).unwrap(),
69+
"https://login.example:8443"
70+
);
71+
assert_eq!(builderlab_origin(&local, Some(String::new())).unwrap(), "");
72+
}
73+
74+
#[test]
75+
fn invalid_configuration_fails_without_echoing_its_contents() {
76+
let directory = tempfile::tempdir().unwrap();
77+
let local = directory.path().join(".env.local");
78+
for configured in [
79+
"http://localhost:4318",
80+
"https://user:[email protected]",
81+
"https://login.example?",
82+
"https://login.example#",
83+
"invalid",
84+
] {
85+
let error = builderlab_origin(&local, Some(configured.into())).unwrap_err();
86+
assert!(!error.contains(configured));
87+
}
88+
std::fs::write(&local, "BUZZ_BUILDERLAB_URL='unterminated").unwrap();
89+
assert_eq!(
90+
builderlab_origin(&local, None).unwrap_err(),
91+
"Invalid native build .env.local"
92+
);
93+
// Explicit process input also overrides malformed same-key assignments.
94+
assert_eq!(builderlab_origin(&local, Some(String::new())).unwrap(), "");
95+
}
96+
97+
#[test]
98+
fn unrelated_syntax_is_ignored_without_promoting_multiline_contents() {
99+
let directory = tempfile::tempdir().unwrap();
100+
let local = directory.path().join(".env.local");
101+
std::fs::write(&local, "PRIVATE_VALUE='unterminated").unwrap();
102+
assert_eq!(builderlab_origin(&local, None).unwrap(), "");
103+
104+
for quote in ["'", "\"", "`"] {
105+
std::fs::write(
106+
&local,
107+
format!(
108+
"PRIVATE_VALUE={quote}multiline\nBUZZ_BUILDERLAB_URL=https://hidden.example\n{quote}\nINVALID=unquoted value\nexport BUZZ_BUILDERLAB_URL=https://app.builderlab.xyz/\nTRAILING='unfinished\n"
109+
),
110+
)
111+
.unwrap();
112+
assert_eq!(
113+
builderlab_origin(&local, None).unwrap(),
114+
"https://app.builderlab.xyz"
115+
);
116+
}
117+
for quote in ["'", "\""] {
118+
std::fs::write(
119+
&local,
120+
format!(
121+
"PRIVATE_VALUE={quote}unfinished\nBUZZ_BUILDERLAB_URL=https://hidden.example\n"
122+
),
123+
)
124+
.unwrap();
125+
assert_eq!(builderlab_origin(&local, None).unwrap(), "");
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)