Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

What secure-dotfile changes

Give an AI a configuration file with an API key written into it, and the key goes into the conversation along with the configuration. To choose a configuration change, the AI needs to know which variable to update and where its value will come from. It doesn't need the key itself.

In my secure-dotfile tool, a separate program reads the configuration and moves secret values into a credential store. The AI receives migration candidates and operation results. Consider this line in .bashrc, a file the shell reads at startup. The value is a dummy string used for testing.

bash
export DEMO_API_KEY="demo-article-value-1234567890"

After moving the value, the configuration can contain an expression that retrieves it from the store. The test described below produced this replacement line.

bash
export DEMO_API_KEY="$(secret-tool lookup service DEMO_API_KEY account "$USER" 2>/dev/null)"

The variable is still named DEMO_API_KEY. At shell startup, secret-tool lookup asks the store for its value. secret-tool is a command for storing and retrieving credentials. These tests used a replacement command backed by a temporary file, without connecting to a real credential store.

Removing the literal from the file doesn't stop a program from receiving the value at startup. What the configuration contains and what the AI has permission to read remain separate questions. First, consider the information the configuration-reading program returns.

A literal value in a shell configuration is replaced with a lookup from a credential store.

Data returned to the AI

The scan command examines configuration files and returns candidate variables and proposed changes as JSON. These are the fields used to select and change a candidate in the dummy configuration.

json
{
  "name": "DEMO_API_KEY",
  "path": "~/.bashrc",
  "line": 1,
  "item": "~/.bashrc:1:DEMO_API_KEY",
  "movable": true,
  "becomes": "export DEMO_API_KEY=\"$(secret-tool lookup service DEMO_API_KEY account \"$USER\" 2>/dev/null)\""
}

item identifies the migration target by file, line number, and variable name. The caller passes that string to move to select a target. movable indicates whether the implementation can migrate the assignment; becomes contains the proposed replacement line.

There is no dedicated value field in this output. But becomes is also a string, and it can still contain the secret. In a test where the same dummy value appeared in a trailing comment, that comment carried the value into the output.

Candidate detection also depends on recognized variable names, value patterns, and supported single-line assignments. A count of zero candidates doesn't mean the entire file is free of secrets.

scan returns an item identifier for selecting a target and a becomes string describing the replacement.

Moving a secret value

Returning a candidate from scan doesn't save a value or rewrite a file. A separate move command takes the selected item and the destination store. Separating inspection from modification lets the caller choose what to change after examining the candidates.

The sequence is as follows.

  1. Inspect candidates with scan. Check the variable name, line number, migration support, and proposed replacement.
  2. Choose an item and a store. Specify the configuration entry to change and the destination for its value.
  3. Inspect the plan with move --dry-run. Receive the proposed change without saving the value or rewriting the file.
  4. Run move. Save the value, read it back, and then proceed to the configuration update.
  5. Check the configuration and output separately. Compare the stored value with the input, inspect the file diff, rescan, and check whether the output still contains the value.

A configuration change and an output free of secret values are separate results. Saving and rewriting can both succeed while a display string still carries a secret into the conversation.

Inspection, target selection, a dry run, and migration are separate steps with different effects.

Saving and reading back

The store may already have an entry with the same name as the variable being migrated. The store function first reads that entry and compares it with the input. This excerpt from the public implementation handles an existing value.

python
want = value.encode("utf-8")
try:
    state, have, code, err = read_secret(name)
    if state == "found":
        if have == want:
            return None
        return "a different value is already stored as %s; remove or rename that item in the store first, then run the " \
               "move again" % name

want is the value being migrated; have is the value read from the store. An identical value allows the operation to continue. A different value stops it before the configuration is rewritten, avoiding an unconditional overwrite of credentials that happen to share a variable name.

If no entry exists, the function saves a new one. It sends the value through standard input rather than as a command-line argument. In the secret-tool branch, input carries the value.

python
r = subprocess.run(["secret-tool", "store", "--label=%s (added by secure-dotfile)" % name,
                    "service", name, "account", ACCOUNT],
                   input=value.encode("utf-8"), capture_output=True, timeout=TIMEOUT)

The storage program and the store still read the value. This approach removes the command-line argument as one path through which the value would be passed. The implementation also uses standard input with macOS's security -i and 1Password's op item create -; neither was exercised in these tests.

After the storage command finishes, the function reads the value back. It allows the configuration update only after the returned bytes match the original value. A successful command exit alone doesn't establish that the saved value can be retrieved correctly.

An existing value must match; a new value must be saved and read back before the configuration update proceeds.

Rewriting the configuration

move rereads the selected line and checks that it is a supported assignment to the selected variable. The value to save comes from this reread, rather than from a comparison with the value seen during the earlier scan. Once saving and read-back verification succeed, it replaces the assignment's value with a lookup expression.

Before writing the edited text to the file, the implementation checks its syntax. It checks both the original and edited configurations, using this condition to stop the update.

python
before_ok, _ = syntax_check(path, original)
after_ok, label = syntax_check(path, edited)
if before_ok and after_ok is False:

If the original configuration is valid and the edited version is invalid, the file stays unchanged. This condition does not reject every case where syntax checking is unavailable or the original file is already invalid. A verified syntax result and an unavailable check have different meanings.

With this ordering, an entry may already have been saved even when the file update doesn't happen. Saving to the store and changing the configuration are separate updates. After a failure, both states need inspection.

The configuration's consumer also determines which replacement format works. A shell can evaluate a lookup expression, but .npmrc, which npm reads, cannot execute that expression. For .npmrc, the tool writes an environment-variable reference in the form ${NAME} and adds a line to the shell configuration that sets the variable.

For AWS, it uses credential_process, which specifies a program that retrieves credentials. .env and .netrc are detected but not migrated automatically. Files generated by chezmoi aren't changed directly either; the tool directs the caller to edit the source and apply it. Moving a value into a store and giving a configuration consumer a usable replacement are separate parts of the implementation.

Store updates happen before file updates, and each configuration consumer needs a replacement format it can use.

Testing with dummy configuration

For this article, the original CLI was run to check what remains after a failure, alongside an inspection of its save-and-rewrite logic. The four cases were a normal migration, a conflict with an existing value, a read-back mismatch, and a value repeated in a comment. Each case checked the configuration, the replacement store, the output, and the next scan.

The environment was macOS with Python 3.15.2. The tests used a temporary HOME, working directory, and PATH. That PATH contained only the replacement secret-tool and bash for syntax checking. The replacement command read and wrote test files; it did not reproduce a real store's encryption or authentication.

The script came from public commit bd2711bd45fd4f529858181573b99e2738afabf5. It ran unchanged in a separate process, and its SHA-256 was identical before and after execution. The public tests also use disposable environments and replacement commands, but the four results below come from tests prepared for this article.

Four dummy-data cases compare the configuration, store, command output, and rescan result.

A normal migration

The initial dummy value went into a test .bashrc. The CLI then scanned the file, showed the planned change, and performed the migration with these arguments. Here, ~ denotes the test HOME; the actual test passed the temporary directory's absolute path.

bash
python3 secure_dotfile.py scan --store secret-service --json
python3 secure_dotfile.py move --store secret-service --item '~/.bashrc:1:DEMO_API_KEY' --dry-run --json
python3 secure_dotfile.py move --store secret-service --item '~/.bashrc:1:DEMO_API_KEY' --json

--item selects the target returned by the scan, and --store selects the store type. These commands describe a disposable test with a replacement command, not instructions to run against a configuration containing real secrets. After --dry-run, both the configuration and the replacement store were unchanged.

The subsequent move returned this result.

json
{
  "results": [
    {
      "name": "DEMO_API_KEY",
      "path": "~/.bashrc",
      "line": 1,
      "status": "moved",
      "note": "stored in the secret service as DEMO_API_KEY; syntax check passed (bash -n)"
    }
  ]
}

status was moved, and note reported storage and the bash -n syntax check. Beyond the displayed result, the stored value matched the input, and the configuration contained the lookup expression shown at the start. A rescan returned zero candidates. Neither the scan output nor the dry-run output contained the dummy value in this case.

The normal case saves the input value, replaces the assignment with a lookup, and returns moved.

A conflicting stored value

Putting a different dummy value under the same name in the store changed the result of the same migration command. The operation was skipped for this reason.

json
{
  "status": "skipped",
  "note": "a different value is already stored as DEMO_API_KEY; remove or rename that item in the store first, then run the move again"
}

This excerpt shows the status and reason from results. Neither the existing stored value nor the original configuration line changed. A rescan still found one candidate.

The CLI nevertheless exited with code 0. A caller checking only the process exit code would miss that the target wasn't migrated. It needs to read each target's status and note, then decide whether to keep or rename the existing entry before retrying.

A conflicting existing value leaves both the stored entry and the configuration unchanged, even though the CLI exits with code 0.

A read-back mismatch

A storage command can exit with code 0 while the subsequent read returns a different value. To test this, the replacement command accepted a write but returned a different string on lookup. The migration's status and reason were as follows.

json
{
  "status": "skipped",
  "note": "the value could not be stored and read back (secret-tool exited with 0); unlock the keyring and run the move again"
}

The configuration stayed unchanged, and a rescan still found one candidate. The input value was present in the replacement store's file, however. An entry had been saved even though the result was skipped.

The message suggests unlocking the keyring, but the condition introduced by this test was a mismatched read-back string. That message doesn't establish the cause of a real store failure. Because a value can be saved while the configuration remains unchanged, inspect both before retrying.

A read-back mismatch can leave a saved value in the store while the configuration remains unchanged.

A value left in the output

The final case repeated the assignment's dummy value in a comment.

bash
export DEMO_API_KEY="demo-article-value-1234567890" # demo-article-value-1234567890

Scanning this line produced a becomes string in the following form. The dry-run result included the same comment.

bash
export DEMO_API_KEY="$(secret-tool lookup service DEMO_API_KEY account "$USER" 2>/dev/null)" # demo-article-value-1234567890

Only the assignment's value span is replaced, so the trailing comment survives. The actual CLI confirmed that a secret can leave through becomes even without a dedicated value field. This output path is unfixed in the pinned public version.

The migration also returned moved, and a rescan returned zero candidates. Replacing the assignment's literal with a lookup removed it from candidate detection, but the value remained in the comment. Neither a successful migration nor the candidate count established that the output and file were free of the value.

A separate function-level reproduction also left the value of a later assignment on the same line in the proposed replacement string. That case was not one of the four CLI tests. Function-level observations, CLI observations, and behavior untested against real stores are recorded separately.

Replacing the assignment value leaves the trailing comment in both the proposed output and the updated file.

Applying these findings to your own tools

The implementation and tests suggest several checks for other configuration-editing tools. These are design proposals derived from the results, not a claim that the current secure-dotfile implementation satisfies all of them.

When building data to return to an AI, decide how much of the original line to reuse. Replacing one span and returning the rest also returns comments and later commands. Constructing display data from the variable name and target location lets the tool choose each returned field, though reason strings and standard error still need checking for secret values.

An output test can make two separate assertions: there is no value field, and the complete output doesn't contain the input dummy string. The commented configuration passes the first assertion and fails the second. Comparing against the original string catches a failure that a field-name check misses.

For update ordering, check that the read-back value matches in addition to checking that the save command finished. Report when a value has been saved but the configuration is still unchanged, so the caller can inspect both states before resubmitting an operation that failed partway through.

Success checks also need to distinguish the process exit code, each item's migration result, and remaining copies of the value. The conflict and read-back mismatch returned skipped despite an exit code of 0. The commented case returned moved and zero rescan candidates while retaining the value. Test expectations can preserve those distinctions instead of treating completion as proof that a confidentiality condition holds.

Design checks distinguish output fields from whole-output inspection, read-back verification from command exit, and store state from file state.

In the comparison table, residual output refers to scan and dry-run output. Item statuses come from move; its result JSON did not contain the dummy value in these tests.

Runtime permissions and inspection limits

Restricting returned output doesn't prevent the AI from reading a value through another path. A value retrieved by the shell enters the environment of programs it starts. If the AI runs with permission to read that environment, moving the literal out of the configuration doesn't prevent access.

A skill instruction prohibiting direct reads of a configuration file is also different from the operating system denying access. The tool's migration output needs inspection, and the runtime needs a decision about which programs receive the value. These tests did not verify AI-agent isolation or OS-enforced read denial.

Git history and backups remain separate copies after configuration migration. This tool's Git-history inspection covers the target file and has a 64 MB limit. The tests didn't use Git, so the result was unknown. An unavailable inspection is not a result saying that no value was found.

Even a history result of no doesn't cover other files or backups. Moving a value into a store doesn't revoke or reissue a key either. The current configuration, retained copies, and permissions to read the value each require their own check.

Migration output control and permission to read a runtime environment protect different paths to a secret value.

Closing

When checking a configuration-editing tool, compare the complete output with the dummy input value, then inspect the stored entry and the file separately. In these tests, moved and zero rescan candidates still left the value in a comment. Check runtime read permissions separately from those migration results.

  • secure-dotfile implementation and usage at the tested commit