Compare commits
2
Commits
261c817750
...
2457a344d8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2457a344d8 | ||
|
|
5582c02edf |
@@ -119,15 +119,19 @@ order; the first one found wins:
|
||||
4. A prompt on the terminal with echo turned off.
|
||||
|
||||
If none of these is available and standard input is not a terminal, the tool
|
||||
refuses and exits with status 1. A mnemonic that fails the BIP-39 checksum is
|
||||
refused with a message saying so. Keys are derived from the mnemonic's words
|
||||
joined by single spaces, whatever whitespace is around or between them, so one
|
||||
word per line, tabs or extra spaces give the same keys.
|
||||
refuses and exits with status 1. A mnemonic that is empty, does not have 12, 15,
|
||||
18, 21 or 24 words, has a word that is not on the BIP-39 word list, or fails the
|
||||
BIP-39 checksum is refused with a message saying which. A word that is not on
|
||||
the list is named by its position, never printed. Keys are derived from the
|
||||
mnemonic's words joined by single spaces, whatever whitespace is around or
|
||||
between them, so one word per line, tabs or extra spaces give the same keys.
|
||||
|
||||
The key index, `--words`, the `--to` recipients and the file `age encrypt` or
|
||||
`age decrypt` is given to read are checked before the mnemonic is looked for, so
|
||||
a run refused for one of them never prompts for the mnemonic or runs the
|
||||
mnemonic command.
|
||||
The key index, `--words`, the `--to` recipients, the file `age encrypt` or
|
||||
`age decrypt` is given to read and its `-o` path are checked before the mnemonic
|
||||
is looked for, so a run refused for one of them never prompts for the mnemonic
|
||||
or runs the mnemonic command. An `-o` path is refused at that point if it is a
|
||||
directory or a symlink to one, if its directory is missing or is not a
|
||||
directory, or if it is a symlink that points at nothing.
|
||||
|
||||
`KEYFUNC_MNEMONIC` and `KEYFUNC_MNEMONIC_COMMAND` are removed from the
|
||||
environment before the mnemonic command, the system `ssh` (`keyfunc ssh to`) and
|
||||
|
||||
+62
-5
@@ -11,6 +11,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sneak.berlin/go/keyfunc/internal/agekey"
|
||||
@@ -108,9 +109,9 @@ func decrypt() *cobra.Command {
|
||||
}
|
||||
|
||||
// runEncrypt encrypts to the derived recipient and any others given.
|
||||
// The recipients are read and the input opened before the key is
|
||||
// derived, so that a run refused for either never asks for the
|
||||
// mnemonic.
|
||||
// The recipients are read, the -o path looked at and the input opened
|
||||
// before the key is derived, so that a run refused for any of them
|
||||
// never asks for the mnemonic.
|
||||
func runEncrypt(cmd *cobra.Command, args []string) error {
|
||||
names, err := cmd.Flags().GetStringArray("to")
|
||||
if err != nil {
|
||||
@@ -127,6 +128,11 @@ func runEncrypt(cmd *cobra.Command, args []string) error {
|
||||
return fmt.Errorf("reading the armor flag: %w", err)
|
||||
}
|
||||
|
||||
err = checkOutput(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
src, closeSrc, err := input(cmd, args)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -144,9 +150,14 @@ func runEncrypt(cmd *cobra.Command, args []string) error {
|
||||
})
|
||||
}
|
||||
|
||||
// runDecrypt decrypts with the derived identity. The input is opened
|
||||
// before the key is derived, as for runEncrypt.
|
||||
// runDecrypt decrypts with the derived identity. The -o path is looked
|
||||
// at and the input opened before the key is derived, as for runEncrypt.
|
||||
func runDecrypt(cmd *cobra.Command, args []string) error {
|
||||
err := checkOutput(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
src, closeSrc, err := input(cmd, args)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -225,6 +236,52 @@ func input(cmd *cobra.Command, args []string) (io.Reader, func(), error) {
|
||||
return file, func() { _ = file.Close() }, nil
|
||||
}
|
||||
|
||||
// checkOutput refuses an -o path that output could not write to,
|
||||
// before anything is written: a directory or a symlink to one, one
|
||||
// whose directory is missing or is not a directory, or a symlink that
|
||||
// points at nothing. It creates nothing; output looks at the path again
|
||||
// when it writes.
|
||||
func checkOutput(cmd *cobra.Command) error {
|
||||
name, err := cmd.Flags().GetString("output")
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading the output file: %w", err)
|
||||
}
|
||||
|
||||
if name == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
info, err := os.Lstat(name)
|
||||
|
||||
switch {
|
||||
case errors.Is(err, fs.ErrNotExist):
|
||||
// replace makes its new file in this directory. When the
|
||||
// directory is a file instead, os.Lstat has already failed with
|
||||
// ENOTDIR, the case below.
|
||||
directory := filepath.Dir(name)
|
||||
|
||||
_, err = os.Stat(directory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("looking at %s: %w", directory, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
case err != nil:
|
||||
return fmt.Errorf("looking at %s: %w", name, err)
|
||||
case info.Mode().Type() == fs.ModeSymlink:
|
||||
info, err = os.Stat(name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("following %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return fmt.Errorf("writing to %s: %w", name, syscall.EISDIR)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// output has the work write to the named path, going by what is there
|
||||
// without following a final symlink:
|
||||
//
|
||||
|
||||
@@ -108,6 +108,44 @@ func TestAnInputFileThatCannotBeOpenedIsRefused(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The mnemonic command fails in these runs, so the refusal of the -o
|
||||
// path shows that it was looked at before the mnemonic was looked for.
|
||||
func TestAnOutputPathThatCannotBeWrittenIsRefused(t *testing.T) {
|
||||
t.Setenv(mnemonic.CommandVariable, failingMnemonicCommand)
|
||||
|
||||
plain := written(t, "notes.txt", "the secret\n")
|
||||
directory := t.TempDir()
|
||||
missing := filepath.Join(directory, "missing")
|
||||
dangling := filepath.Join(directory, "dangling")
|
||||
require.NoError(t, os.Symlink(missing, dangling))
|
||||
|
||||
toDirectory := filepath.Join(directory, "to-directory")
|
||||
require.NoError(t, os.Symlink(t.TempDir(), toDirectory))
|
||||
|
||||
refusals := map[string]error{
|
||||
filepath.Join(missing, "notes.age"): fs.ErrNotExist,
|
||||
filepath.Join(plain, "notes.age"): syscall.ENOTDIR,
|
||||
dangling: fs.ErrNotExist,
|
||||
directory: syscall.EISDIR,
|
||||
toDirectory: syscall.EISDIR,
|
||||
}
|
||||
|
||||
for name, refusal := range refusals {
|
||||
out, err := execute(t, "age", "encrypt", "-o", name, plain)
|
||||
require.ErrorIs(t, err, refusal, name)
|
||||
require.Empty(t, out, name)
|
||||
|
||||
out, err = execute(t, "age", "decrypt", "-o", name, plain)
|
||||
require.ErrorIs(t, err, refusal, name)
|
||||
require.Empty(t, out, name)
|
||||
}
|
||||
|
||||
// Nothing was made beside the two symlinks.
|
||||
left, err := os.ReadDir(directory)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, left, 2)
|
||||
}
|
||||
|
||||
func TestAFileForAnotherKeyIsRefused(t *testing.T) {
|
||||
t.Setenv(mnemonic.Variable, example())
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/term"
|
||||
@@ -29,6 +30,28 @@ var ErrMissing = errors.New(
|
||||
"no mnemonic given and standard input is not a terminal",
|
||||
)
|
||||
|
||||
// The lengths a BIP-39 mnemonic may have.
|
||||
const (
|
||||
twelve = 12
|
||||
fifteen = 15
|
||||
eighteen = 18
|
||||
twentyOne = 21
|
||||
twentyFour = 24
|
||||
)
|
||||
|
||||
// ErrEmpty is returned for a mnemonic with no words in it, such as the
|
||||
// output of a mnemonic command that printed nothing.
|
||||
var ErrEmpty = errors.New("the mnemonic is empty")
|
||||
|
||||
// ErrWordCount is returned for a mnemonic with a number of words that
|
||||
// BIP-39 does not allow.
|
||||
var ErrWordCount = errors.New("a mnemonic has 12, 15, 18, 21 or 24 words")
|
||||
|
||||
// ErrUnknownWord is returned for a mnemonic with a word that is not on
|
||||
// the BIP-39 English word list. The error gives the word's position,
|
||||
// never the word, so the mnemonic stays out of the message.
|
||||
var ErrUnknownWord = errors.New("not on the BIP-39 word list")
|
||||
|
||||
// ErrChecksum is returned for a mnemonic that fails the BIP-39
|
||||
// checksum.
|
||||
var ErrChecksum = errors.New("the mnemonic fails its BIP-39 checksum")
|
||||
@@ -126,13 +149,34 @@ func ask() (string, error) {
|
||||
|
||||
// checked joins the words with single spaces, whatever whitespace
|
||||
// separated them, since the seed is computed over the string itself,
|
||||
// and refuses a mnemonic that does not pass the BIP-39 checksum.
|
||||
func checked(words string) (string, error) {
|
||||
words = strings.Join(strings.Fields(words), " ")
|
||||
// and refuses a mnemonic that is not a valid BIP-39 mnemonic, with an
|
||||
// error that says what is wrong with it.
|
||||
func checked(text string) (string, error) {
|
||||
words := strings.Fields(text)
|
||||
|
||||
if !bip39.IsMnemonicValid(words) {
|
||||
switch len(words) {
|
||||
case 0:
|
||||
return "", ErrEmpty
|
||||
case twelve, fifteen, eighteen, twentyOne, twentyFour:
|
||||
default:
|
||||
return "", fmt.Errorf("%w, not %d", ErrWordCount, len(words))
|
||||
}
|
||||
|
||||
wordList := bip39.English()
|
||||
|
||||
for i, word := range words {
|
||||
if !slices.Contains(wordList, word) {
|
||||
return "", fmt.Errorf(
|
||||
"word %d of the mnemonic is %w", i+1, ErrUnknownWord,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
joined := strings.Join(words, " ")
|
||||
|
||||
if !bip39.IsMnemonicValid(joined) {
|
||||
return "", ErrChecksum
|
||||
}
|
||||
|
||||
return words, nil
|
||||
return joined, nil
|
||||
}
|
||||
|
||||
@@ -97,6 +97,27 @@ func TestAFailingCommandSaysWhatWentWrong(t *testing.T) {
|
||||
require.Contains(t, err.Error(), "nothing here")
|
||||
}
|
||||
|
||||
func TestValidMnemonicsOf15To24WordsAreAccepted(t *testing.T) {
|
||||
t.Setenv(mnemonic.CommandVariable, "")
|
||||
|
||||
// abandon repeated, then the last word that makes the checksum
|
||||
// match: 15, 18, 21 and 24 words.
|
||||
valid := []string{
|
||||
strings.Repeat("abandon ", 14) + "address",
|
||||
strings.Repeat("abandon ", 17) + "agent",
|
||||
strings.Repeat("abandon ", 20) + "admit",
|
||||
strings.Repeat("abandon ", 23) + "art",
|
||||
}
|
||||
|
||||
for _, words := range valid {
|
||||
t.Setenv(mnemonic.Variable, words)
|
||||
|
||||
got, err := mnemonic.Read(t.Context(), "")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, words, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestABadChecksumIsRefused(t *testing.T) {
|
||||
t.Setenv(mnemonic.CommandVariable, "")
|
||||
t.Setenv(mnemonic.Variable, broken())
|
||||
@@ -105,6 +126,38 @@ func TestABadChecksumIsRefused(t *testing.T) {
|
||||
require.ErrorIs(t, err, mnemonic.ErrChecksum)
|
||||
}
|
||||
|
||||
func TestACommandThatPrintsNothingGivesAnEmptyMnemonic(t *testing.T) {
|
||||
t.Setenv(mnemonic.CommandVariable, "")
|
||||
t.Setenv(mnemonic.Variable, "")
|
||||
|
||||
_, err := mnemonic.Read(t.Context(), "true")
|
||||
require.ErrorIs(t, err, mnemonic.ErrEmpty)
|
||||
require.NotContains(t, err.Error(), "checksum")
|
||||
}
|
||||
|
||||
func TestAWrongWordCountIsRefused(t *testing.T) {
|
||||
t.Setenv(mnemonic.CommandVariable, "")
|
||||
t.Setenv(mnemonic.Variable, strings.Repeat("abandon ", 3))
|
||||
|
||||
_, err := mnemonic.Read(t.Context(), "")
|
||||
require.ErrorIs(t, err, mnemonic.ErrWordCount)
|
||||
require.ErrorContains(t, err, "not 3")
|
||||
require.NotContains(t, err.Error(), "checksum")
|
||||
}
|
||||
|
||||
func TestAWordNotOnTheListIsNamedByPositionOnly(t *testing.T) {
|
||||
const typo = "abuot"
|
||||
|
||||
t.Setenv(mnemonic.CommandVariable, "")
|
||||
t.Setenv(mnemonic.Variable, strings.Repeat("abandon ", 11)+typo)
|
||||
|
||||
_, err := mnemonic.Read(t.Context(), "")
|
||||
require.ErrorIs(t, err, mnemonic.ErrUnknownWord)
|
||||
require.ErrorContains(t, err, "word 12 ")
|
||||
require.NotContains(t, err.Error(), typo)
|
||||
require.NotContains(t, err.Error(), "checksum")
|
||||
}
|
||||
|
||||
func TestNothingToReadAndNobodyToAsk(t *testing.T) {
|
||||
t.Setenv(mnemonic.CommandVariable, "")
|
||||
t.Setenv(mnemonic.Variable, "")
|
||||
|
||||
Reference in New Issue
Block a user