Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It speaks the same format as the web app, so links work in both directions: a li
```bash
secretli share # type or paste the secret, then Ctrl-D
pbpaste | secretli share -e 1h # text from a pipe, gone after an hour
secretli share --paste -c # the clipboard's text in, the link back on the clipboard
secretli share --from-clipboard -c # the clipboard's text in, the link back on the clipboard
secretli share deploy.key notes.pdf # files; -p adds a password, --reusable lets it open more than once
secretli open <link> # text to stdout, files to the current directory; -c copies text instead
secretli status <link> # what a link points to, without opening it
Expand Down Expand Up @@ -58,7 +58,7 @@ For shell completion without Homebrew, `secretli completion zsh --help` (or `bas

## Use

**Sharing.** Files are arguments; text comes from a pipe, from a prompt when you are at a terminal, from the clipboard with `--paste`, or from `-t`. The prompt, the pipe and the clipboard keep the secret out of your shell history; `-t` does not. `--paste` leaves off the clipboard's final line break, as `open --copy` does, and with `--name` shares the clipboard as a file; it works with the same clipboard tools as `--copy`. `-` shares stdin as a file named by `--name`:
**Sharing.** Files are arguments; text comes from a pipe, from a prompt when you are at a terminal, from the clipboard with `--from-clipboard`, or from `-t`. The prompt, the pipe and the clipboard keep the secret out of your shell history; `-t` does not. `--from-clipboard` leaves off the clipboard's final line break, as `open --to-clipboard` does, and with `--name` shares the clipboard as a file; it works with the same clipboard tools as `--to-clipboard`. `-` shares stdin as a file named by `--name`:

```bash
pg_dump db | gzip | secretli share --name db.sql.gz
Expand All @@ -68,15 +68,15 @@ Links open once and expire after a day unless you say otherwise: `-e` takes `5m`

Every share prints two links: the one to hand out, and the owner link, which can delete the secret and, for a reusable one, tells you whether it has been opened. Keep the owner link to yourself.

**Opening.** Text goes to stdout exactly as it was shared. At a terminal, control characters in it, which could steer the terminal, show as `�`, and the command says so; a pipe, a file or `--copy` gets the exact text. File names and error messages from the server reach the terminal without control characters or direction overrides either. Files are saved to the current directory or to `--out`, and nothing is overwritten unless you pass `--force`; `--stdout` streams a single file instead. When a file of that name exists, a reusable secret is refused before anything is saved, and you can open it again elsewhere; a one-time secret, used up by then, saves that file as `name (2).ext`, or the next number free, and says so. A file's name is made safe first: no path, no control characters, no names Windows reserves for devices, at most 255 bytes. Two files of one secret with the same name are both saved, the second as `name (2).ext`, and the command says so. `--force` replaces a file that exists with a new one, so a link in its place is replaced, never followed. Before anything is opened, the command says what the link points to. Opening a one-time secret uses it up, so for one of those it asks first, and Enter means no; `-y`/`--yes` opens it without asking. Reusable secrets open right away. Without an argument the link is read from stdin, or asked for without showing what you paste, which keeps it out of your shell history and the scrollback too.
**Opening.** Text goes to stdout exactly as it was shared. At a terminal, control characters in it, which could steer the terminal, show as `�`, and the command says so; a pipe, a file or `--to-clipboard` gets the exact text. File names and error messages from the server reach the terminal without control characters or direction overrides either. Files are saved to the current directory or to `--out`, and nothing is overwritten unless you pass `--force`; `--stdout` streams a single file instead. When a file of that name exists, a reusable secret is refused before anything is saved, and you can open it again elsewhere; a one-time secret, used up by then, saves that file as `name (2).ext`, or the next number free, and says so. A file's name is made safe first: no path, no control characters, no names Windows reserves for devices, at most 255 bytes. Two files of one secret with the same name are both saved, the second as `name (2).ext`, and the command says so. `--force` replaces a file that exists with a new one, so a link in its place is replaced, never followed. Before anything is opened, the command says what the link points to. Opening a one-time secret uses it up, so for one of those it asks first, and Enter means no; `-y`/`--yes` opens it without asking. Reusable secrets open right away. Without an argument the link is read from stdin, or asked for without showing what you paste, which keeps it out of your shell history and the scrollback too.

When a secret holds several files, `open` lists them with their sizes and asks which to save. Enter saves them all; numbers, ranges and names or patterns pick some, as in `2`, `1 3`, `1-2` or `*.jpg`, and a list of more than 30 files shows the first 30, while numbers and patterns reach them all. Of a one-time secret, the files you don't save are gone with it, and it says so before it asks. A secret over 1 MiB is downloaded after the question, which the server allows for 15 minutes after it is opened, so for one of those it also says the time by which the files have to be saved. Only the files you pick count when names are checked against the directory. `--stdout` asks for the one file to write. With `--yes` or `--json`, or where there is no terminal, nothing is asked: every file is saved, and `--stdout` needs a secret of a single file.

`-c`/`--copy` puts text on the clipboard instead of the terminal, so it doesn't stay in the scrollback. The final line break is left off, so pasting a password doesn't also press Enter. 45 seconds later the clipboard is cleared, unless you copied something else by then. Before anything is opened, `--copy` checks that there is a clipboard tool (`pbcopy`, `wl-copy`, `xclip`, `xsel` or `clip.exe`) and that the secret is text; should copying still fail, the text is printed after all, so a one-time secret isn't lost.
`-c`/`--to-clipboard` puts text on the clipboard instead of the terminal, so it doesn't stay in the scrollback. The final line break is left off, so pasting a password doesn't also press Enter. 45 seconds later the clipboard is cleared, unless you copied something else by then. Before anything is opened, `--to-clipboard` checks that there is a clipboard tool (`pbcopy`, `wl-copy`, `xclip`, `xsel` or `clip.exe`) and that the secret is text; should copying still fail, the text is printed after all, so a one-time secret isn't lost.

**Handing a link over with a code.** Instead of copying a link to another device, `send` hands it over with a short code like `7-acid-rocket`. On the other device, type the code at the server's `/c` page (`secretli.app/c`) or run `secretli receive 7-acid-rocket`; `secretli share --code` makes a secret and hands it over in one go. The link travels encrypted, and only after the other device has proved it typed the same code; the words never leave the two devices. A code works once and for ten minutes, and a wrong code ends the transfer on both sides. Given an owner link, `send` hands over only the link to hand out. It works both ways with the web app's "Send with a code".

`receive` opens what it receives like `open` does, with the same `--out`, `--stdout`, `--force`, `--yes`, `--copy` and password options, or prints the link with `--link` (with `--copy`, it copies the link). If you decline to open a one-time secret, it prints the link instead, since the code is used up by then. Typing is forgiving: `7 acid rocket`, `7-ACID-ROCKET` and `7-aci-roc` all work. Without an argument the code is asked for, and what you type isn't shown; a misspelt word is caught before the code is used.
`receive` opens what it receives like `open` does, with the same `--out`, `--stdout`, `--force`, `--yes`, `--to-clipboard` and password options, or prints the link with `--link` (with `--to-clipboard`, it copies the link). If you decline to open a one-time secret, it prints the link instead, since the code is used up by then. Typing is forgiving: `7 acid rocket`, `7-ACID-ROCKET` and `7-aci-roc` all work. Without an argument the code is asked for, and what you type isn't shown; a misspelt word is caught before the code is used.

**Checking and deleting.** `status` describes a secret from its link without opening it: text or files, one-time or reusable, password or not, when it was sent and when it expires, and whether it has been opened. Once a secret is gone, opened, deleted or expired, the server keeps nothing about it, so all `status` can say then is that it is gone, to the owner as to a recipient, as it does for a link to nothing. The exit code is 4 then, and with `--json` the state is `live` or `gone`. `delete` takes the owner link and asks before it deletes, unless you pass `--yes`.

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const clipboardClearAfter = 45 * time.Second
// clipboardTool is the desktop's clipboard.
type clipboardTool interface {
// Available fails when there is no clipboard to copy to, so that
// --copy is refused before anything is opened.
// --to-clipboard is refused before anything is opened.
Available() error
Copy(text string) error
Paste() (string, error)
Expand Down
42 changes: 21 additions & 21 deletions internal/cli/clipboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestOpenCopiesTheSecretInsteadOfPrintingIt(t *testing.T) {
cb := fakeClipboard(t)
link, _ := shareText(t, "--server="+srv.URL, "hunter2\n")

stdout, stderr, code := runCLI(t, "", "open", link, "--copy", "--yes")
stdout, stderr, code := runCLI(t, "", "open", link, "--to-clipboard", "--yes")
if code != 0 || stdout != "" {
t.Fatalf("exit %d, stdout %q, stderr %q", code, stdout, stderr)
}
Expand All @@ -112,12 +112,12 @@ func TestOpenCopiesTheSecretInsteadOfPrintingIt(t *testing.T) {
}
}

// The link carries the key, so share --copy has it cleared like open --copy.
// The link carries the key, so share --to-clipboard has it cleared like open --to-clipboard.
func TestShareCopiesTheLinkAndClearsItLater(t *testing.T) {
srv := fakeServer(t)
cb := fakeClipboard(t)

stdout, stderr, code := runCLI(t, "the launch code", "share", "--server="+srv.URL, "--copy")
stdout, stderr, code := runCLI(t, "the launch code", "share", "--server="+srv.URL, "--to-clipboard")
if code != 0 {
t.Fatalf("exit %d, stderr %q", code, stderr)
}
Expand All @@ -134,16 +134,16 @@ func TestShareCopiesTheLinkAndClearsItLater(t *testing.T) {
}
}

// --paste shares the clipboard's text, and with --copy the link takes its
// place there: what open --copy gives back is what was pasted.
// --from-clipboard shares the clipboard's text, and with --to-clipboard the link takes its
// place there: what open --to-clipboard gives back is what was pasted.
func TestSharePastesTheClipboard(t *testing.T) {
srv := fakeServer(t)
server := "--server=" + srv.URL
cb := fakeClipboard(t)

// The paste tool's own line break is not part of the secret.
cb.text, cb.pasteSuffix = "hunter2", "\r\n"
stdout, stderr, code := runCLI(t, "ignored", "share", server, "--paste", "--copy")
stdout, stderr, code := runCLI(t, "ignored", "share", server, "--from-clipboard", "--to-clipboard")
if code != 0 {
t.Fatalf("exit %d, stderr %q", code, stderr)
}
Expand All @@ -158,7 +158,7 @@ func TestSharePastesTheClipboard(t *testing.T) {

// With --name, the clipboard is shared as a file.
cb.text = "A=1\nB=2\n"
link, _ = shareText(t, server, "", "--paste", "--name", ".env")
link, _ = shareText(t, server, "", "--from-clipboard", "--name", ".env")
dir := t.TempDir()
if _, stderr, code := runCLI(t, "", "open", link, "--yes", "--out", dir); code != 0 {
t.Fatalf("open file: exit %d, stderr %q", code, stderr)
Expand All @@ -179,18 +179,18 @@ func TestPasteIsRefusedBeforeAnythingIsUploaded(t *testing.T) {

cb.text = "x"
for _, args := range [][]string{{"-t", "y"}, {file}, {"-"}} {
if _, stderr, code := runCLI(t, "", append([]string{"share", server, "--paste"}, args...)...); code != ExitError || !strings.Contains(stderr, "--paste shares the clipboard; leave out --text and files") {
if _, stderr, code := runCLI(t, "", append([]string{"share", server, "--from-clipboard"}, args...)...); code != ExitError || !strings.Contains(stderr, "--from-clipboard shares the clipboard; leave out --text and files") {
t.Errorf("%v: exit %d, stderr %q", args, code, stderr)
}
}

cb.text = "\n"
if _, stderr, code := runCLI(t, "", "share", server, "--paste"); code != ExitError || !strings.Contains(stderr, "--paste: the clipboard holds no text") {
if _, stderr, code := runCLI(t, "", "share", server, "--from-clipboard"); code != ExitError || !strings.Contains(stderr, "--from-clipboard: the clipboard holds no text") {
t.Errorf("empty clipboard: exit %d, stderr %q", code, stderr)
}

clipboard = noClipboard{}
if _, stderr, code := runCLI(t, "", "share", server, "--paste"); code != ExitError || !strings.Contains(stderr, "--paste: no clipboard tool found") {
if _, stderr, code := runCLI(t, "", "share", server, "--from-clipboard"); code != ExitError || !strings.Contains(stderr, "--from-clipboard: no clipboard tool found") {
t.Errorf("no clipboard: exit %d, stderr %q", code, stderr)
}
}
Expand All @@ -205,19 +205,19 @@ func TestCopyIsRefusedBeforeAnythingIsUsedUp(t *testing.T) {
args []string
want string
}{
{[]string{"--stdout"}, "--stdout writes a file and --copy copies text"},
{[]string{"--json"}, "--json prints the secret; leave out --copy"},
{[]string{"--stdout"}, "--stdout writes a file and --to-clipboard copies text"},
{[]string{"--json"}, "--json prints the secret; leave out --to-clipboard"},
} {
// With --json, the error is JSON on stdout.
stdout, stderr, code := runCLI(t, "", append([]string{"open", link, "--copy", "--yes"}, tc.args...)...)
stdout, stderr, code := runCLI(t, "", append([]string{"open", link, "--to-clipboard", "--yes"}, tc.args...)...)
if code != ExitError || !strings.Contains(stdout+stderr, tc.want) {
t.Errorf("%v: exit %d, stdout %q, stderr %q", tc.args, code, stdout, stderr)
}
}

// No clipboard tool: refused before the link is even read.
clipboard = noClipboard{}
if _, stderr, code := runCLI(t, "", "open", link, "--copy", "--yes"); code != ExitError || !strings.Contains(stderr, "--copy: no clipboard tool found") {
if _, stderr, code := runCLI(t, "", "open", link, "--to-clipboard", "--yes"); code != ExitError || !strings.Contains(stderr, "--to-clipboard: no clipboard tool found") {
t.Errorf("no clipboard: exit %d, stderr %q", code, stderr)
}
clipboard = cb
Expand All @@ -229,7 +229,7 @@ func TestCopyIsRefusedBeforeAnythingIsUsedUp(t *testing.T) {
t.Fatal(err)
}
fileLink, _ := shareText(t, server, "", notes)
if _, stderr, code := runCLI(t, "", "open", fileLink, "--copy", "--yes"); code != ExitError || !strings.Contains(stderr, "--copy is for text, and this secret holds files") {
if _, stderr, code := runCLI(t, "", "open", fileLink, "--to-clipboard", "--yes"); code != ExitError || !strings.Contains(stderr, "--to-clipboard is for text, and this secret holds files") {
t.Errorf("files: exit %d, stderr %q", code, stderr)
}

Expand All @@ -249,7 +249,7 @@ func TestACopyThatFailsPrintsTheSecretAfterAll(t *testing.T) {
link, _ := shareText(t, "--server="+srv.URL, "hunter2\n")

// The one-time secret is used up by then, so this is the only copy.
stdout, stderr, code := runCLI(t, "", "open", link, "--copy", "--yes")
stdout, stderr, code := runCLI(t, "", "open", link, "--to-clipboard", "--yes")
if code != 0 || stdout != "hunter2\n" || !strings.Contains(stderr, "Couldn't copy to the clipboard (xclip: exit status 1), so here it is:") {
t.Errorf("exit %d, stdout %q, stderr %q", code, stdout, stderr)
}
Expand All @@ -272,7 +272,7 @@ func TestReceiveCopies(t *testing.T) {
}

link, _ := shareText(t, server, "handed over\n")
if stdout, stderr, code := receive(link, "--copy", "--yes"); code != 0 || stdout != "" {
if stdout, stderr, code := receive(link, "--to-clipboard", "--yes"); code != 0 || stdout != "" {
t.Errorf("text: exit %d, stdout %q, stderr %q", code, stdout, stderr)
}
if text, _ := cb.state(); text != "handed over" {
Expand All @@ -281,7 +281,7 @@ func TestReceiveCopies(t *testing.T) {

// With --link, the link is what goes on the clipboard.
link, _ = shareText(t, server, "a link\n")
if stdout, stderr, code := receive(link, "--copy", "--link"); code != 0 || stdout != "" || !strings.Contains(stderr, "Copied the link") {
if stdout, stderr, code := receive(link, "--to-clipboard", "--link"); code != 0 || stdout != "" || !strings.Contains(stderr, "Copied the link") {
t.Errorf("link: exit %d, stdout %q, stderr %q", code, stdout, stderr)
}
if text, _ := cb.state(); text != link {
Expand All @@ -296,8 +296,8 @@ func TestReceiveCopies(t *testing.T) {
t.Fatal(err)
}
fileLink, _ := shareText(t, server, "", notes)
stdout, stderr, code := receive(fileLink, "--copy", "--yes")
if code != ExitError || strings.TrimSpace(stdout) != fileLink || !strings.Contains(stderr, "Not opened: --copy is for text") || !strings.Contains(stderr, "Here is the link, which still works") {
stdout, stderr, code := receive(fileLink, "--to-clipboard", "--yes")
if code != ExitError || strings.TrimSpace(stdout) != fileLink || !strings.Contains(stderr, "Not opened: --to-clipboard is for text") || !strings.Contains(stderr, "Here is the link, which still works") {
t.Errorf("files: exit %d, stdout %q, stderr %q", code, stdout, stderr)
}
if _, stderr, code := runCLI(t, "", "open", fileLink, "--yes", "--out", filepath.Join(dir, "received")); code != 0 {
Expand Down Expand Up @@ -372,7 +372,7 @@ func TestTabCompletion(t *testing.T) {
}
}

// The command behind --copy's clearing stays out of sight.
// The command behind --to-clipboard's clearing stays out of sight.
if stdout, _, _ := runCLI(t, "", "--help"); strings.Contains(stdout, "clear-clipboard") {
t.Errorf("help lists clear-clipboard: %q", stdout)
}
Expand Down
Loading