{"record":{"id":"e88d24a1feb5495a","repo":"Hmbown/CodeWhale","slug":"xai-oauth-path-must-be-a-regular-file","errorCode":null,"errorMessage":"xAI OAuth path must be a regular file","messagePattern":"xAI OAuth path must be a regular file","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/config/src/xai_credentials.rs","lineNumber":902,"sourceCode":"            )\n        } != 0\n        {\n            return Err(std::io::Error::last_os_error()).context(\"retiring xAI OAuth file\");\n        }\n        Ok(())\n    }\n}\n\n#[cfg(unix)]\nfn validate_owned_file_handle(file: &File, path: &Path) -> Result<fs::Metadata> {\n    use std::os::unix::fs::MetadataExt as _;\n    let metadata = file.metadata().with_context(|| {\n        format!(\n            \"inspecting Codewhale-owned xAI OAuth file {}\",\n            crate::quote_os_path(path)\n        )\n    })?;\n    anyhow::ensure!(metadata.is_file(), \"xAI OAuth path must be a regular file\");\n    // SAFETY: geteuid(2) dereferences no pointers.\n    anyhow::ensure!(\n        metadata.uid() == unsafe { libc::geteuid() },\n        \"xAI OAuth file must be owned by the current user\"\n    );\n    anyhow::ensure!(\n        metadata.nlink() == 1,\n        \"xAI OAuth file must not have multiple filesystem links\"\n    );\n    Ok(metadata)\n}\n\n#[cfg(windows)]\nfn open_owned_credentials_directory(directory: &Path) -> Result<XaiOAuthCredentialStore> {\n    use std::os::windows::fs::OpenOptionsExt as _;\n    use windows_sys::Win32::Storage::FileSystem::{\n        FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT, FILE_GENERIC_READ,\n        FILE_SHARE_READ, FILE_SHARE_WRITE, WRITE_DAC, WRITE_OWNER,","sourceCodeStart":884,"sourceCodeEnd":920,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/config/src/xai_credentials.rs#L884-L920","documentation":"validate_owned_file_handle stats the opened file and requires it to be a regular file. This blocks directories, FIFOs, devices, and symlinks-to-weird-things from being used as credential storage, closing TOCTOU holes where a path could swap between open and use.","triggerScenarios":"Any of read_to_string, write_owned_file, remove_raw, rename_raw, or the internal/windows openers encountering a path that resolved to something other than a regular file at open time — e.g. an attacker replaced the credential file with a FIFO or symlink.","commonSituations":"A symlink placed where the token file should be; a named pipe created by a mischievous script in the credentials dir; /dev-style device paths configured as the credential path.","solutions":["Delete the non-regular path and re-authenticate so the store recreates a plain file.","Ensure nothing in the environment creates symlinks or FIFOs in the credentials directory (sync tools, dotfile managers).","Do not point the store at special filesystem paths (devices, procfs).","Pre-check with symlink_metadata in your setup if the directory is not fully trusted."],"exampleFix":"// before\nlet store = store.read_to_string(name)?; // fails: path is a FIFO\n// after\nlet md = std::fs::symlink_metadata(dir.join(name))?;\nanyhow::ensure!(md.is_file(), \"credential path must be a regular file\");","handlingStrategy":"validation","validationCode":"let md = std::fs::symlink_metadata(path)?;\nanyhow::ensure!(md.is_file(), \"credential path must be a regular file, not symlink/FIFO\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep dotfile managers and sync tools out of the credentials directory","Delete and re-authenticate rather than repairing suspicious paths","Refuse to point the store at special filesystems"],"tags":["security","symlink","filesystem","toctou"],"backgroundTag":"unexpected-response-shape","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}