fix(utils): hard-error in get_config_dir when HOME is unresolvable - #15
Open
TheBigWealth89 wants to merge 1 commit into
Open
Conversation
Previously, get_config_dir() silently fell back to the current working
directory when dirs_next::home_dir() returned None, causing the CLI to
write auth tokens and config.json into ./.txio/ instead of a stable
home directory. This had two real-world risks:
- Silent credential loss in ephemeral environments (CI, containers)
where the working directory is wiped after the run.
- Accidental credential exposure via git if the CLI was run from
inside a tracked project directory.
Changes:
- get_config_dir() now returns Result<PathBuf> and fails with a
clear, actionable error message if home_dir() returns None or an
empty string: "Could not determine home directory (checked $HOME).
Set the HOME environment variable and try again."
- All 11 callers propagate the Result with ? (save_token, get_token,
remove_token, save_config, get_config, list_config, remove_config,
save_current_chain, get_current_chain, load_environment).
- get_token and get_current_chain signatures change from Option<String>
to Result<Option<String>>; all call sites in handlers.rs updated.
- Error propagates to main(), which already prints errors to stderr
and exits with code 1.
- Added test: get_config_dir_fails_when_home_unset.
BREAKING CHANGE: any workflow relying on the old silent working-directory
fallback (./.txio/) will now receive an explicit error. This is intentional.
Contributor
|
@TheBigWealth89 kindly resolve conflicts |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fix(utils): Hard-error in
get_config_dir()whenHOMEis unresolvableCloses #12
What was broken
get_config_dir()insrc/utils/mod.rssilently fell back toPathBuf::from(".")wheneverdirs_next::home_dir()returnedNone. This caused the CLI to write auth tokens andconfig.jsoninto./.txio/in the current working directory instead of the user's home directory.This introduced two concrete security/reliability risks:
.txio/folder (and itstokenfile) could be inadvertently committed and pushed to a remote.What changed
src/utils/mod.rsget_config_dir()now returnsResult<PathBuf>instead ofPathBuf.dirs_next::home_dir()returningNoneor an empty string, it immediately returnsErrwith a clear, actionable message:filter(|p| !p.as_os_str().is_empty())) ensuresHOME=""is treated identically toHOMEbeing unset.?:save_token,get_token,remove_token,save_config,get_config,list_config,remove_config,save_current_chain,get_current_chain,remove_config,load_environment.get_tokenandget_current_chainreturn types change fromOption<String>toResult<Option<String>>.src/cli/handlers.rsget_token()andget_current_chain()updated to handle the newResultwrapper with?.main().src/main.rs— no changes requiredmain()already catches anyErrreturned fromCommandHandler::handle(), prints it tostderrin a user-friendly format, and exits with code1. The new error flows through this existing path automatically.Tests (
src/utils/mod.rs)get_config_dir_fails_when_home_unset— unsetsHOMEunder the existingENV_LOCKmutex (which serializes all env-var-mutating tests), callsget_config_dir(), and asserts the result isErrcontaining"Could not determine home directory". RestoresHOMEon exit.config_dir_created_with_mode_0700— unwraps the now-Resultreturn value (safe within the test sinceHOMEis explicitly set).Why the hard-error approach
The alternative (loud
eprintln!warning + implicit fallback path) was explicitly considered and rejected. A warning is easy to miss in automated pipelines, and any silent path selection — even a "better" one like$XDG_CONFIG_HOMEor/tmp— creates new implicit behaviour that can still surprise users. A hard error ensures the failure is never invisible and forces the operator to fix the root cause ($HOMEmust be set).Breaking change
Any workflow that relied on the undocumented behaviour of txio writing to
./.txio/whenHOMEwas unset will now receive an explicit error instead of silently proceeding. This is the desired outcome — the old behaviour was a bug, not a feature.Acceptance criteria
dirs_next::home_dir()returnsNone, the CLI exits with a clear error message (no silent fallback to.).get_config_dir()handle theResultproperly — no.unwrap()-induced panics introduced.