Lock idle Salesforce orgs behind a 1Password unlock so the sf CLI can't deploy to them by accident.
As a Salesforce ISV partner, I connect to a lot of orgs. Our own production org, our packaging org, our DevHub, sandboxes, scratch orgs, and most importantly, customer orgs. The Salesforce CLI (sf) makes hopping between them easy. Sometimes too easy.
A typo on an sf project deploy start --target-org XYZ can push code into the wrong place. Incorrectly push to an internal org, that's an inconvenience. Incorrectly push to a customer org, that's a nightmare.
I wanted a small, simple guardrail that made it less likely that we would accidentally deploy to a sensitive org without an explicit act of intent.
It shouldn't require any changes inside the customer org(s), require no new infrastructure, and would need to be more robust than an "are you sure?" prompt I could dismiss on autopilot.
This README walks through (high-level, and my best understanding of) how the sf CLI stores authentication, why moving a single file is enough to make an org unreachable, and includes the full source of the scripts I use.
When you run sf org login web --alias MyOrg, the CLI walks through the standard OAuth flow and then writes the resulting tokens to your local filesystem under ~/.sfdx/. That directory contains a few interesting things:
~/.sfdx/
├── alias.json alias to username mapping
├── key.json local AES key used for token encryption
├── you@yourcompany.com.json one file per authenticated user
├── admin@customer-prod.com.json ...
└── ...
alias.json is a flat object mapping aliases to usernames:
{
"orgs": {
"DevHub": "you@yourcompany.com",
"AcmeProd": "admin@customer-prod.com"
}
}Each per-user JSON file contains the access token, refresh token, instance URL, org ID, and a bunch of other metadata fields:
{
"accessToken": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"instanceUrl": "https://yourcompany.my.salesforce.com",
"orgId": "00D40000000XXXXYYY",
"username": "someuser@someorg.com",
"loginUrl": "https://login.salesforce.com",
"refreshToken": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxx",
"clientId": "PlatformCLI",
"isDevHub": true,
"instanceApiVersion": "67.0",
"instanceApiVersionLastRetrieved": "7/21/2026, 7:50:21 PM",
"name": "Some Org, LLC",
"instanceName": "USA334",
"namespacePrefix": null,
"isSandbox": false,
"isScratch": false,
"trailExpirationDate": null,
"tracksSource": false,
"orgEdition": "Enterprise Edition"
}If we move ~/.sfdx/<username>.json out of ~/.sfdx/ and stash it somewhere that requires a real unlock action to retrieve, the org becomes unreachable to the CLI.
Put a different way: if this file is available, the CLI can use the org. If it is not, the CLI can't.
For me, the "somewhere that requires a real unlock" is a combination of a few scripts and the 1Password CLI.
The 1Password CLI (op) reads from your local 1Password app, and any read/write operation against a vaulted item triggers whatever unlock policy you have configured (system password, YubiKey, etc).
I've been leaning on the 1Password CLI for quite a bit lately. Besides storing these org authentication JSON files as secure notes, I've just about fully replaced any .env-type files that contained sensitive tokens / keys with callouts to the 1Password CLI to retrieve them, in realtime, when they're actually needed. The goal is zero plain text credentials of any kind being stored on my laptop.
For this use case, is it overkill to store these JSON files in 1Password? Could we have just as easily moved the files out of .sfdx into another directory that the Salesforce CLI doesn't check?
Maybe, but in these days of semi-autonomous grep-ing agents, I would rather it not be somewhere else on the drive that could easily be found.
There are three primary scripts in the solution.
Locks an org away. Takes the org's saved login file out of ~/.sfdx/, uploads it into my 1Password vault, and strips the alias out of the CLI's config so the org disappears from sf org list. It also records the org in a small local "what's locked" registry.
Brings an org back. Pulls the login file back down from 1Password (this is the step that triggers the 1Password unlock prompt — password, YubiKey, or biometrics), drops it back into ~/.sfdx/, and restores the alias so the CLI can use the org again. Nothing is written until you complete the unlock, so getting to a protected org always requires that one deliberate physical act. It reminds you to re-lock when you're done.
Shows everything, locked and unlocked. Runs the normal sf org list untouched (all your usual flags still work), then adds a "Locked Orgs" section underneath listing the ones currently stashed in 1Password, each marked with a 🔒.
There's also a fourth helper, sf-lock-test <alias>, for validating your 1Password setup before you trust it against a real org. It exercises only the 1Password side of the workflow: uploads the auth file under a temporary <alias>-test title, reads it back, byte-compares the round-trip, then deletes the temp item — all without touching any sf state. Run it once during setup.
Developed on Fedora with bash. The scripts should work without changes on any modern Linux distribution, and with minor adjustments on macOS. Before they'll work you need:
- The Salesforce CLI (
sf) installed and authenticated to at least one org. - The 1Password CLI (
op) installed and signed in for your shell session. (Installing and signing into the 1Password CLI is out of scope here — see 1Password's docs.) jqinstalled (used for JSON parsing).- A dedicated 1Password vault named
sf-org-list. Create it in the 1Password app before running the scripts.
If you have biometric or hardware-key unlock configured, op will trigger it on the first command that needs vault access.
-
Install the prerequisites above and create the
sf-org-listvault in 1Password. -
Copy the scripts into a directory on your
PATH(~/.local/binis a common choice):cp sf-lock sf-unlock sf-orgs sf-lock-test ~/.local/bin/ chmod +x ~/.local/bin/sf-lock ~/.local/bin/sf-unlock ~/.local/bin/sf-orgs ~/.local/bin/sf-lock-test
Make sure that directory is on your
PATH(addexport PATH="$HOME/.local/bin:$PATH"to your shell profile if it isn't). -
Sign in to the 1Password CLI for your session.
-
Validate the plumbing against a non-sensitive org before trusting it:
sf-lock-test MyScratchOrg
A clean run ends with
round-trip OK (byte-identical)and leaves yoursfstate untouched.
Here is how this plays out in a normal week.
1. Authenticate the customer sandbox. Nothing special here, you use the standard Salesforce command:
sf org login web --alias AcmeSandboxThe org is now available to the CLI like any other.
2. Do the work, then hand it off. You complete your changes in the customer sandbox, send the client an email letting them know it is ready, and wait for their feedback. At this point the ball is in their court, and you may not hear back for days.
3. Lock the org. Since you are done for now and do not want to risk touching it while it sits idle, you lock it:
sf-lock AcmeSandboxThe credentials move into 1Password, the alias disappears from sf org list, and the org is now unreachable. No accidental deploy can land there while you wait.
4. Move on to everything else. You go do a bunch of other dev work using internal orgs, scratch orgs, other customers' sandboxes. The locked org is safely out of reach the whole time, so there is nothing to be careful about. A typo on --target-org can't hit it, because the CLI literally has no way in.
5. The client responds. They have reviewed the work and sent feedback. Time to get back in.
6. Unlock and work. You bring the org back:
sf-unlock AcmeSandboxThis is the key moment that requires a deliberate act: the 1Password unlock prompt. You complete it, the credentials land back in ~/.sfdx/, and you pick up right where you left off.
7. Finish, lock, repeat. When the round of work is done, you lock it again:
sf-lock AcmeSandboxAnd the cycle continues. The org stays locked by default and only opens for the brief windows when you are actively working in it. Each of those windows opened by a conscious 1Password unlock, never by accident.
All four scripts share a couple of constants near the top:
VAULT="sf-org-list"— the 1Password vault name. Change it in all four scripts if you use a different vault.SFDX_DIR="$HOME/.sfdx"— where thesfCLI stores auth. The default is correct for a standard install.
The local registry lives at ~/.sfdx/locked-orgs.json and is created automatically on the first lock.
This solution protects against accidents: a mistyped --target-org against a protected org, a stale target-org config left over from an earlier task, and muscle-memory or tab-completion slips. In every case the auth file is gone, so the CLI fails with an authorization error before any API call is made.
It does not protect against attackers. A compromised machine can wait for you to run sf-unlock and steal the refresh token; a compromised 1Password account can pull the document directly; and anyone who can complete the unlock can deploy. The friction is in the unlock, not in authorization.
That is the whole point: this isn't a defense against attackers, it's a single moment of deliberate intent between you and the customer org — and that moment is what catches the accident.
So that's it. Nothing overly groundbreaking here, just a handful of short bash scripts and a 1Password vault that solved a real, nagging worry for me: the fear of accidentally deploying to the wrong customer org.
If you operate across a lot of orgs and that same worry sounds familiar, maybe it'll be useful to you too. Take it, change it, and make it your own.
MIT