-
Notifications
You must be signed in to change notification settings - Fork 76
Validate Signal runtime config #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,31 +19,53 @@ interface Config { | |||||
| deviceName?: string; | ||||||
| } | ||||||
|
|
||||||
| const RUNTIMES = ['signal-cli', 'signald'] as const; | ||||||
|
|
||||||
| function requirePhoneNumber(config: Config): string { | ||||||
| const phoneNumber = config.phoneNumber?.trim(); | ||||||
| if (!/^\+[1-9]\d{7,14}$/.test(phoneNumber)) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| throw new Error('chat-signal phoneNumber must be an E.164 number such as +14155551234'); | ||||||
| } | ||||||
| return phoneNumber; | ||||||
| } | ||||||
|
|
||||||
| function requireRuntime(config: Config): Config['runtime'] { | ||||||
| const runtime = String(config.runtime ?? '').trim(); | ||||||
| if (!RUNTIMES.includes(runtime as Config['runtime'])) { | ||||||
| throw new Error(`chat-signal runtime must be one of: ${RUNTIMES.join(', ')}`); | ||||||
| } | ||||||
| return runtime as Config['runtime']; | ||||||
| } | ||||||
|
|
||||||
| export default defineTarget<Config>({ | ||||||
| id: 'chat-signal', | ||||||
| kind: 'chat', | ||||||
| label: 'Signal (signal-cli / signald)', | ||||||
| async build(ctx, config) { | ||||||
| ctx.log(`prepare ${config.runtime} config for ${config.phoneNumber}`); | ||||||
| const phoneNumber = requirePhoneNumber(config); | ||||||
| const runtime = requireRuntime(config); | ||||||
| ctx.log(`prepare ${runtime} config for ${phoneNumber}`); | ||||||
| const artifactDir = join(ctx.outDir, 'signal-runtime'); | ||||||
| const planPath = join(artifactDir, 'signal-runtime-plan.json'); | ||||||
| await mkdir(artifactDir, { recursive: true }); | ||||||
| await writeFile(planPath, `${JSON.stringify({ | ||||||
| phoneNumber: config.phoneNumber, | ||||||
| runtime: config.runtime, | ||||||
| phoneNumber, | ||||||
| runtime, | ||||||
| deviceName: config.deviceName, | ||||||
| captchaTokenPresent: !!config.captchaToken, | ||||||
| }, null, 2)}\n`, 'utf-8'); | ||||||
| return { artifact: planPath }; | ||||||
| }, | ||||||
| async ship(ctx, config) { | ||||||
| ctx.log(`register Signal number ${config.phoneNumber} (${config.runtime})`); | ||||||
| const phoneNumber = requirePhoneNumber(config); | ||||||
| const runtime = requireRuntime(config); | ||||||
| ctx.log(`register Signal number ${phoneNumber} (${runtime})`); | ||||||
| if (ctx.dryRun) return { id: 'dry-run' }; | ||||||
| // TODO: | ||||||
| // - signal-cli register -v <phone> (requires captchaToken) | ||||||
| // - verify with SMS/voice code (human step unless using a SIP gateway) | ||||||
| // - store runtime secrets (identity keys) in secrets vault | ||||||
| return { id: `signal:${config.phoneNumber}@${ctx.version}` }; | ||||||
| return { id: `signal:${phoneNumber}@${ctx.version}` }; | ||||||
| }, | ||||||
| async status(id) { | ||||||
| return { state: 'live', version: id }; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buildwith invalid runtime. The new runtime-rejection test only exercisesship. Sincebuildalso callsrequireRuntimedirectly, a counterpart test that passes an invalid runtime tobuildwould confirm the guard fires on both entry points. Without it, a future refactor that accidentally drops therequireRuntimecall frombuildwould go undetected.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!