Skip to content

Commit 784dc61

Browse files
committed
Fix: Skip empty lines when listening to OAuth callback
It is possible that a browser, after login, sends out empty lines through the socket before sending the auth code. This fix will skip the empty lines and will parse the first non-empty line.
1 parent 33bf3a7 commit 784dc61

1 file changed

Lines changed: 33 additions & 27 deletions

File tree

oauth/src/lib.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -179,33 +179,39 @@ fn get_authcode_listener(
179179
info!("OAuth server listening on {socket_address:?}");
180180

181181
// The server will terminate itself after collecting the first code.
182-
let mut stream = listener
183-
.incoming()
184-
.flatten()
185-
.next()
186-
.ok_or(OAuthError::AuthCodeListenerTerminated)?;
187-
let mut reader = BufReader::new(&stream);
188-
let mut request_line = String::new();
189-
reader
190-
.read_line(&mut request_line)
191-
.map_err(|_| OAuthError::AuthCodeListenerRead)?;
192-
193-
let redirect_url = request_line
194-
.split_whitespace()
195-
.nth(1)
196-
.ok_or(OAuthError::AuthCodeListenerParse)?;
197-
let code = get_code(&("http://localhost".to_string() + redirect_url));
198-
199-
let response = format!(
200-
"HTTP/1.1 200 OK\r\ncontent-length: {}\r\n\r\n{}",
201-
message.len(),
202-
message
203-
);
204-
stream
205-
.write_all(response.as_bytes())
206-
.map_err(|_| OAuthError::AuthCodeListenerWrite)?;
207-
208-
code
182+
for incoming in listener.incoming() {
183+
let mut stream = match incoming {
184+
Ok(stream) => stream,
185+
Err(_) => continue,
186+
};
187+
188+
let mut reader = BufReader::new(&stream);
189+
let mut request_line = String::new();
190+
reader
191+
.read_line(&mut request_line)
192+
.map_err(|_| OAuthError::AuthCodeListenerRead)?;
193+
194+
if request_line.trim().is_empty() {
195+
continue; // Skip empty lines
196+
}
197+
198+
let redirect_url = request_line
199+
.split_whitespace()
200+
.nth(1)
201+
.ok_or(OAuthError::AuthCodeListenerParse)?;
202+
let code = get_code(&("http://localhost".to_string() + redirect_url));
203+
204+
let response = format!(
205+
"HTTP/1.1 200 OK\r\ncontent-length: {}\r\n\r\n{}",
206+
message.len(),
207+
message
208+
);
209+
stream
210+
.write_all(response.as_bytes())
211+
.map_err(|_| OAuthError::AuthCodeListenerWrite)?;
212+
return code;
213+
}
214+
Err(OAuthError::AuthCodeListenerTerminated)
209215
}
210216

211217
// If the specified `redirect_uri` is HTTP and contains a port,

0 commit comments

Comments
 (0)