Skip to content

feat: add webhook options for encryption in CLI#120

Merged
dillonstreator merged 3 commits intoosbytes:mainfrom
adavila0703:main
May 1, 2026
Merged

feat: add webhook options for encryption in CLI#120
dillonstreator merged 3 commits intoosbytes:mainfrom
adavila0703:main

Conversation

@adavila0703
Copy link
Copy Markdown
Contributor

@adavila0703 adavila0703 commented Apr 30, 2026

Introduces new command-line options for webhook integration during the encryption process. Users can now specify a webhook URL, an optional label, and a comma-separated list of events to receive notifications for.

Usage:

cfyi encrypt "your secret" \
  --wh-url https://example.com/hooks/crypt-fyi \
  --wh-events read,burn \
  --wh-name "CI token"

Available events for --wh-events:

  • read - When the secret is read successfully (default)
  • burn - When the secret is burned
  • failed-key - When decryption fails (wrong key or password)
  • failed-ip - When the viewer IP fails the IP allow-list
    Multiple events can be specified as a comma-separated list (e.g., read,burn,failed-key).

Changes:

  • Replaced per-event boolean flags with single --wh-events list flag
  • Validated webhook config via vaultValueSchema from @crypt.fyi/core
  • Added parseWhEvents and trimmedWhName helpers with unit tests

Closes #51

Introduces new command-line options for webhook integration during the encryption process. Users can now specify a webhook URL, name, and toggle notifications for various events such as successful reads, failed decryption, and burns. Updated README to include usage examples and detailed descriptions of the new options.

Closes osbytes#118
Copy link
Copy Markdown
Contributor

@dillonstreator dillonstreator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Direction is right but the flag interface needs rework before merge.

1. Replace per-event boolean flags with single --wh-events list

Current shape (5 flags w/ mixed --no-* / --*-on-* polarity) is confusing and won't scale. Every new server event = new CLI flag + new validation branch.

Proposed: one comma-separated --wh-events flag. Default read (matches server zod default r: true). Use wh- prefix to align with payload schema keys (wh.u, wh.n, wh.r, wh.b, wh.fpk, wh.fip).

.option('--wh-url <url>', 'webhook URL for notifications')
.option('--wh-events <list>', 'comma-separated events: read,burn,failed-key,failed-ip', 'read')
.option('--wh-name <name>', 'optional label (max 50 chars)')

Usage:

cfyi encrypt "secret" \
  --wh-url https://example.com/hook \
  --wh-events read,burn \
  --wh-name "CI token"

Benefits:

  • Drops ~20 lines of dangling-flag validation (index.ts:50-69)
  • Uniform additive polarity (no --no-* footgun)
  • CLI ↔ wire-format consistency via wh- prefix
  • Future events = add to allow-list, no new flag
  • No single-char shorts for now — leaves -w/-W namespace open if demand emerges (and avoids the lowercase/uppercase-meaning-different-things pitfall)

Sketch for parsing:

const VALID_EVENTS = ['read', 'failed-key', 'failed-ip', 'burn'] as const;
const EVENT_TO_KEY = {
  'read': 'r',
  'failed-key': 'fpk',
  'failed-ip': 'fip',
  'burn': 'b',
} as const;

function parseWhEvents(raw: string) {
  const events = raw.split(',').map(s => s.trim()).filter(Boolean);
  const flags = { r: false, fpk: false, fip: false, b: false };
  for (const e of events) {
    if (!(e in EVENT_TO_KEY)) {
      throw new Error(`Unknown webhook event: '${e}'. Valid: ${VALID_EVENTS.join(', ')}`);
    }
    flags[EVENT_TO_KEY[e as keyof typeof EVENT_TO_KEY]] = true;
  }
  return flags;
}

2. Drop duplicate validation; reuse zod schema from @crypt.fyi/core

validateWebhookCliInput (index.ts:213-231) re-implements URL parsing + name length checks already enforced by vaultValueSchema.wh in core. Single source of truth:

import { vaultValueSchema } from '@crypt.fyi/core';

vaultValueSchema.shape.wh.unwrap().parse({
  u: whUrl,
  n: trimmedWhName(options.whName),
  ...eventFlags,
});

Catches future schema changes (new constraints, new fields) automatically. Removes ~14 lines.

3. Don't hardcode CLI defaults that mirror server defaults

Currently CLI sends r: options.webhookOnRead (commander default true). Server zod also defaults r: true. Fine today, but means future server-side default change won't propagate to CLI users transparently. With --wh-events defaulting to 'read' at CLI level this is moot for read — but the principle still applies for failed-key/failed-ip/burn: send only what user requested, let server apply zod defaults to absent keys.

4. README example wording

Example uses --webhook-on-burn alone — payload fires on read AND burn. Current text mentions "By default, the API is also notified on a successful read" but it's buried after the example. With --wh-events-style interface this is naturally explicit (--wh-events read,burn).

5. Tests

PR adds zero tests. At minimum, unit-test parseWhEvents (valid list, unknown event, empty string, whitespace handling, duplicates).

Nits

  • Trailing whitespace index.ts:215 after let url: URL;
  • Smart quote (U+2019) in README "website's" — check repo convention

TL;DR: collapse 5 booleans → 1 list flag (--wh-events), use schema for validation, add tests. Happy to pair on the rewrite if helpful.

adavila0703 and others added 2 commits April 30, 2026 19:46
…ate via schema

Reworks the CLI webhook interface based on PR review feedback:

- Replace 5 per-event boolean flags with single --wh-events comma-separated list

- Use vaultValueSchema from @crypt.fyi/core for validation instead of manual checks

- Add parseWhEvents and trimmedWhName helpers in new webhook.ts module

- Add jest + ts-jest and unit tests for webhook event parsing

- Update README with new --wh-url, --wh-events, --wh-name usage examples
@adavila0703 adavila0703 requested a review from dillonstreator May 1, 2026 02:34
@dillonstreator dillonstreator merged commit af898e9 into osbytes:main May 1, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add webhook support to cli

2 participants