Skip to content

sessions: restrict session file permissions to 0600 and directories to 0700 - #111

Merged
bbondy merged 1 commit into
brave:mainfrom
winshaurya:fix/secure-session-permissions
Sep 8, 2026
Merged

bbondy merged 1 commit into
brave:mainfrom
winshaurya:fix/secure-session-permissions

Conversation

@winshaurya

Copy link
Copy Markdown
Contributor

Hello @bbondy,

This pull request addresses #86 by restricting session files and directories to the current user on Unix platforms.

What is changed

  • crates/tui/src/sessions.rs: Session records (.json), temporary files (.tmp), and audit trails (.audit.jsonl) are written with mode 0600. Session directories under ~/.bravebot/sessions are created with mode 0700. Existing files and directories are tightened on write.
  • crates/tui/tests/sessions.rs: Added Unix tests confirming that session records and audit trails are written mode 0600, session directories are created mode 0700, and existing looser permissions are tightened.
  • docs/specs/sessions.md: Added normative clause SESSION-16 covering the mode 0600 file and mode 0700 directory security guarantees.

Why and how it solves the issue

Session records store conversation history, prompts, model responses, code snippets shown to the planner, and accumulated standing permissions. Previously, files and directories were created with the default process umask (typically 0644 and 0755), leaving session data world-readable on shared or multi-user systems. Writing with mode 0600 and creating directories with mode 0700 matches the pattern in crates/skus/src/store.rs:219 and crates/tui/src/editor.rs:160, keeping private session state readable only by the owning user.

Please let me know if any adjustments or additions are desired. Thank you for your time and review!

@winshaurya
winshaurya force-pushed the fix/secure-session-permissions branch from 2283359 to 81ce7a0 Compare September 6, 2026 22:45
…ies to 0700

Session records, temporary files, and audit trails under ~/.bravebot/sessions
hold the whole conversation: prompts, model replies, file snippets shown to the
planner, and accumulated standing permissions. Created at the default process
umask, typically 0644 and 0755, those records are readable by every local
account on multi-user machines and shared hosts.

On Unix systems:
- Write session records (.json), temporary files (.tmp), and audit trails
  (.audit.jsonl) with mode 0600.
- Create session directories with mode 0700.
- Tighten pre-existing files and directories on write.

Adds SESSION-16.

Closes brave#86

@bbondy bbondy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. Writing the record 0600 before any content goes into it, rather than writing and then tightening, is the right shape and matches what crates/skus/src/store.rs does for credentials.

I pushed a small follow-up onto the branch: rebased on main, fixed the Event variant the two new tests used so they compile and run, ran cargo fmt, collapsed the nested if in ensure_dir_secure for clippy, and bumped sessions.md to 16 clauses in docs/specs/README.md so make check-spec passes. Both tests fail on the parent commit and pass with the change.

~/.bravebot itself and the prompt history are still written at the umask; that is the rest of #86 and is now #114.

@bbondy
bbondy merged commit ef94593 into brave:main Sep 8, 2026
shisirbasel added a commit that referenced this pull request Sep 9, 2026
~/.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 added a commit that referenced this pull request Sep 10, 2026
~/.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 added a commit that referenced this pull request Sep 10, 2026
~/.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 added a commit that referenced this pull request Sep 10, 2026
* the state directory belongs to the user

~/.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

* fix(lsp): the index goes under the state directory, not one nested inside 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.

* fix(lsp): the index a server keeps is reachable only by its owner

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.

* fix(skus): narrow the credential file that is already there

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.

* fix(install): the state directory the installer creates belongs to the 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.

* docs(state-directory): STATE-2 says what holds across the resolvers

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.

* test: the scratch directories follow the one convention for them

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.

* fix(lsp): remove the index an earlier build left one directory too deep

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.

* fix(lsp): the live tests hand the servers the state directory

`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.

* fix(lsp): a server is not pointed at a directory this process could not 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.

* fix: narrowing a directory does not follow a link out of the one it was 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.

* fix(install): the state directory is created with the mode it keeps

`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.

* test(lsp): the cache tests clean up after a failure and do not read the 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.

* docs(state-directory): the spec covers every writer and resolver it names

`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.

* docs(state-directory): STATE-1 says how far the file modes reach

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.

* docs: two comments say what is true rather than what changed

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.

* test(config): the state directory's rule is checked without setting HOME

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.

---------

Co-authored-by: Brian R. Bondy <[email protected]>
@winshaurya
winshaurya deleted the fix/secure-session-permissions branch October 10, 2026 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants