1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-01 09:13:54 +00:00

debug_log -> tracing

This commit is contained in:
Anders Åberg
2025-11-11 15:10:41 +01:00
parent 86a5d5607c
commit ca6bf708fb
6 changed files with 37 additions and 114 deletions

View File

@@ -11,7 +11,7 @@ use crate::ipc2::{
PasskeyAssertionRequest, PasskeyAssertionResponse, Position, TimedCallback, UserVerification,
WindowsProviderClient,
};
use crate::util::{debug_log, delay_load, wstr_to_string};
use crate::util::{delay_load, wstr_to_string};
use crate::webauthn::WEBAUTHN_CREDENTIAL_LIST;
use crate::{
com_provider::{
@@ -222,18 +222,10 @@ unsafe fn create_get_assertion_response(
// Encode to CBOR with error handling
let mut cbor_data = Vec::new();
if let Err(e) = ciborium::ser::into_writer(&cbor_value, &mut cbor_data) {
debug_log(&format!(
"ERROR: Failed to encode CBOR assertion response: {:?}",
e
));
tracing::error!("ERROR: Failed to encode CBOR assertion response: {:?}", e);
return Err(HRESULT(-1));
}
debug_log(&format!(
"Formatted CBOR assertion response: {:?}",
cbor_data
));
let response_len = cbor_data.len();
// Allocate memory for the response data
@@ -283,11 +275,6 @@ pub unsafe fn plugin_get_assertion(
let transaction_id = format!("{:?}", req.transaction_id);
let coords = req.window_coordinates().unwrap_or((400, 400));
debug_log(&format!(
"Get assertion request - Transaction: {}",
transaction_id
));
if req.encoded_request_byte_count == 0 || req.encoded_request_pointer.is_null() {
tracing::error!("No encoded request data provided");
return Err(HRESULT(-1));

View File

@@ -6,7 +6,6 @@ use windows_core::{implement, interface, IInspectable, IUnknown, Interface, HRES
use crate::assert::plugin_get_assertion;
use crate::ipc2::WindowsProviderClient;
use crate::make_credential::plugin_make_credential;
use crate::util::debug_log;
use crate::webauthn::WEBAUTHN_CREDENTIAL_LIST;
/// Plugin request type enum as defined in the IDL
@@ -99,11 +98,6 @@ pub unsafe fn parse_credential_list(credential_list: &WEBAUTHN_CREDENTIAL_LIST)
return allowed_credentials;
}
debug_log(&format!(
"Parsing {} credentials from credential list",
credential_list.cCredentials
));
// ppCredentials is an array of pointers to WEBAUTHN_CREDENTIAL_EX
let credentials_array = std::slice::from_raw_parts(
credential_list.ppCredentials,
@@ -119,10 +113,7 @@ pub unsafe fn parse_credential_list(credential_list: &WEBAUTHN_CREDENTIAL_LIST)
let credential = &*credential_ptr;
if credential.cbId == 0 || credential.pbId.is_null() {
debug_log(&format!(
"WARNING: Credential {} has invalid ID, skipping",
i
));
tracing::debug!("WARNING: Credential {} has invalid ID, skipping", i);
continue;
}
// Extract credential ID bytes
@@ -130,17 +121,9 @@ pub unsafe fn parse_credential_list(credential_list: &WEBAUTHN_CREDENTIAL_LIST)
let credential_id_slice =
std::slice::from_raw_parts(credential.pbId, credential.cbId as usize);
debug_log(&format!(
"Parsed credential {}: {} bytes, {:?}",
i, credential.cbId, &credential_id_slice,
));
allowed_credentials.push(credential_id_slice.to_vec());
}
debug_log(&format!(
"Successfully parsed {} allowed credentials",
allowed_credentials.len()
));
allowed_credentials
}

View File

@@ -30,8 +30,6 @@ pub use registration::{
PasskeyRegistrationRequest, PasskeyRegistrationResponse, PreparePasskeyRegistrationCallback,
};
use crate::util::debug_log;
static INIT: Once = Once::new();
#[derive(Debug, Serialize, Deserialize)]
@@ -108,7 +106,6 @@ impl WindowsProviderClient {
// FIXME: Remove unwraps! They panic and terminate the whole application.
#[allow(clippy::unwrap_used)]
pub fn connect() -> Self {
debug_log("YO!");
INIT.call_once(|| {
/*
let filter = EnvFilter::builder()
@@ -116,7 +113,6 @@ impl WindowsProviderClient {
.from_env_lossy();
let log_file_path = "C:\\temp\\bitwarden_windows_passkey_provider.log";
debug_log(&format!("Trying to set up log file at {log_file_path}"));
// FIXME: Remove unwrap
let file = std::fs::File::options()
.append(true)

View File

@@ -13,7 +13,7 @@ use crate::ipc2::{
PasskeyRegistrationRequest, PasskeyRegistrationResponse, Position, TimedCallback,
UserVerification, WindowsProviderClient,
};
use crate::util::{debug_log, delay_load, wstr_to_string, WindowsString};
use crate::util::{delay_load, wstr_to_string, WindowsString};
use crate::webauthn::WEBAUTHN_CREDENTIAL_LIST;
// Windows API types for WebAuthn (from webauthn.h.sample)
@@ -285,10 +285,10 @@ unsafe fn decode_make_credential_request(
// Check if the call succeeded (following C++ THROW_IF_FAILED pattern)
if result.is_err() {
debug_log(&format!(
"ERROR: WebAuthNDecodeMakeCredentialRequest failed with HRESULT: 0x{:08x}",
tracing::error!(
"WebAuthNDecodeMakeCredentialRequest failed with HRESULT: 0x{:08x}",
result.0
));
);
return Err(format!(
"Windows API call failed with HRESULT: 0x{:08x}",
result.0
@@ -306,13 +306,13 @@ unsafe fn decode_make_credential_request(
))
}
/// Helper for registration requests
/// Helper for registration requests
fn send_registration_request(
ipc_client: &WindowsProviderClient,
request: PasskeyRegistrationRequest,
) -> Result<PasskeyRegistrationResponse, String> {
debug_log(&format!("Registration request data - RP ID: {}, User ID: {} bytes, User name: {}, Client data hash: {} bytes, Algorithms: {:?}, Excluded credentials: {}",
request.rp_id, request.user_handle.len(), request.user_name, request.client_data_hash.len(), request.supported_algorithms, request.excluded_credentials.len()));
tracing::debug!("Registration request data - RP ID: {}, User ID: {} bytes, User name: {}, Client data hash: {} bytes, Algorithms: {:?}, Excluded credentials: {}",
request.rp_id, request.user_handle.len(), request.user_name, request.client_data_hash.len(), request.supported_algorithms, request.excluded_credentials.len());
let request_json = serde_json::to_string(&request)
.map_err(|err| format!("Failed to serialize registration request: {err}"))?;
@@ -490,16 +490,9 @@ pub unsafe fn plugin_make_credential(
req.encoded_request_byte_count as usize,
);
debug_log(&format!(
"Encoded request: {} bytes",
encoded_request_slice.len()
));
// Try to decode the request using Windows API
let decoded_wrapper = decode_make_credential_request(encoded_request_slice).map_err(|err| {
debug_log(&format!(
"ERROR: Failed to decode make credential request: {err}"
));
tracing::debug!("ERROR: Failed to decode make credential request: {}", err);
HRESULT(-1)
})?;
let decoded_request = decoded_wrapper.as_ref();
@@ -621,10 +614,10 @@ pub unsafe fn plugin_make_credential(
// Extract excluded credentials from credential list
let excluded_credentials = parse_credential_list(&decoded_request.CredentialList);
if !excluded_credentials.is_empty() {
debug_log(&format!(
tracing::debug!(
"Found {} excluded credentials for make credential",
excluded_credentials.len()
));
);
}
// Create Windows registration request
@@ -643,10 +636,10 @@ pub unsafe fn plugin_make_credential(
},
};
debug_log(&format!(
tracing::debug!(
"Make credential request - RP: {}, User: {}",
rpid, registration_request.user_name
));
);
// Send registration request
let passkey_response =
@@ -654,10 +647,10 @@ pub unsafe fn plugin_make_credential(
tracing::error!("Registration request failed: {err}");
HRESULT(-1)
})?;
debug_log(&format!(
tracing::debug!(
"Registration response received: {:?}",
passkey_response
));
);
// Create proper WebAuthn response from passkey_response
tracing::debug!("Creating WebAuthn make credential response");
@@ -666,9 +659,9 @@ pub unsafe fn plugin_make_credential(
tracing::error!("Failed to create WebAuthn response: {err}");
HRESULT(-1)
})?;
debug_log(&format!(
tracing::debug!(
"Successfully created WebAuthn response: {webauthn_response:?}"
));
);
(*response).encoded_response_byte_count = webauthn_response.len() as u32;
(*response).encoded_response_pointer = webauthn_response.as_mut_ptr();
tracing::debug!("Set pointer, returning HRESULT(0)");

View File

@@ -1,8 +1,3 @@
use std::fs::{create_dir_all, OpenOptions};
use std::io::Write;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use windows::Win32::Foundation::*;
use windows::Win32::System::LibraryLoader::*;
use windows_core::*;
@@ -51,37 +46,6 @@ impl WindowsString for str {
}
}
pub fn file_log(msg: &str) {
let log_path = "C:\\temp\\bitwarden_com_debug.log";
// Create the temp directory if it doesn't exist
if let Some(parent) = Path::new(log_path).parent() {
let _ = create_dir_all(parent);
}
if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(log_path) {
let now = SystemTime::now();
let timestamp = match now.duration_since(UNIX_EPOCH) {
Ok(duration) => {
let total_secs = duration.as_secs();
let millis = duration.subsec_millis();
let secs = total_secs % 60;
let mins = (total_secs / 60) % 60;
let hours = (total_secs / 3600) % 24;
format!("{:02}:{:02}:{:02}.{:03}", hours, mins, secs, millis)
}
Err(_) => "??:??:??.???".to_string(),
};
let _ = writeln!(file, "[{}] {}", timestamp, msg);
}
}
pub fn debug_log(message: &str) {
tracing::debug!(message);
file_log(message)
}
// Helper function to convert Windows wide string (UTF-16) to Rust String
pub unsafe fn wstr_to_string(
wstr_ptr: *const u16,

View File

@@ -8,7 +8,7 @@
use windows_core::*;
use crate::com_buffer::ComBuffer;
use crate::util::{debug_log, delay_load, WindowsString};
use crate::util::{delay_load, WindowsString};
/// Windows WebAuthn Authenticator Options structure
/// Header File Name: _WEBAUTHN_CTAPCBOR_AUTHENTICATOR_OPTIONS
@@ -135,7 +135,7 @@ pub fn add_credentials(
clsid_guid: GUID,
credentials: Vec<WebAuthnPluginCredentialDetails>,
) -> std::result::Result<(), String> {
debug_log("Loading WebAuthNPluginAuthenticatorAddCredentials function...");
tracing::debug!("Loading WebAuthNPluginAuthenticatorAddCredentials function...");
let result = unsafe {
delay_load::<WebAuthNPluginAuthenticatorAddCredentialsFnDeclaration>(
@@ -146,8 +146,8 @@ pub fn add_credentials(
match result {
Some(api) => {
debug_log("Function loaded successfully, calling API...");
debug_log(&format!("Adding {} credentials", credentials.len()));
tracing::debug!("Function loaded successfully, calling API...");
tracing::debug!("Adding {} credentials", credentials.len());
let credential_count = credentials.len() as u32;
let credentials_ptr = if credentials.is_empty() {
@@ -160,18 +160,18 @@ pub fn add_credentials(
if result.is_err() {
let error_code = result.0;
debug_log(&format!("API call failed with HRESULT: 0x{:x}", error_code));
tracing::error!("API call failed with HRESULT: 0x{:x}", error_code);
return Err(format!(
"Error: Error response from WebAuthNPluginAuthenticatorAddCredentials()\nHRESULT: 0x{:x}\n{}",
error_code, result.message()
));
}
debug_log("API call succeeded");
tracing::debug!("API call succeeded");
Ok(())
}
None => {
debug_log("Failed to load WebAuthNPluginAuthenticatorAddCredentials function from webauthn.dll");
tracing::error!("Failed to load WebAuthNPluginAuthenticatorAddCredentials function from webauthn.dll");
Err(String::from("Error: Can't complete add_credentials(), as the function WebAuthNPluginAuthenticatorAddCredentials can't be loaded."))
}
}
@@ -181,7 +181,7 @@ pub fn remove_credentials(
clsid_guid: GUID,
credentials: Vec<WebAuthnPluginCredentialDetails>,
) -> std::result::Result<(), String> {
debug_log("Loading WebAuthNPluginAuthenticatorRemoveCredentials function...");
tracing::debug!("Loading WebAuthNPluginAuthenticatorRemoveCredentials function...");
let result = unsafe {
delay_load::<WebAuthNPluginAuthenticatorRemoveCredentialsFnDeclaration>(
@@ -192,7 +192,7 @@ pub fn remove_credentials(
match result {
Some(api) => {
debug_log(&format!("Removing {} credentials", credentials.len()));
tracing::debug!("Removing {} credentials", credentials.len());
let credential_count = credentials.len() as u32;
let credentials_ptr = if credentials.is_empty() {
@@ -232,7 +232,7 @@ pub struct OwnedCredentialDetails {
pub fn get_all_credentials(
clsid_guid: GUID,
) -> std::result::Result<Vec<OwnedCredentialDetails>, String> {
debug_log("Loading WebAuthNPluginAuthenticatorGetAllCredentials function...");
tracing::debug!("Loading WebAuthNPluginAuthenticatorGetAllCredentials function...");
let result = unsafe {
delay_load::<WebAuthNPluginAuthenticatorGetAllCredentialsFnDeclaration>(
@@ -256,7 +256,7 @@ pub fn get_all_credentials(
}
if credentials_array_ptr.is_null() || credential_count == 0 {
debug_log("No credentials returned");
tracing::debug!("No credentials returned");
return Ok(Vec::new());
}
@@ -333,7 +333,7 @@ pub fn get_all_credentials(
// Free the array using the Windows API - this frees everything including strings
free_credential_details_array(credential_count, credentials_array_ptr);
debug_log(&format!("Retrieved {} credentials", owned_credentials.len()));
tracing::debug!("Retrieved {} credentials", owned_credentials.len());
Ok(owned_credentials)
},
None => {
@@ -360,12 +360,12 @@ fn free_credential_details_array(
if let Some(api) = result {
unsafe { api(credential_count, credentials_array) };
} else {
debug_log("Warning: Could not load WebAuthNPluginAuthenticatorFreeCredentialDetailsArray");
tracing::warn!("Could not load WebAuthNPluginAuthenticatorFreeCredentialDetailsArray");
}
}
pub fn remove_all_credentials(clsid_guid: GUID) -> std::result::Result<(), String> {
debug_log("Loading WebAuthNPluginAuthenticatorRemoveAllCredentials function...");
tracing::debug!("Loading WebAuthNPluginAuthenticatorRemoveAllCredentials function...");
let result = unsafe {
delay_load::<WebAuthNPluginAuthenticatorRemoveAllCredentialsFnDeclaration>(
@@ -376,13 +376,13 @@ pub fn remove_all_credentials(clsid_guid: GUID) -> std::result::Result<(), Strin
match result {
Some(api) => {
debug_log("Function loaded successfully, calling API...");
tracing::debug!("Function loaded successfully, calling API...");
let result = unsafe { api(&clsid_guid) };
if result.is_err() {
let error_code = result.0;
debug_log(&format!("API call failed with HRESULT: 0x{:x}", error_code));
tracing::error!("API call failed with HRESULT: 0x{:x}", error_code);
return Err(format!(
"Error: Error response from WebAuthNPluginAuthenticatorRemoveAllCredentials()\nHRESULT: 0x{:x}\n{}",
@@ -390,11 +390,11 @@ pub fn remove_all_credentials(clsid_guid: GUID) -> std::result::Result<(), Strin
));
}
debug_log("API call succeeded");
tracing::debug!("API call succeeded");
Ok(())
}
None => {
debug_log("Failed to load WebAuthNPluginAuthenticatorRemoveAllCredentials function from webauthn.dll");
tracing::error!("Failed to load WebAuthNPluginAuthenticatorRemoveAllCredentials function from webauthn.dll");
Err(String::from("Error: Can't complete remove_all_credentials(), as the function WebAuthNPluginAuthenticatorRemoveAllCredentials can't be loaded."))
}
}