Skip to content

the state directory belongs to the user - #134

Merged
shisirbasel merged 17 commits into
mainfrom
state-directory-permissions
Sep 10, 2026
Merged

shisirbasel merged 17 commits into
mainfrom
state-directory-permissions

Conversation

@shisirbasel

@shisirbasel shisirbasel commented Sep 9, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #114

~/.bravebot landed at the process umask: drwxr-xr-x, with history at -rw-r--r--. That file
is every prompt anybody has typed, meaning the paths they were working on, the branch names, and
whatever they pasted into one. On a shared build host every local account could read it.

The modes were also a matter of call order. The credential store wrote 0600 and the session store
0700, each creating the parent with a bare create_dir_all, so ~/.bravebot ended up 0700 or 0755
depending on which subsystem happened to write first.

One helper creates a directory under it 0700 and one writes a file into it 0600, and every writer
that can reach them goes through them: the history, the model, theme, effort and editing choices,
the update answer, and the session records that had their own copies. The crates that sit below the
one holding the helper keep their own copy of the modes rather than inverting a dependency for four
lines: the one that imports a subscription, and the language server client.

Both narrow what is already there. Creating a directory that exists does not touch its mode, so
without that this would reach no machine that has run an older build, which is every machine
holding the history worth protecting. Narrowing stops at the state directory: what else is in
somebody's home is not this program's business.

The writers that were outside it

Three subsystems write into ~/.bravebot without going through the helper, and each has to ask for
the modes itself.

  • The language server index. A bare create_dir_all, so a directory holding an index derived
    from every file in the workspace landed at the umask. Because it is recursive, on a machine where
    nothing had written to ~/.bravebot yet this is what created the state directory, at the umask,
    for the history that arrived later.
  • The installer. mkdir -p at the umask, before the program has run once, so a fresh install
    opened the directory the history would land in. The record it writes there is created under
    umask 077 now rather than written over.
  • The credential file. The directory around it was narrowed and the file inside it was not, so
    the one holding a bearer token kept whatever mode it was found with.

~/.bravebot/themes is named in the issue but nothing outside tests creates it, so there was
nothing to change there. crates/tui/src/update.rs gained a writer after the issue was filed, and
that one was landing at the umask.

A path bug this turned up

cache_for took a home and appended .bravebot itself, while every caller passes
home::directory(), which is that directory already. The index for a workspace was landing in
~/.bravebot/.bravebot/lsp/<hash>: a directory nothing else writes to, reads or narrows. Its four
clauses' worth of tests passed their own scratch home, so they agreed with the function rather than
with the host. The argument is the state directory now, the joined name is a constant, and a test
pins that nothing sits between them. An index under the old path is orphaned rather than moved;
nothing reads it and the server rebuilds one on the first question.

Checked

Driven through contrib/drive_tui.py against a scratch HOME with umask 022, a real session typed
into a real terminal. Before, on the parent commit:

drwxr-xr-x  .bravebot
-rw-r--r--  history
drwx------  sessions

After:

drwx------  .bravebot
-rw-------  history
drwx------  sessions
-rw-------  sessions/<project>/<id>.json
-rw-------  sessions/<project>/<id>.audit.jsonl

The installer fragment run against a scratch HOME seeded 0755/0644 under umask 022 leaves
drwx------ .bravebot and -rw------- installed-by.

Seventeen tests, writing real files and reading real modes back: sixteen pin STATE-1 and one pins
the cache path under LSP-10. With the source reverted and the tests kept, five of the six
integration tests fail; the sixth asserts that nothing above the state directory is touched, and
passes either way by design. Each of the other writers' tests fails against its own unfixed code.

Spec

New docs/specs/state-directory.md, id STATE. STATE-1 is the rule this change establishes, and
governs names every writer it covers, the installer included. STATE-2 states where the directory
is and that a missing HOME yields no directory rather than a guessed one: existing behaviour with
existing tests, included so the file stands alone rather than being a spec about modes with no
subject.

STATE-2 says what holds across resolvers rather than claiming there is one. There are three:
crates/agent/src/home.rs, crates/skus/src/store.rs and crates/config/src/settings.rs. Two of
those crates sit below the one holding the answer, so layering forbids them the dependency and one
definition is not available to be had. What has to hold across them is the name and the refusal to
guess, and each is pinned on that. The language server client resolves nothing: it is handed the
directory.

@shisirbasel
shisirbasel marked this pull request as draft September 9, 2026 21:33
@shisirbasel
shisirbasel force-pushed the state-directory-permissions branch 2 times, most recently from 74f5b6d to bc05d6e Compare September 10, 2026 14:18
~/.bravebot landed at the process umask: drwxr-xr-x, with history at
-rw-r--r--. That file is every prompt anybody has typed, meaning the
paths they were working on, the branch names, and whatever they pasted
into one. On a shared build host every local account could read it.

The modes were also a matter of call order. The credential store wrote
0600 and the session store 0700, each creating the parent with a bare
create_dir_all, so ~/.bravebot ended up 0700 or 0755 depending on which
subsystem happened to write first.

One helper creates a directory under it 0700 and one writes a file into
it 0600, and every writer that can reach them goes through them: the
history, the model, theme, effort and editing choices, the update
answer, and the session records that had their own copies. The crate
that imports a subscription depends on nothing, so it keeps its own
copy of the modes rather than gaining a dependency for four lines.

Both narrow what is already there. Creating a directory that exists
does not touch its mode, so without that this would reach no machine
that has run an older build, which is every machine holding the history
worth protecting. Narrowing stops at the state directory: what else is
in somebody's home is not this program's business.

state-directory.md is the spec #86 asked for and #111 stopped short of,
covering the directory rather than one file at a time.

Closes #114
@shisirbasel
shisirbasel force-pushed the state-directory-permissions branch from bc05d6e to c4d07e7 Compare September 10, 2026 14:35
@shisirbasel
shisirbasel marked this pull request as ready for review September 10, 2026 15:50
…side it

cache_for took a home and appended `.bravebot` itself, while every caller
passes home::directory(), which is that directory already. The index for a
workspace landed in ~/.bravebot/.bravebot/lsp/<hash>: a directory nothing
else writes to, reads, or narrows, holding an index derived from every file
in the tree.

The argument is the state directory now, and the name it is joined with is a
constant, so the doubling cannot come back by way of a caller reading the
parameter name differently. The tests passed their own scratch home, so they
agreed with the function rather than with the host, which is why the path
survived four clauses' worth of coverage.

An index under the old path is orphaned rather than moved. Nothing reads it,
and the server rebuilds one on the first question it is asked.
The cache directory was created with create_dir_all, so it landed at the
process umask: drwxr-xr-x for a directory holding an index derived from
every file in the workspace. Who may read that is who may read the source
it was built from.

It is also created recursively, so on a machine where nothing has written
to ~/.bravebot yet, this is what creates the state directory, at the umask,
for the prompt history that arrives later. That is the call order STATE-1
exists to settle.

Narrowed as well as created, since a directory keeps the mode it was made
with. Two levels and no further: the state directory above them belongs to
whichever subsystem created it, and this crate has no business setting it.
The modes are spelled out here rather than shared, because this crate
depends on the kernel alone and reaching the helper would invert that.
The mode on the open is asked for as the file is created and says nothing
about one already on disk, which a person may have restored from a backup
or copied between machines. The directory around it was narrowed and the
file inside it was not, so the one holding a bearer token was the one left
as it was found.

Narrowed between the open and the write, so the file is private while it is
empty rather than from the moment it holds a token again.
…e user

The installer creates ~/.bravebot to record which path it wrote the binary
to, and it did so with a bare mkdir -p at the umask. On a fresh machine that
is what creates the directory, so it was drwxr-xr-x before the program had
run once, and the prompt history arrived into a directory an install had
already opened.

The record is removed and written again under umask 077 rather than written
over, so its mode is the one it is created with rather than one a chmod
catches up with. A directory already there is narrowed, which is the case on
every machine that has run an earlier install.
The clause claimed every subsystem resolves ~/.bravebot through the same
answer, and argued from the cost of having two. There are four: the agent's
own, the credential store's, the settings layer's and the language server
client's. Three of those crates sit below the one holding the answer, so
layering forbids them the dependency and one definition is not available to
be had.

Stated as the rule the tests actually pin: the name, and no fallback where
HOME says nothing. What has to hold across independent resolvers is that
much, and each is pinned on it, so the clause is now one a reader can hold a
diff against.
Three roots were in use for a test that redirects HOME: target/test-scratch,
which testutil documents and five crates provide, the system temporary
directory, and target/scratch. The last is the one the state directory tests
introduced, and the reason testutil exists is written in its doc comment, so
a second spelling of the same idea is the thing it was meant to prevent.

The agent's own tests reach the helper directly, since it is a unit test in
the crate that has one. The integration test cannot, so it keeps its local
copy and spells the same root, and gains the Drop its siblings have: without
one, a run left a scratch home behind holding a fake prompt history.
@bbondy

bbondy commented Sep 10, 2026

Copy link
Copy Markdown
Member

Reviewed and pushed the fixes onto this branch as six commits, rather than splitting any of them
out into follow-up issues. The original change is right and its tests hold; what follows is
everything I found, and where each of them landed.

STATE-1 claimed more than the code delivered. The clause says a directory under ~/.bravebot
is created 0700 and a file written into one 0600, "whichever subsystem is doing the writing", and
three subsystems did not.

  • crates/lsp/src/server.rs created the language server's index directory with a bare
    create_dir_all, so a directory holding an index derived from every file in the workspace landed
    at the umask. It is recursive, so on a machine where nothing had written to ~/.bravebot yet, the
    language server was what created the state directory, at the umask, for the history that arrived
    later. That is exactly the call-order failure the clause exists to settle. Fixed in f2cc538,
    which narrows the two directories the crate owns and stops below the state directory, since what
    that is set to belongs to whoever created it. The modes are spelled out in the crate rather than
    shared, for the layering reason this PR already gives for the credential store.
  • install.sh did mkdir -p "$STATE_DIR" at the umask and wrote installed-by into it, before the
    program has run once, so a fresh install opened the directory the history would land in. Fixed in
    f07fbb0: the directory is narrowed, and the record is removed and written again under
    umask 077 rather than written over, so its mode is the one it is created with.
  • crates/skus/src/store.rs narrowed the parent directory but not the credential file. The mode on
    the open applies at creation and says nothing about a file already there, which a person may have
    restored from a backup. Fixed in db52ea8, narrowed between the open and the write so the file is
    private while it is empty.

Both new writers are in STATE-1's governs now, and each fix ships with a test that fails against
its own unfixed code.

STATE-2's first sentence was false. It claimed every subsystem resolves ~/.bravebot through
the same answer and argued from the cost of having two. There are four: crates/agent/src/home.rs,
crates/skus/src/store.rs, crates/config/src/settings.rs and crates/lsp/src/server.rs. Three of
those crates sit below the one holding the answer, so layering forbids them the dependency and one
definition is not available to be had. Reworded in 1dc0bc2 to the rule the four verified-by
tests actually pin: the name, and no fallback where HOME says nothing.

A path bug the spec work turned up, fixed here rather than filed. cache_for took a home and
appended .bravebot itself, while every caller passes home::directory(), which is that directory
already. The index was landing in ~/.bravebot/.bravebot/lsp/<hash>, a directory nothing else
writes to, reads or narrows. The four LSP-10 tests passed their own scratch home, so they agreed
with the function rather than with the host, which is how it survived. 40adca6 makes the argument
the state directory, makes the joined name a constant, and adds a test under LSP-10 pinning that
nothing sits between the two. An index under the old path is orphaned rather than moved: nothing
reads it, and the server rebuilds one on the first question it is asked.

Test hygiene, all in 732e6b9. The new tests rolled their own target/scratch root while the
workspace documents target/test-scratch in testutil.rs, five crates provide it, and this PR's
own skus test lands there. The agent's unit tests now reach crate::testutil::scratch_dir
directly, which was available to them all along and is already used by cmdline.rs and
programs.rs. The integration test cannot reach it, so it keeps a local copy spelling the same
root, and gains the Drop its siblings have: without one, every run left a scratch home behind
holding a fake prompt history. crates/cli/src/main.rs had the same drift and is aligned too.

The description was stale and is updated: seventeen tests rather than eleven, and five of six
integration tests fail without the fix rather than four of five.

Verified on the branch head: make check passes, make check-spec reports 0 errors with no new
warnings, and make check-toolchain is quiet. Each of the six commits was checked to build, format,
pass clippy with -D warnings and pass the spec check on its own, since the pre-commit hook reads
the working tree rather than what is staged. The installer fragment was run against a scratch HOME
seeded 0755/0644 under umask 022 and leaves drwx------ and -rw-------.

Moving the index to the right path leaves the old one where it was. That
path is `~/.bravebot/.bravebot/lsp/`, which nothing reads, rebuilds or
narrows, holding an index derived from every file in the workspace at
whatever the umask gave it. On a machine that has run an earlier build it
stays there for as long as the machine does, which is the case this
branch exists to close.

Removed rather than narrowed: nothing will ever read it, so a private
copy of it is worth no more than none, and narrowing would mean walking
a tree to reach the files inside.

The path is derived rather than spelled. Appending the state directory's
own name is what put it there, so joining that name is what finds it, and
the state directory keeps one definition. Only a directory holding what
this crate would have written is removed, so one that happens to carry
the same name and holds something else is left where it is.
@github-actions

Copy link
Copy Markdown

[puLL-Merge] - brave/bravebot@134

Description

Centralizes state-directory file/dir creation in bravebot_agent::home with 0700/0600 modes, and tightens existing paths left open by older builds. Replaces per-callsite create_dir_all/OpenOptions in tui::store, tui::sessions, tui::update. LSP cache path fixed: cache_for now takes ~/.bravebot itself instead of $HOME (old code appended .bravebot again → index landed in ~/.bravebot/.bravebot/lsp), plus one-shot removal of the misplaced tree and mode narrowing. skus::store::save chmods parent dir and existing credential file. install.sh chmods state dir and writes the install record under umask 077. Test scratch dirs moved from env::temp_dir/target/scratch to target/test-scratch.

Possible Issues

  • **`crate::test

`Servers::new` takes `~/.bravebot` itself, so passing a bare `$HOME` puts the
index in the home directory rather than under the state directory: on a machine
with rust-analyzer installed the test sets `CARGO_TARGET_DIR` to `$HOME/lsp/<hash>`
and cargo fills it with a build tree next to the user's own files.
…ot make

The index directory was created for its effect and the result thrown away, so a
server handed one that could not be made created it itself, at the umask, and
filled it with an index derived from every file in the workspace. Go's
`GOCACHE` sat one level deeper than anything created here and was made by the
server every time.

Every directory a server is pointed at is now created before it starts, and a
failure to create one stops the launch and says so. Dropping the variable
instead would write the index into the workspace, which is what LSP-10 forbids.
…as given

`set_permissions` resolves a link, and the bound on how far narrowing walks is
a comparison of paths, which says where a name sits and nothing about where it
leads. Somebody who keeps their sessions on a synced volume and links the
directory into place had the target set to 0700, which is a directory outside
anything this was given.

Both copies of the rule step over a link rather than following it: the helper
under the state directory, and the language server client's own.
`mkdir -p` at the umask creates the directory 0755 on a fresh machine and the
chmod a syscall later closes it, which is the ordering the comment beside it
says the record avoids. Asking for the mode as the directory is made leaves no
window; the chmod stays, since that is what narrows a directory an earlier
install left.
…he umask

Two problems in the same tests. The scratch directory was removed on the last
line of the test body, so a failing assertion left a tree of directories with
modes on it and the next run measured what the failure left. And the mode
asserted on the state directory came from `create_dir_all` at the process
umask, so `cargo test` failed under any umask but 022.

A guard removes the directory however the test ends, and the mode the claim is
about is set by the test rather than inherited from whoever runs it.
…ames

`crates/tui/src/sessions.rs` is the largest writer into the state directory and
one of STATE-1's tests exists to pin it, but a diff touching the file was not
reviewed against the spec. `crates/config/src/settings.rs` resolves the
directory the way STATE-2 describes and nothing pinned it, so a fallback added
there would have been caught by nothing.

Both are now governed, and the settings resolver has the tests the other two
have.
Two claims a reviewer could hold a diff against and find false. STATE-1 read as
though every file in the directory were narrowed, and a file this program only
reads is not: `settings.json` sits at 0644 on a machine whose umask put it
there, and nothing here ever writes it. That is the directory mode's job, and
it is a known cost rather than a clause.

The file helper is also used for one write outside the directory, the export
SESSION-17 governs, which a clause titled "everything written into it" has to
account for.
A test's doc comment described the tightening an earlier implementation had,
in the past tense, so a reader who has only seen this code cannot check it
against anything. The scratch-directory helper this branch adds opens the same
way. Both say the reason the rule holds instead.
Comment thread crates/config/src/settings.rs Resolved
Comment thread crates/config/src/settings.rs Resolved
Comment thread crates/config/src/settings.rs Resolved
Comment thread crates/config/src/settings.rs Resolved
Three tests set the variable to check what `home` makes of it, which
takes a lock against every other test in this binary, a restore on the
way out, and four unsafe blocks to do it. The security scan flags each
of those, and rightly: an environment variable is process-wide, and
setting one while other tests run is what the unsafety is about.

The rule is a function of one string, so it is split from the read and
checked directly. The tests keep their names and what they assert, the
lock and the restore go, and they no longer depend on the order the
binary runs them in.
@shisirbasel

Copy link
Copy Markdown
Collaborator Author

commits lgtm

@shisirbasel
shisirbasel merged commit bef5ef5 into main Sep 10, 2026
13 checks passed
@shisirbasel
shisirbasel deleted the state-directory-permissions branch September 10, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

store: ~/.bravebot and the prompt history are written world-readable

3 participants