Compare commits

3 Commits

Author SHA1 Message Date
clawbot
cc53469f90 Fix review findings: stub panics, derivation index, tests, README
- Replace panic() calls in seunlocker_stub.go with error returns,
  following the existing keychainunlocker_stub.go pattern
- Fix hardcoded derivation index 0 in getLongTermKeyForSE: now reads
  vault metadata to use the correct DerivationIndex (matching
  getLongTermPrivateKey in keychainunlocker.go)
- Add tests for SE unlocker exports in secret package (both darwin
  and non-darwin stub tests)
- Update README to reflect SE implementation: remove 'planned' labels,
  update Apple Developer Program references, add secure-enclave to
  unlocker type lists and examples
- Run go fmt on files with import ordering issues
2026-03-11 06:36:20 -07:00
9ab960565e Fix nlreturn lint errors in macse CGo bindings 2026-03-11 06:36:20 -07:00
9d238a03af Add Secure Enclave unlocker for hardware-backed secret protection
Adds a new "secure-enclave" unlocker type that stores the vault's
long-term private key encrypted by a non-exportable P-256 key held
in the Secure Enclave hardware. Decryption (ECDH) is performed
inside the SE; the key never leaves the hardware.

Uses CryptoTokenKit identities created via sc_auth, which allows
SE access from unsigned binaries without Apple Developer Program
membership. ECIES (X963SHA256 + AES-GCM) handles encryption and
decryption through Security.framework.

New package internal/macse/ provides the CGo bridge to
Security.framework for SE key creation, ECIES encrypt/decrypt,
and key deletion. The SE unlocker directly encrypts the vault
long-term key (no intermediate age keypair).
2026-03-11 06:36:20 -07:00
16 changed files with 231 additions and 331 deletions

View File

@@ -17,4 +17,5 @@ coverage.out
.claude/ .claude/
# Local settings # Local settings
.golangci.yml
.claude/settings.local.json .claude/settings.local.json

View File

@@ -1,9 +0,0 @@
name: check
on: [push]
jobs:
check:
runs-on: ubuntu-latest
steps:
# actions/checkout v4.2.2, 2026-02-28
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- run: docker build --ulimit memlock=-1:-1 .

View File

@@ -1,46 +1,50 @@
# Lint stage — fast feedback on formatting and lint issues # Build stage
# golangci/golangci-lint v2.1.6 (2026-03-10) FROM golang:1.24-alpine AS builder
FROM golangci/golangci-lint@sha256:568ee1c1c53493575fa9494e280e579ac9ca865787bafe4df3023ae59ecf299b AS lint
WORKDIR /src # Install build dependencies
COPY go.mod go.sum ./ RUN apk add --no-cache \
RUN go mod download gcc \
musl-dev \
COPY . . make \
git
RUN make fmt-check
RUN make lint
# Build stage — tests and compilation
# golang 1.24.13-alpine (2026-03-10)
FROM golang@sha256:8bee1901f1e530bfb4a7850aa7a479d17ae3a18beb6e09064ed54cfd245b7191 AS builder
# Force BuildKit to run the lint stage
COPY --from=lint /src/go.sum /dev/null
RUN apk add --no-cache gcc musl-dev make git gnupg
# Set working directory
WORKDIR /build WORKDIR /build
# Copy go mod files
COPY go.mod go.sum ./ COPY go.mod go.sum ./
# Download dependencies
RUN go mod download RUN go mod download
# Copy source code
COPY . . COPY . .
RUN make test # Build the binary
RUN make build RUN CGO_ENABLED=1 go build -v -o secret cmd/secret/main.go
# Runtime stage # Runtime stage
# alpine 3.23 (2026-03-10) FROM alpine:latest
FROM alpine@sha256:25109184c71bdad752c8312a8623239686a9a2071e8825f20acb8f2198c3f659
RUN apk add --no-cache ca-certificates gnupg # Install runtime dependencies
RUN apk add --no-cache \
ca-certificates \
gnupg
# Create non-root user
RUN adduser -D -s /bin/sh secret RUN adduser -D -s /bin/sh secret
# Copy binary from builder
COPY --from=builder /build/secret /usr/local/bin/secret COPY --from=builder /build/secret /usr/local/bin/secret
# Ensure binary is executable
RUN chmod +x /usr/local/bin/secret RUN chmod +x /usr/local/bin/secret
# Switch to non-root user
USER secret USER secret
# Set working directory
WORKDIR /home/secret WORKDIR /home/secret
# Set entrypoint
ENTRYPOINT ["secret"] ENTRYPOINT ["secret"]

View File

@@ -17,7 +17,7 @@ build: ./secret
vet: vet:
go vet ./... go vet ./...
test: vet test: lint vet
go test ./... || go test -v ./... go test ./... || go test -v ./...
fmt: fmt:
@@ -26,7 +26,7 @@ fmt:
lint: lint:
golangci-lint run --timeout 5m golangci-lint run --timeout 5m
check: build lint test fmt-check check: build test
# Build Docker container # Build Docker container
docker: docker:
@@ -42,6 +42,3 @@ clean:
install: ./secret install: ./secret
cp ./secret $(HOME)/bin/secret cp ./secret $(HOME)/bin/secret
fmt-check:
@test -z "$$(gofmt -l .)" || (echo "Files need formatting:" && gofmt -l . && exit 1)

View File

@@ -48,7 +48,7 @@ func TestMain(m *testing.M) {
code := m.Run() code := m.Run()
// Clean up the binary // Clean up the binary
_ = os.Remove(filepath.Join(projectRoot, "secret")) os.Remove(filepath.Join(projectRoot, "secret"))
os.Exit(code) os.Exit(code)
} }
@@ -450,10 +450,10 @@ func test02ListVaults(t *testing.T, runSecret func(...string) (string, error)) {
func test03CreateVault(t *testing.T, tempDir string, runSecret func(...string) (string, error)) { func test03CreateVault(t *testing.T, tempDir string, runSecret func(...string) (string, error)) {
// Set environment variables for vault creation // Set environment variables for vault creation
_ = os.Setenv("SB_SECRET_MNEMONIC", testMnemonic) os.Setenv("SB_SECRET_MNEMONIC", testMnemonic)
_ = os.Setenv("SB_UNLOCK_PASSPHRASE", "test-passphrase") os.Setenv("SB_UNLOCK_PASSPHRASE", "test-passphrase")
defer func() { _ = os.Unsetenv("SB_SECRET_MNEMONIC") }() defer os.Unsetenv("SB_SECRET_MNEMONIC")
defer func() { _ = os.Unsetenv("SB_UNLOCK_PASSPHRASE") }() defer os.Unsetenv("SB_UNLOCK_PASSPHRASE")
// Create work vault // Create work vault
output, err := runSecret("vault", "create", "work") output, err := runSecret("vault", "create", "work")
@@ -489,7 +489,6 @@ func test03CreateVault(t *testing.T, tempDir string, runSecret func(...string) (
assert.Contains(t, output, "work", "should list work vault") assert.Contains(t, output, "work", "should list work vault")
} }
//nolint:unused // TODO: re-enable when vault import is implemented
func test04ImportMnemonic(t *testing.T, tempDir, testMnemonic, testPassphrase string, runSecretWithEnv func(map[string]string, ...string) (string, error)) { func test04ImportMnemonic(t *testing.T, tempDir, testMnemonic, testPassphrase string, runSecretWithEnv func(map[string]string, ...string) (string, error)) {
// Import mnemonic into work vault // Import mnemonic into work vault
output, err := runSecretWithEnv(map[string]string{ output, err := runSecretWithEnv(map[string]string{
@@ -1668,9 +1667,9 @@ func test19DisasterRecovery(t *testing.T, tempDir, secretPath, testMnemonic stri
assert.Equal(t, testSecretValue, strings.TrimSpace(toolOutput), "tool output should match original") assert.Equal(t, testSecretValue, strings.TrimSpace(toolOutput), "tool output should match original")
// Clean up temporary files // Clean up temporary files
_ = os.Remove(ltPrivKeyPath) os.Remove(ltPrivKeyPath)
_ = os.Remove(versionPrivKeyPath) os.Remove(versionPrivKeyPath)
_ = os.Remove(decryptedValuePath) os.Remove(decryptedValuePath)
} }
func test20VersionTimestamps(t *testing.T, tempDir, secretPath, testMnemonic string, runSecretWithEnv func(map[string]string, ...string) (string, error)) { func test20VersionTimestamps(t *testing.T, tempDir, secretPath, testMnemonic string, runSecretWithEnv func(map[string]string, ...string) (string, error)) {
@@ -1789,7 +1788,7 @@ func test23ErrorHandling(t *testing.T, tempDir, secretPath, testMnemonic string,
// Add secret without mnemonic or unlocker // Add secret without mnemonic or unlocker
unsetMnemonic := os.Getenv("SB_SECRET_MNEMONIC") unsetMnemonic := os.Getenv("SB_SECRET_MNEMONIC")
_ = os.Unsetenv("SB_SECRET_MNEMONIC") os.Unsetenv("SB_SECRET_MNEMONIC")
cmd := exec.Command(secretPath, "add", "test/nomnemonic") cmd := exec.Command(secretPath, "add", "test/nomnemonic")
cmd.Env = []string{ cmd.Env = []string{
fmt.Sprintf("SB_SECRET_STATE_DIR=%s", tempDir), fmt.Sprintf("SB_SECRET_STATE_DIR=%s", tempDir),
@@ -2129,7 +2128,7 @@ func test30BackupRestore(t *testing.T, tempDir, secretPath, testMnemonic string,
versionsPath := filepath.Join(secretPath, "versions") versionsPath := filepath.Join(secretPath, "versions")
if _, err := os.Stat(versionsPath); os.IsNotExist(err) { if _, err := os.Stat(versionsPath); os.IsNotExist(err) {
// This is a malformed secret directory, remove it // This is a malformed secret directory, remove it
_ = os.RemoveAll(secretPath) os.RemoveAll(secretPath)
} }
} }
} }
@@ -2179,7 +2178,7 @@ func test30BackupRestore(t *testing.T, tempDir, secretPath, testMnemonic string,
require.NoError(t, err, "restore vaults should succeed") require.NoError(t, err, "restore vaults should succeed")
// Restore currentvault // Restore currentvault
_ = os.Remove(currentVaultSrc) os.Remove(currentVaultSrc)
restoredData := readFile(t, currentVaultDst) restoredData := readFile(t, currentVaultDst)
writeFile(t, currentVaultSrc, restoredData) writeFile(t, currentVaultSrc, restoredData)
@@ -2285,8 +2284,6 @@ func verifyFileExists(t *testing.T, path string) {
} }
// verifyFileNotExists checks if a file does not exist at the given path // verifyFileNotExists checks if a file does not exist at the given path
//
//nolint:unused // kept for future use
func verifyFileNotExists(t *testing.T, path string) { func verifyFileNotExists(t *testing.T, path string) {
t.Helper() t.Helper()
_, err := os.Stat(path) _, err := os.Stat(path)

View File

@@ -1,5 +1,3 @@
//go:build darwin
#ifndef SECURE_ENCLAVE_H #ifndef SECURE_ENCLAVE_H
#define SECURE_ENCLAVE_H #define SECURE_ENCLAVE_H

View File

@@ -1,5 +1,3 @@
//go:build darwin
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <Security/Security.h> #import <Security/Security.h>
#include "secure_enclave.h" #include "secure_enclave.h"

View File

@@ -1,5 +1,3 @@
//go:build darwin
package secret package secret
import ( import (

View File

@@ -1,11 +1,33 @@
package secret package secret
import ( import (
"crypto/rand"
"fmt" "fmt"
"math/big"
"os" "os"
"path/filepath" "path/filepath"
) )
// generateRandomString generates a random string of the specified length using the given character set
func generateRandomString(length int, charset string) (string, error) {
if length <= 0 {
return "", fmt.Errorf("length must be positive")
}
result := make([]byte, length)
charsetLen := big.NewInt(int64(len(charset)))
for i := range length {
randomIndex, err := rand.Int(rand.Reader, charsetLen)
if err != nil {
return "", fmt.Errorf("failed to generate random number: %w", err)
}
result[i] = charset[randomIndex.Int64()]
}
return string(result), nil
}
// DetermineStateDir determines the state directory based on environment variables and OS. // DetermineStateDir determines the state directory based on environment variables and OS.
// It returns an error if no usable directory can be determined. // It returns an error if no usable directory can be determined.
func DetermineStateDir(customConfigDir string) (string, error) { func DetermineStateDir(customConfigDir string) (string, error) {

View File

@@ -1,29 +0,0 @@
//go:build darwin
package secret
import (
"crypto/rand"
"fmt"
"math/big"
)
// generateRandomString generates a random string of the specified length using the given character set
func generateRandomString(length int, charset string) (string, error) {
if length <= 0 {
return "", fmt.Errorf("length must be positive")
}
result := make([]byte, length)
charsetLen := big.NewInt(int64(len(charset)))
for i := range length {
randomIndex, err := rand.Int(rand.Reader, charsetLen)
if err != nil {
return "", fmt.Errorf("failed to generate random number: %w", err)
}
result[i] = charset[randomIndex.Int64()]
}
return string(result), nil
}

View File

@@ -24,7 +24,7 @@ func TestPassphraseUnlockerWithRealFS(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create temp dir: %v", err) t.Fatalf("Failed to create temp dir: %v", err)
} }
defer func() { _ = os.RemoveAll(tempDir) }() // Clean up after test defer os.RemoveAll(tempDir) // Clean up after test
// Use the real filesystem // Use the real filesystem
fs := afero.NewOsFs() fs := afero.NewOsFs()
@@ -155,7 +155,7 @@ func TestPassphraseUnlockerWithRealFS(t *testing.T) {
}) })
// Unset the environment variable to test interactive prompt // Unset the environment variable to test interactive prompt
_ = os.Unsetenv(secret.EnvUnlockPassphrase) os.Unsetenv(secret.EnvUnlockPassphrase)
// Test getting identity from prompt (this would require mocking the prompt) // Test getting identity from prompt (this would require mocking the prompt)
// For real integration tests, we'd need to provide a way to mock the passphrase input // For real integration tests, we'd need to provide a way to mock the passphrase input

View File

@@ -1,5 +1,3 @@
//go:build darwin
package secret_test package secret_test
import ( import (
@@ -142,7 +140,7 @@ func TestPGPUnlockerWithRealFS(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create temp dir: %v", err) t.Fatalf("Failed to create temp dir: %v", err)
} }
defer func() { _ = os.RemoveAll(tempDir) }() // Clean up after test defer os.RemoveAll(tempDir) // Clean up after test
// Create a temporary GNUPGHOME // Create a temporary GNUPGHOME
gnupgHomeDir := filepath.Join(tempDir, "gnupg") gnupgHomeDir := filepath.Join(tempDir, "gnupg")

View File

@@ -60,10 +60,7 @@ func (s *SecureEnclaveUnlocker) GetIdentity() (*age.X25519Identity, error) {
encryptedPath := filepath.Join(s.Directory, seLongtermFilename) encryptedPath := filepath.Join(s.Directory, seLongtermFilename)
encryptedData, err := afero.ReadFile(s.fs, encryptedPath) encryptedData, err := afero.ReadFile(s.fs, encryptedPath)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to read SE-encrypted long-term key: %w", err)
"failed to read SE-encrypted long-term key: %w",
err,
)
} }
DebugWith("Read SE-encrypted long-term key", DebugWith("Read SE-encrypted long-term key",
@@ -73,10 +70,7 @@ func (s *SecureEnclaveUnlocker) GetIdentity() (*age.X25519Identity, error) {
// Decrypt using the Secure Enclave (ECDH happens inside SE hardware) // Decrypt using the Secure Enclave (ECDH happens inside SE hardware)
decryptedData, err := macse.Decrypt(seKeyLabel, encryptedData) decryptedData, err := macse.Decrypt(seKeyLabel, encryptedData)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to decrypt long-term key with SE: %w", err)
"failed to decrypt long-term key with SE: %w",
err,
)
} }
// Parse the decrypted long-term private key // Parse the decrypted long-term private key
@@ -88,10 +82,7 @@ func (s *SecureEnclaveUnlocker) GetIdentity() (*age.X25519Identity, error) {
} }
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to parse long-term private key: %w", err)
"failed to parse long-term private key: %w",
err,
)
} }
DebugWith("Successfully decrypted long-term key via SE", DebugWith("Successfully decrypted long-term key via SE",
@@ -174,11 +165,7 @@ func (s *SecureEnclaveUnlocker) getSEKeyInfo() (label string, hash string, err e
} }
// NewSecureEnclaveUnlocker creates a new SecureEnclaveUnlocker instance. // NewSecureEnclaveUnlocker creates a new SecureEnclaveUnlocker instance.
func NewSecureEnclaveUnlocker( func NewSecureEnclaveUnlocker(fs afero.Fs, directory string, metadata UnlockerMetadata) *SecureEnclaveUnlocker {
fs afero.Fs,
directory string,
metadata UnlockerMetadata,
) *SecureEnclaveUnlocker {
return &SecureEnclaveUnlocker{ return &SecureEnclaveUnlocker{
Directory: directory, Directory: directory,
Metadata: metadata, Metadata: metadata,
@@ -195,22 +182,13 @@ func generateSEKeyLabel(vaultName string) (string, error) {
enrollmentDate := time.Now().UTC().Format("2006-01-02") enrollmentDate := time.Now().UTC().Format("2006-01-02")
return fmt.Sprintf( return fmt.Sprintf("%s.%s-%s-%s", seKeyLabelPrefix, vaultName, hostname, enrollmentDate), nil
"%s.%s-%s-%s",
seKeyLabelPrefix,
vaultName,
hostname,
enrollmentDate,
), nil
} }
// CreateSecureEnclaveUnlocker creates a new SE unlocker. // CreateSecureEnclaveUnlocker creates a new SE unlocker.
// The vault's long-term private key is encrypted directly by the Secure Enclave // The vault's long-term private key is encrypted directly by the Secure Enclave
// using ECIES. No intermediate age keypair is used. // using ECIES. No intermediate age keypair is used.
func CreateSecureEnclaveUnlocker( func CreateSecureEnclaveUnlocker(fs afero.Fs, stateDir string) (*SecureEnclaveUnlocker, error) {
fs afero.Fs,
stateDir string,
) (*SecureEnclaveUnlocker, error) {
if err := checkMacOSAvailable(); err != nil { if err := checkMacOSAvailable(); err != nil {
return nil, err return nil, err
} }
@@ -238,20 +216,14 @@ func CreateSecureEnclaveUnlocker(
// Step 2: Get the vault's long-term private key // Step 2: Get the vault's long-term private key
ltPrivKeyData, err := getLongTermKeyForSE(fs, vault) ltPrivKeyData, err := getLongTermKeyForSE(fs, vault)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to get long-term private key: %w", err)
"failed to get long-term private key: %w",
err,
)
} }
defer ltPrivKeyData.Destroy() defer ltPrivKeyData.Destroy()
// Step 3: Encrypt the long-term key directly with the SE (ECIES) // Step 3: Encrypt the long-term key directly with the SE (ECIES)
encryptedLtKey, err := macse.Encrypt(seKeyLabel, ltPrivKeyData.Bytes()) encryptedLtKey, err := macse.Encrypt(seKeyLabel, ltPrivKeyData.Bytes())
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to encrypt long-term key with SE: %w", err)
"failed to encrypt long-term key with SE: %w",
err,
)
} }
// Step 4: Create unlocker directory and write files // Step 4: Create unlocker directory and write files
@@ -263,19 +235,13 @@ func CreateSecureEnclaveUnlocker(
unlockerDirName := fmt.Sprintf("se-%s", filepath.Base(seKeyLabel)) unlockerDirName := fmt.Sprintf("se-%s", filepath.Base(seKeyLabel))
unlockerDir := filepath.Join(vaultDir, "unlockers.d", unlockerDirName) unlockerDir := filepath.Join(vaultDir, "unlockers.d", unlockerDirName)
if err := fs.MkdirAll(unlockerDir, DirPerms); err != nil { if err := fs.MkdirAll(unlockerDir, DirPerms); err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to create unlocker directory: %w", err)
"failed to create unlocker directory: %w",
err,
)
} }
// Write SE-encrypted long-term key // Write SE-encrypted long-term key
ltKeyPath := filepath.Join(unlockerDir, seLongtermFilename) ltKeyPath := filepath.Join(unlockerDir, seLongtermFilename)
if err := afero.WriteFile(fs, ltKeyPath, encryptedLtKey, FilePerms); err != nil { if err := afero.WriteFile(fs, ltKeyPath, encryptedLtKey, FilePerms); err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to write SE-encrypted long-term key: %w", err)
"failed to write SE-encrypted long-term key: %w",
err,
)
} }
// Write metadata // Write metadata
@@ -308,10 +274,7 @@ func CreateSecureEnclaveUnlocker(
// getLongTermKeyForSE retrieves the vault's long-term private key // getLongTermKeyForSE retrieves the vault's long-term private key
// either from the mnemonic env var or by unlocking via the current unlocker. // either from the mnemonic env var or by unlocking via the current unlocker.
func getLongTermKeyForSE( func getLongTermKeyForSE(fs afero.Fs, vault VaultInterface) (*memguard.LockedBuffer, error) {
fs afero.Fs,
vault VaultInterface,
) (*memguard.LockedBuffer, error) {
envMnemonic := os.Getenv(EnvMnemonic) envMnemonic := os.Getenv(EnvMnemonic)
if envMnemonic != "" { if envMnemonic != "" {
// Read vault metadata to get the correct derivation index // Read vault metadata to get the correct derivation index
@@ -332,16 +295,9 @@ func getLongTermKeyForSE(
} }
// Use mnemonic with the vault's actual derivation index // Use mnemonic with the vault's actual derivation index
ltIdentity, err := agehd.DeriveIdentity( ltIdentity, err := agehd.DeriveIdentity(envMnemonic, metadata.DerivationIndex)
envMnemonic,
metadata.DerivationIndex,
)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to derive long-term key from mnemonic: %w", err)
"failed to derive long-term key from mnemonic: %w",
err,
)
} }
return memguard.NewBufferFromBytes([]byte(ltIdentity.String())), nil return memguard.NewBufferFromBytes([]byte(ltIdentity.String())), nil
@@ -354,29 +310,17 @@ func getLongTermKeyForSE(
currentIdentity, err := currentUnlocker.GetIdentity() currentIdentity, err := currentUnlocker.GetIdentity()
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to get current unlocker identity: %w", err)
"failed to get current unlocker identity: %w",
err,
)
} }
// All unlocker types store longterm.age in their directory // All unlocker types store longterm.age in their directory
longtermPath := filepath.Join( longtermPath := filepath.Join(currentUnlocker.GetDirectory(), "longterm.age")
currentUnlocker.GetDirectory(),
"longterm.age",
)
encryptedLtKey, err := afero.ReadFile(fs, longtermPath) encryptedLtKey, err := afero.ReadFile(fs, longtermPath)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf("failed to read encrypted long-term key: %w", err)
"failed to read encrypted long-term key: %w",
err,
)
} }
ltPrivKeyBuffer, err := DecryptWithIdentity( ltPrivKeyBuffer, err := DecryptWithIdentity(encryptedLtKey, currentIdentity)
encryptedLtKey,
currentIdentity,
)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to decrypt long-term key: %w", err) return nil, fmt.Errorf("failed to decrypt long-term key: %w", err)
} }

View File

@@ -10,9 +10,7 @@ import (
"github.com/spf13/afero" "github.com/spf13/afero"
) )
var errSENotSupported = fmt.Errorf( var errSENotSupported = fmt.Errorf("secure enclave unlockers are only supported on macOS")
"secure enclave unlockers are only supported on macOS",
)
// SecureEnclaveUnlockerMetadata is a stub for non-Darwin platforms. // SecureEnclaveUnlockerMetadata is a stub for non-Darwin platforms.
type SecureEnclaveUnlockerMetadata struct { type SecureEnclaveUnlockerMetadata struct {
@@ -50,10 +48,7 @@ func (s *SecureEnclaveUnlocker) GetDirectory() string {
// GetID returns the unlocker ID. // GetID returns the unlocker ID.
func (s *SecureEnclaveUnlocker) GetID() string { func (s *SecureEnclaveUnlocker) GetID() string {
return fmt.Sprintf( return fmt.Sprintf("%s-secure-enclave", s.Metadata.CreatedAt.Format("2006-01-02.15.04"))
"%s-secure-enclave",
s.Metadata.CreatedAt.Format("2006-01-02.15.04"),
)
} }
// Remove returns an error on non-Darwin platforms. // Remove returns an error on non-Darwin platforms.
@@ -63,11 +58,7 @@ func (s *SecureEnclaveUnlocker) Remove() error {
// NewSecureEnclaveUnlocker creates a stub SecureEnclaveUnlocker on non-Darwin platforms. // NewSecureEnclaveUnlocker creates a stub SecureEnclaveUnlocker on non-Darwin platforms.
// The returned instance's methods that require macOS functionality will return errors. // The returned instance's methods that require macOS functionality will return errors.
func NewSecureEnclaveUnlocker( func NewSecureEnclaveUnlocker(fs afero.Fs, directory string, metadata UnlockerMetadata) *SecureEnclaveUnlocker {
fs afero.Fs,
directory string,
metadata UnlockerMetadata,
) *SecureEnclaveUnlocker {
return &SecureEnclaveUnlocker{ return &SecureEnclaveUnlocker{
Directory: directory, Directory: directory,
Metadata: metadata, Metadata: metadata,
@@ -76,9 +67,6 @@ func NewSecureEnclaveUnlocker(
} }
// CreateSecureEnclaveUnlocker returns an error on non-Darwin platforms. // CreateSecureEnclaveUnlocker returns an error on non-Darwin platforms.
func CreateSecureEnclaveUnlocker( func CreateSecureEnclaveUnlocker(_ afero.Fs, _ string) (*SecureEnclaveUnlocker, error) {
_ afero.Fs,
_ string,
) (*SecureEnclaveUnlocker, error) {
return nil, errSENotSupported return nil, errSENotSupported
} }

View File

@@ -1,148 +0,0 @@
//go:build darwin
package secret
import (
"testing"
)
func TestValidateKeychainItemName(t *testing.T) {
tests := []struct {
name string
itemName string
wantErr bool
}{
// Valid cases
{
name: "valid simple name",
itemName: "my-secret-key",
wantErr: false,
},
{
name: "valid name with dots",
itemName: "com.example.app.key",
wantErr: false,
},
{
name: "valid name with underscores",
itemName: "my_secret_key_123",
wantErr: false,
},
{
name: "valid alphanumeric",
itemName: "Secret123Key",
wantErr: false,
},
{
name: "valid with hyphen at start",
itemName: "-my-key",
wantErr: false,
},
{
name: "valid with dot at start",
itemName: ".hidden-key",
wantErr: false,
},
// Invalid cases
{
name: "empty item name",
itemName: "",
wantErr: true,
},
{
name: "item name with spaces",
itemName: "my secret key",
wantErr: true,
},
{
name: "item name with semicolon",
itemName: "key;rm -rf /",
wantErr: true,
},
{
name: "item name with pipe",
itemName: "key|cat /etc/passwd",
wantErr: true,
},
{
name: "item name with backticks",
itemName: "key`whoami`",
wantErr: true,
},
{
name: "item name with dollar sign",
itemName: "key$(whoami)",
wantErr: true,
},
{
name: "item name with quotes",
itemName: "key\"name",
wantErr: true,
},
{
name: "item name with single quotes",
itemName: "key'name",
wantErr: true,
},
{
name: "item name with backslash",
itemName: "key\\name",
wantErr: true,
},
{
name: "item name with newline",
itemName: "key\nname",
wantErr: true,
},
{
name: "item name with carriage return",
itemName: "key\rname",
wantErr: true,
},
{
name: "item name with ampersand",
itemName: "key&echo test",
wantErr: true,
},
{
name: "item name with redirect",
itemName: "key>/tmp/test",
wantErr: true,
},
{
name: "item name with null byte",
itemName: "key\x00name",
wantErr: true,
},
{
name: "item name with parentheses",
itemName: "key(test)",
wantErr: true,
},
{
name: "item name with brackets",
itemName: "key[test]",
wantErr: true,
},
{
name: "item name with asterisk",
itemName: "key*",
wantErr: true,
},
{
name: "item name with question mark",
itemName: "key?",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateKeychainItemName(tt.itemName)
if (err != nil) != tt.wantErr {
t.Errorf("validateKeychainItemName() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@@ -154,3 +154,144 @@ func TestValidateGPGKeyID(t *testing.T) {
}) })
} }
} }
func TestValidateKeychainItemName(t *testing.T) {
tests := []struct {
name string
itemName string
wantErr bool
}{
// Valid cases
{
name: "valid simple name",
itemName: "my-secret-key",
wantErr: false,
},
{
name: "valid name with dots",
itemName: "com.example.app.key",
wantErr: false,
},
{
name: "valid name with underscores",
itemName: "my_secret_key_123",
wantErr: false,
},
{
name: "valid alphanumeric",
itemName: "Secret123Key",
wantErr: false,
},
{
name: "valid with hyphen at start",
itemName: "-my-key",
wantErr: false,
},
{
name: "valid with dot at start",
itemName: ".hidden-key",
wantErr: false,
},
// Invalid cases
{
name: "empty item name",
itemName: "",
wantErr: true,
},
{
name: "item name with spaces",
itemName: "my secret key",
wantErr: true,
},
{
name: "item name with semicolon",
itemName: "key;rm -rf /",
wantErr: true,
},
{
name: "item name with pipe",
itemName: "key|cat /etc/passwd",
wantErr: true,
},
{
name: "item name with backticks",
itemName: "key`whoami`",
wantErr: true,
},
{
name: "item name with dollar sign",
itemName: "key$(whoami)",
wantErr: true,
},
{
name: "item name with quotes",
itemName: "key\"name",
wantErr: true,
},
{
name: "item name with single quotes",
itemName: "key'name",
wantErr: true,
},
{
name: "item name with backslash",
itemName: "key\\name",
wantErr: true,
},
{
name: "item name with newline",
itemName: "key\nname",
wantErr: true,
},
{
name: "item name with carriage return",
itemName: "key\rname",
wantErr: true,
},
{
name: "item name with ampersand",
itemName: "key&echo test",
wantErr: true,
},
{
name: "item name with redirect",
itemName: "key>/tmp/test",
wantErr: true,
},
{
name: "item name with null byte",
itemName: "key\x00name",
wantErr: true,
},
{
name: "item name with parentheses",
itemName: "key(test)",
wantErr: true,
},
{
name: "item name with brackets",
itemName: "key[test]",
wantErr: true,
},
{
name: "item name with asterisk",
itemName: "key*",
wantErr: true,
},
{
name: "item name with question mark",
itemName: "key?",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateKeychainItemName(tt.itemName)
if (err != nil) != tt.wantErr {
t.Errorf("validateKeychainItemName() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}