@stacksjs/defaults 0.75.628 → 0.75.630
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ai/skills/stacks-actions/SKILL.md +1 -1
- package/ai/skills/stacks-env/SKILL.md +5 -0
- package/ai/skills/stacks-native/CATALOG.md +6 -6
- package/app/Actions/Dashboard/Infrastructure/environment-file.ts +5 -1
- package/app/Actions/Dashboard/Infrastructure/environment-layers.test.ts +126 -0
- package/app/Actions/Dashboard/Infrastructure/environment-layers.ts +192 -0
- package/app/Actions/Dashboard/Settings/mail-settings.test.ts +45 -5
- package/app/Actions/Dashboard/Settings/mail-settings.ts +91 -86
- package/ide/vscode/package.json +1 -1
- package/package.json +5 -5
- package/resources/components/Dashboard/Settings/MailSettingsDashboard.stx +14 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: stacks-actions
|
|
3
|
-
description: Use when working with Stacks server actions - creating actions in app/Actions/, auto-generated API actions from the useApi model trait, the
|
|
3
|
+
description: Use when working with Stacks server actions - creating actions in app/Actions/, auto-generated API actions from the useApi model trait, the 779 default framework actions (auth, dashboard, commerce, content, deployment, jobs), action request/response handling, or action registration. Covers @stacksjs/actions and storage/framework/defaults/app/Actions/.
|
|
4
4
|
license: MIT
|
|
5
5
|
compatibility: Bun >= 1.3.0, TypeScript
|
|
6
6
|
allowed-tools: Read Edit Write Bash Grep Glob
|
|
@@ -190,6 +190,11 @@ import { loadEnv, autoLoadEnv, envPlugin } from '@stacksjs/env'
|
|
|
190
190
|
const loaded = loadEnv({ path: '.env', overload: false }) // { loaded, errors }
|
|
191
191
|
autoLoadEnv() // .env, .env.local, .env.<effectiveEnv>, .env.<effectiveEnv>.local
|
|
192
192
|
|
|
193
|
+
// The files autoLoadEnv reads, in that order, existing ones only. The last
|
|
194
|
+
// one that sets a key (and that this process can decrypt) is where the app's
|
|
195
|
+
// value comes from, so a tool that edits env files writes there.
|
|
196
|
+
environmentFiles(activeEnvName(), projectRoot) // ['.env', '.env.development']
|
|
197
|
+
|
|
193
198
|
// Bun plugin (auto-loads .env before app starts)
|
|
194
199
|
envPlugin()
|
|
195
200
|
```
|
|
@@ -21,7 +21,7 @@ Browser runtime bindings are documented separately in `stacks-auto-imports`.
|
|
|
21
21
|
| `@stacksjs/auth` | [`stacks-auth`](../stacks-auth/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/auth/src/index.ts) | 136 |
|
|
22
22
|
| `@stacksjs/browser` | [`stacks-browser`](../stacks-browser/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/browser/src/index.ts) | 10 |
|
|
23
23
|
| `@stacksjs/browser-extension` | [`stacks-browser-extension`](../stacks-browser-extension/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/browser-extension/src/index.ts) | 9 |
|
|
24
|
-
| `@stacksjs/buddy` | [`stacks-buddy`](../stacks-buddy/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/buddy/src/index.ts) |
|
|
24
|
+
| `@stacksjs/buddy` | [`stacks-buddy`](../stacks-buddy/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/buddy/src/index.ts) | 215 |
|
|
25
25
|
| `@stacksjs/build` | [`stacks-build`](../stacks-build/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/build/src/index.ts) | 7 |
|
|
26
26
|
| `@stacksjs/cache` | [`stacks-cache`](../stacks-cache/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/cache/src/index.ts) | 24 |
|
|
27
27
|
| `@stacksjs/calendar-api` | [`stacks-calendar`](../stacks-calendar/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/calendar-api/src/index.ts) | 5 |
|
|
@@ -45,7 +45,7 @@ Browser runtime bindings are documented separately in `stacks-auto-imports`.
|
|
|
45
45
|
| `@stacksjs/docs` | [`stacks-docs`](../stacks-docs/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/docs/src/index.ts) | 5 |
|
|
46
46
|
| `@stacksjs/email` | [`stacks-email`](../stacks-email/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/email/src/index.ts) | 27 |
|
|
47
47
|
| `@stacksjs/enums` | [`stacks-enums`](../stacks-enums/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/enums/src/index.ts) | 3 |
|
|
48
|
-
| `@stacksjs/env` | [`stacks-env`](../stacks-env/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/index.ts) |
|
|
48
|
+
| `@stacksjs/env` | [`stacks-env`](../stacks-env/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/index.ts) | 33 |
|
|
49
49
|
| `@stacksjs/error-handling` | [`stacks-error-handling`](../stacks-error-handling/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/error-handling/src/index.ts) | 10 |
|
|
50
50
|
| `@stacksjs/events` | [`stacks-events`](../stacks-events/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/events/src/index.ts) | 13 |
|
|
51
51
|
| `@stacksjs/faker` | [`stacks-faker`](../stacks-faker/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/faker/src/index.ts) | 1 |
|
|
@@ -66,7 +66,7 @@ Browser runtime bindings are documented separately in `stacks-auto-imports`.
|
|
|
66
66
|
| `@stacksjs/newsletter` | [`stacks-newsletter`](../stacks-newsletter/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/newsletter/src/index.ts) | 10 |
|
|
67
67
|
| `@stacksjs/notifications` | [`stacks-notifications`](../stacks-notifications/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/notifications/src/index.ts) | 18 |
|
|
68
68
|
| `@stacksjs/objects` | [`stacks-objects`](../stacks-objects/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/objects/src/index.ts) | 10 |
|
|
69
|
-
| `@stacksjs/orm` | [`stacks-orm`](../stacks-orm/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/index.ts) |
|
|
69
|
+
| `@stacksjs/orm` | [`stacks-orm`](../stacks-orm/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/index.ts) | 80 |
|
|
70
70
|
| `@stacksjs/pagination` | [`stacks-pagination`](../stacks-pagination/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/pagination/src/index.ts) | 1 |
|
|
71
71
|
| `@stacksjs/path` | [`stacks-path`](../stacks-path/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/path/src/index.ts) | 14 |
|
|
72
72
|
| `@stacksjs/payments` | [`stacks-payments`](../stacks-payments/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/payments/src/index.ts) | 40 |
|
|
@@ -222,7 +222,7 @@ Read the entrypoint and package export map for re-exported or type-only surfaces
|
|
|
222
222
|
|
|
223
223
|
Local source modules: [src/commands/index.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/buddy/src/commands/index.ts).
|
|
224
224
|
|
|
225
|
-
Evidence directory: [
|
|
225
|
+
Evidence directory: [215 retained tests](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/buddy/tests). These files are evidence pointers, not a claim that all tests were run for this catalog.
|
|
226
226
|
|
|
227
227
|
### @stacksjs/build
|
|
228
228
|
|
|
@@ -452,7 +452,7 @@ Read the entrypoint and package export map for re-exported or type-only surfaces
|
|
|
452
452
|
|
|
453
453
|
Local source modules: [src/cli.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/cli.ts), [src/crypto.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/crypto.ts), [src/deployment.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/deployment.ts), [src/integrations.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/integrations.ts), [src/numeric-keys.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/numeric-keys.ts), [src/parser.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/parser.ts), [src/plaintext.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/plaintext.ts), [src/plugin.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/plugin.ts), [src/runtime.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/runtime.ts), [src/tenants.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/tenants.ts), [src/types.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/types.ts), [src/utils.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/utils.ts), [src/withheld.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/src/withheld.ts).
|
|
454
454
|
|
|
455
|
-
Evidence directory: [
|
|
455
|
+
Evidence directory: [33 retained tests](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/env/tests). These files are evidence pointers, not a claim that all tests were run for this catalog.
|
|
456
456
|
|
|
457
457
|
### @stacksjs/error-handling
|
|
458
458
|
|
|
@@ -654,7 +654,7 @@ Declared runtime exports: `Activity`, `AnalyticsEvent`, `Auction`, `AuctionItem`
|
|
|
654
654
|
|
|
655
655
|
Local source modules: [src/auto-crud.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/auto-crud.ts), [src/batch-loader.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/batch-loader.ts), [src/db.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/db.ts), [src/define-model.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/define-model.ts), [src/extend-model.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/extend-model.ts), [src/gdpr/index.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/gdpr/index.ts), [src/generate-database-schema.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/generate-database-schema.ts), [src/locked-row.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/locked-row.ts), [src/messaging.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/messaging.ts), [src/model-types.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/model-types.ts), [src/ownership.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/ownership.ts), [src/paginator-request.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/paginator-request.ts), [src/row-token.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/row-token.ts), [src/subquery.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/subquery.ts), [src/traits/audit.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/traits/audit.ts), [src/traits/billable.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/traits/billable.ts), [src/traits/categorizable.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/traits/categorizable.ts), [src/traits/commentable.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/traits/commentable.ts), [src/traits/likeable.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/traits/likeable.ts), [src/traits/taggable.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/traits/taggable.ts), [src/transaction.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/transaction.ts), [src/types.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/types.ts), [src/utils/encrypted.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/utils/encrypted.ts), [src/utils/prunable.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/src/utils/prunable.ts).
|
|
656
656
|
|
|
657
|
-
Evidence directory: [
|
|
657
|
+
Evidence directory: [80 retained tests](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/orm/tests). These files are evidence pointers, not a claim that all tests were run for this catalog.
|
|
658
658
|
|
|
659
659
|
### @stacksjs/pagination
|
|
660
660
|
|
|
@@ -101,7 +101,11 @@ function serializeEnvironmentValue(value: string): string {
|
|
|
101
101
|
.replace(/\n/g, '\\n')}"`
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
/**
|
|
105
|
+
* Write a file the way an env file has to be written: whole or not at all
|
|
106
|
+
* (a same-directory rename), and readable by its owner only.
|
|
107
|
+
*/
|
|
108
|
+
export async function atomicWrite(path: string, content: string): Promise<void> {
|
|
105
109
|
await mkdir(dirname(path), { recursive: true })
|
|
106
110
|
const temporaryPath = join(
|
|
107
111
|
dirname(path),
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { decryptValue, encryptValue, generateKeypair } from '@stacksjs/env'
|
|
6
|
+
import { readEnvironmentLayers, updateEnvironmentLayers } from './environment-layers'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The env files the app loads, read and written the way it loads them
|
|
10
|
+
* (stacksjs/stacks#3340): a later file wins, an encrypted entry this process
|
|
11
|
+
* cannot decrypt does not, and an encrypted value stays encrypted.
|
|
12
|
+
*/
|
|
13
|
+
const directories: string[] = []
|
|
14
|
+
|
|
15
|
+
function project(files: Record<string, string>): { cwd: string, backupPath: string } {
|
|
16
|
+
const cwd = mkdtempSync(join(tmpdir(), 'stacks-env-layers-'))
|
|
17
|
+
directories.push(cwd)
|
|
18
|
+
for (const [name, content] of Object.entries(files))
|
|
19
|
+
writeFileSync(join(cwd, name), content)
|
|
20
|
+
return { cwd, backupPath: join(cwd, 'runtime', 'environment.backup') }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function text(cwd: string, file: string): string {
|
|
24
|
+
return readFileSync(join(cwd, file), 'utf8')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
for (const directory of directories.splice(0))
|
|
29
|
+
rmSync(directory, { recursive: true, force: true })
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
describe('environment layers', () => {
|
|
33
|
+
test('a key set again in a later file comes from that file, and is saved there', async () => {
|
|
34
|
+
const location = project({
|
|
35
|
+
'.env': 'MAIL_HOST=base.test\nMAIL_PORT=25\n',
|
|
36
|
+
'.env.staging': 'MAIL_HOST=staging.test\n',
|
|
37
|
+
})
|
|
38
|
+
const options = { ...location, env: 'staging', privateKey: null }
|
|
39
|
+
|
|
40
|
+
const layers = await readEnvironmentLayers(options)
|
|
41
|
+
expect(layers.layers.map(layer => layer.file)).toEqual(['.env', '.env.staging'])
|
|
42
|
+
expect(layers.entries.MAIL_HOST).toBe('staging.test')
|
|
43
|
+
expect(layers.sources).toMatchObject({ MAIL_HOST: '.env.staging', MAIL_PORT: '.env' })
|
|
44
|
+
|
|
45
|
+
const saved = await updateEnvironmentLayers({ MAIL_HOST: 'saved.test', MAIL_PORT: '2525', MAIL_MAILER: 'smtp' }, layers.revision, options)
|
|
46
|
+
expect(saved.layers?.entries).toMatchObject({ MAIL_HOST: 'saved.test', MAIL_PORT: '2525', MAIL_MAILER: 'smtp' })
|
|
47
|
+
expect(text(location.cwd, '.env.staging')).toBe('MAIL_HOST=saved.test\n')
|
|
48
|
+
// A key no file sets is added to `.env`.
|
|
49
|
+
expect(text(location.cwd, '.env')).toBe('MAIL_HOST=base.test\nMAIL_PORT=2525\n\nMAIL_MAILER=smtp\n')
|
|
50
|
+
expect(text(location.cwd, 'runtime/environment.backup')).toBe('MAIL_HOST=base.test\nMAIL_PORT=25\n')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test('an encrypted entry with no key to read it does not win, and is named', async () => {
|
|
54
|
+
const { publicKey } = generateKeypair()
|
|
55
|
+
const location = project({
|
|
56
|
+
'.env': 'MAIL_HOST=base.test\n',
|
|
57
|
+
'.env.staging': `DOTENV_PUBLIC_KEY_STAGING=${publicKey}\nMAIL_HOST="${encryptValue('staging.test', publicKey)}"\n`,
|
|
58
|
+
})
|
|
59
|
+
const options = { ...location, env: 'staging', privateKey: null }
|
|
60
|
+
|
|
61
|
+
const layers = await readEnvironmentLayers(options)
|
|
62
|
+
expect(layers.entries.MAIL_HOST).toBe('base.test')
|
|
63
|
+
expect(layers.sources.MAIL_HOST).toBe('.env')
|
|
64
|
+
expect(layers.unreadable).toEqual({ MAIL_HOST: '.env.staging' })
|
|
65
|
+
|
|
66
|
+
const staging = text(location.cwd, '.env.staging')
|
|
67
|
+
await updateEnvironmentLayers({ MAIL_HOST: 'saved.test' }, layers.revision, options)
|
|
68
|
+
expect(text(location.cwd, '.env')).toBe('MAIL_HOST=saved.test\n')
|
|
69
|
+
expect(text(location.cwd, '.env.staging')).toBe(staging)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('an encrypted value is read decrypted and saved encrypted, under its file\'s key', async () => {
|
|
73
|
+
const { publicKey, privateKey } = generateKeypair()
|
|
74
|
+
const location = project({
|
|
75
|
+
'.env': 'MAIL_HOST=base.test\n',
|
|
76
|
+
'.env.staging': `DOTENV_PUBLIC_KEY_STAGING=${publicKey}\nMAIL_HOST="${encryptValue('staging.test', publicKey)}"\nMAIL_PASSWORD=plain-before\n`,
|
|
77
|
+
})
|
|
78
|
+
const options = { ...location, env: 'staging', privateKey, secrets: ['MAIL_PASSWORD'] }
|
|
79
|
+
|
|
80
|
+
const layers = await readEnvironmentLayers(options)
|
|
81
|
+
expect(layers.entries.MAIL_HOST).toBe('staging.test')
|
|
82
|
+
expect(layers.sources.MAIL_HOST).toBe('.env.staging')
|
|
83
|
+
|
|
84
|
+
await updateEnvironmentLayers({ MAIL_HOST: 'saved.test', MAIL_PASSWORD: 'new-secret' }, layers.revision, options)
|
|
85
|
+
const staging = Object.fromEntries(text(location.cwd, '.env.staging').trim().split('\n').map(line => [line.slice(0, line.indexOf('=')), line.slice(line.indexOf('=') + 1).replace(/^"|"$/g, '')]))
|
|
86
|
+
expect(staging.MAIL_HOST).toStartWith('encrypted:')
|
|
87
|
+
expect(decryptValue(staging.MAIL_HOST!, privateKey)).toBe('saved.test')
|
|
88
|
+
// A secret is encrypted in a file that holds ciphertext, even where it was plain.
|
|
89
|
+
expect(staging.MAIL_PASSWORD).toStartWith('encrypted:')
|
|
90
|
+
expect(decryptValue(staging.MAIL_PASSWORD!, privateKey)).toBe('new-secret')
|
|
91
|
+
expect(text(location.cwd, '.env')).toBe('MAIL_HOST=base.test\n')
|
|
92
|
+
expect((await readEnvironmentLayers(options)).entries.MAIL_HOST).toBe('saved.test')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('refuses to write a value that has to be encrypted when its file declares no public key', async () => {
|
|
96
|
+
const { publicKey, privateKey } = generateKeypair()
|
|
97
|
+
const location = project({ '.env.staging': `MAIL_HOST="${encryptValue('staging.test', publicKey)}"\n` })
|
|
98
|
+
const options = { ...location, env: 'staging', privateKey }
|
|
99
|
+
const before = text(location.cwd, '.env.staging')
|
|
100
|
+
|
|
101
|
+
const layers = await readEnvironmentLayers(options)
|
|
102
|
+
const result = await updateEnvironmentLayers({ MAIL_HOST: 'saved.test' }, layers.revision, options)
|
|
103
|
+
expect(result.issues).toEqual([{ key: 'MAIL_HOST', message: '.env.staging holds encrypted values but declares no public key. Repair it before changing MAIL_HOST.' }])
|
|
104
|
+
expect(text(location.cwd, '.env.staging')).toBe(before)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
test('refuses a write when any file changed since it was read', async () => {
|
|
108
|
+
const location = project({ '.env': 'MAIL_HOST=base.test\n', '.env.staging': 'MAIL_PORT=25\n' })
|
|
109
|
+
const options = { ...location, env: 'staging', privateKey: null }
|
|
110
|
+
const layers = await readEnvironmentLayers(options)
|
|
111
|
+
|
|
112
|
+
writeFileSync(join(location.cwd, '.env.staging'), 'MAIL_PORT=587\n')
|
|
113
|
+
expect(await updateEnvironmentLayers({ MAIL_HOST: 'saved.test' }, layers.revision, options)).toEqual({ conflict: true })
|
|
114
|
+
expect(text(location.cwd, '.env')).toBe('MAIL_HOST=base.test\n')
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('writes .env when the project has no env file yet', async () => {
|
|
118
|
+
const location = project({})
|
|
119
|
+
const options = { ...location, env: 'staging', privateKey: null }
|
|
120
|
+
const layers = await readEnvironmentLayers(options)
|
|
121
|
+
expect(layers.layers).toEqual([])
|
|
122
|
+
|
|
123
|
+
await updateEnvironmentLayers({ MAIL_MAILER: 'log' }, layers.revision, options)
|
|
124
|
+
expect(text(location.cwd, '.env')).toBe('MAIL_MAILER=log\n')
|
|
125
|
+
})
|
|
126
|
+
})
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { readFile } from 'node:fs/promises'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import { activeEnvName, declaredEnvPublicKey, encryptValue, environmentFiles, envHasCiphertext, parse, resolvePrivateKey } from '@stacksjs/env'
|
|
5
|
+
import { frameworkRuntimePath, projectPath } from '@stacksjs/path'
|
|
6
|
+
import { applyEnvironmentEntries, atomicWrite, parseEnvironmentEntries, validateEnvironmentFile } from './environment-file'
|
|
7
|
+
import type { EnvironmentFileIssue } from './environment-file'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The env files the app loads, read the way it loads them (stacksjs/stacks#3340).
|
|
11
|
+
*
|
|
12
|
+
* The app reads `.env`, `.env.local`, `.env.<env>` and `.env.<env>.local`
|
|
13
|
+
* (`environmentFiles` in @stacksjs/env), and a later file's value replaces an
|
|
14
|
+
* earlier one's. A page that reads and writes `.env` alone shows values the
|
|
15
|
+
* app is not using as soon as another file sets the same key, and its saves
|
|
16
|
+
* change nothing the app reads.
|
|
17
|
+
*
|
|
18
|
+
* So values are read from every file, each with the loader's own parser and
|
|
19
|
+
* private key, and a write goes to the file a key's value comes from.
|
|
20
|
+
*
|
|
21
|
+
* The parser is the loader's, expansion included: a value written
|
|
22
|
+
* `${OTHER}` or `$(allowed-command)` reads as the app reads it.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export interface EnvironmentLayersOptions {
|
|
26
|
+
/** The project directory. Default: the project root. */
|
|
27
|
+
cwd?: string
|
|
28
|
+
/** The environment whose files are read. Default: the one the app loaded (`activeEnvName`). */
|
|
29
|
+
env?: string
|
|
30
|
+
/** The key encrypted values are decrypted with. Default: the app's (`resolvePrivateKey`); null for none. */
|
|
31
|
+
privateKey?: string | null
|
|
32
|
+
/** Where the replaced `.env` is kept. Default: the Environment page's backup. */
|
|
33
|
+
backupPath?: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface EnvironmentLayer {
|
|
37
|
+
/** The file's name in the project directory, e.g. `.env.development`. */
|
|
38
|
+
file: string
|
|
39
|
+
content: string
|
|
40
|
+
/** Its entries as written, ciphertext and all. */
|
|
41
|
+
raw: Record<string, string>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface EnvironmentLayers {
|
|
45
|
+
/** A hash of every file's name and content; a write is refused when it changed. */
|
|
46
|
+
revision: string
|
|
47
|
+
/** The files that exist, in the order the app loads them. */
|
|
48
|
+
layers: EnvironmentLayer[]
|
|
49
|
+
/** The values the app reads, decrypted. */
|
|
50
|
+
entries: Record<string, string>
|
|
51
|
+
/** For each key, the file its value comes from. */
|
|
52
|
+
sources: Record<string, string>
|
|
53
|
+
/**
|
|
54
|
+
* Keys a file loaded after the winning one sets with ciphertext this process
|
|
55
|
+
* cannot decrypt. The loader skips such an entry, so it does not win here;
|
|
56
|
+
* wherever its key is present, it does, and replaces what was saved.
|
|
57
|
+
*/
|
|
58
|
+
unreadable: Record<string, string>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface EnvironmentLayersIssue {
|
|
62
|
+
/** The env key the issue is about, or the file for a whole-file problem. */
|
|
63
|
+
key: string
|
|
64
|
+
message: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function resolved(options: EnvironmentLayersOptions): { cwd: string, privateKey: string | undefined, backupPath: string, env: string } {
|
|
68
|
+
const cwd = options.cwd ?? projectPath()
|
|
69
|
+
const env = options.env ?? activeEnvName()
|
|
70
|
+
return {
|
|
71
|
+
cwd,
|
|
72
|
+
env,
|
|
73
|
+
privateKey: options.privateKey === null ? undefined : (options.privateKey ?? resolvePrivateKey({ env, cwd })),
|
|
74
|
+
backupPath: options.backupPath ?? frameworkRuntimePath('dashboard/environment.backup'),
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function isCiphertext(value: string | undefined): boolean {
|
|
79
|
+
return Boolean(value && (value.startsWith('encrypted:') || value.startsWith('enc:')))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function revisionOf(layers: EnvironmentLayer[]): string {
|
|
83
|
+
const hash = createHash('sha256')
|
|
84
|
+
for (const layer of layers)
|
|
85
|
+
hash.update(layer.file).update('\0').update(layer.content).update('\0')
|
|
86
|
+
return hash.digest('hex')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Every env file the app loads, and the value it reads for each key. */
|
|
90
|
+
export async function readEnvironmentLayers(options: EnvironmentLayersOptions = {}): Promise<EnvironmentLayers> {
|
|
91
|
+
const { cwd, env, privateKey } = resolved(options)
|
|
92
|
+
const layers: EnvironmentLayer[] = []
|
|
93
|
+
for (const file of environmentFiles(env, cwd)) {
|
|
94
|
+
const content = await readFile(join(cwd, file), 'utf8')
|
|
95
|
+
layers.push({ file, content, raw: parseEnvironmentEntries(content) })
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const entries: Record<string, string> = {}
|
|
99
|
+
const sources: Record<string, string> = {}
|
|
100
|
+
const unreadable: Record<string, string> = {}
|
|
101
|
+
for (const layer of layers) {
|
|
102
|
+
const { parsed, skippedEncrypted } = parse(layer.content, {
|
|
103
|
+
privateKey,
|
|
104
|
+
processEnv: process.env as Record<string, string>,
|
|
105
|
+
})
|
|
106
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
107
|
+
entries[key] = value
|
|
108
|
+
sources[key] = layer.file
|
|
109
|
+
delete unreadable[key]
|
|
110
|
+
}
|
|
111
|
+
for (const key of skippedEncrypted)
|
|
112
|
+
unreadable[key] = layer.file
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return { revision: revisionOf(layers), layers, entries, sources, unreadable }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Each layer checked as a file: a malformed one is named, with its line. */
|
|
119
|
+
export function environmentLayerIssues(layers: EnvironmentLayers): Array<EnvironmentFileIssue & { file: string }> {
|
|
120
|
+
return layers.layers.flatMap(layer => validateEnvironmentFile(layer.content).map(issue => ({ ...issue, file: layer.file })))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Write each key to the file the app reads it from, or to `.env` when no file
|
|
125
|
+
* sets it.
|
|
126
|
+
*
|
|
127
|
+
* A value that is encrypted in that file stays encrypted, under the file's own
|
|
128
|
+
* public key, and so does a key named in `secrets` in a file that holds any
|
|
129
|
+
* ciphertext. An empty value is written as it is. Each changed file is
|
|
130
|
+
* replaced atomically; `.env` keeps the backup the Environment page keeps.
|
|
131
|
+
*/
|
|
132
|
+
export async function updateEnvironmentLayers(
|
|
133
|
+
updates: Record<string, string>,
|
|
134
|
+
revision: string,
|
|
135
|
+
options: EnvironmentLayersOptions & { secrets?: readonly string[] } = {},
|
|
136
|
+
): Promise<{ layers?: EnvironmentLayers, conflict?: true, issues?: EnvironmentLayersIssue[] }> {
|
|
137
|
+
const current = await readEnvironmentLayers(options)
|
|
138
|
+
if (current.revision !== revision)
|
|
139
|
+
return { conflict: true }
|
|
140
|
+
|
|
141
|
+
const { cwd, backupPath } = resolved(options)
|
|
142
|
+
const secrets = new Set(options.secrets ?? [])
|
|
143
|
+
const byFile = new Map<string, Record<string, string>>()
|
|
144
|
+
const issues: EnvironmentLayersIssue[] = []
|
|
145
|
+
|
|
146
|
+
for (const [key, value] of Object.entries(updates)) {
|
|
147
|
+
const file = current.sources[key] ?? '.env'
|
|
148
|
+
const layer = current.layers.find(candidate => candidate.file === file)
|
|
149
|
+
const content = layer?.content ?? ''
|
|
150
|
+
const encrypt = value !== '' && (isCiphertext(layer?.raw[key]) || (secrets.has(key) && envHasCiphertext(content)))
|
|
151
|
+
|
|
152
|
+
let written = value
|
|
153
|
+
if (encrypt) {
|
|
154
|
+
const publicKey = declaredEnvPublicKey(content, file)?.key
|
|
155
|
+
if (!publicKey) {
|
|
156
|
+
issues.push({ key, message: `${file} holds encrypted values but declares no public key. Repair it before changing ${key}.` })
|
|
157
|
+
continue
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
written = encryptValue(value, publicKey)
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
issues.push({ key, message: `${key} could not be encrypted with the public key ${file} declares.` })
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
byFile.set(file, { ...byFile.get(file), [key]: written })
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (issues.length)
|
|
172
|
+
return { issues }
|
|
173
|
+
|
|
174
|
+
const rewritten = [...byFile].map(([file, fileUpdates]) => {
|
|
175
|
+
const layer = current.layers.find(candidate => candidate.file === file)
|
|
176
|
+
return { file, before: layer?.content, after: applyEnvironmentEntries(layer?.content ?? '', fileUpdates) }
|
|
177
|
+
})
|
|
178
|
+
for (const { file, after } of rewritten) {
|
|
179
|
+
for (const issue of validateEnvironmentFile(after))
|
|
180
|
+
issues.push({ key: file, message: issue.line ? `${file} line ${issue.line}: ${issue.message}` : `${file}: ${issue.message}` })
|
|
181
|
+
}
|
|
182
|
+
if (issues.length)
|
|
183
|
+
return { issues }
|
|
184
|
+
|
|
185
|
+
for (const { file, before, after } of rewritten) {
|
|
186
|
+
if (file === '.env' && before !== undefined)
|
|
187
|
+
await atomicWrite(backupPath, before)
|
|
188
|
+
await atomicWrite(join(cwd, file), after)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return { layers: await readEnvironmentLayers(options) }
|
|
192
|
+
}
|
|
@@ -4,13 +4,21 @@ import { join } from 'node:path'
|
|
|
4
4
|
import { describe, expect, test } from 'bun:test'
|
|
5
5
|
import { readMailSettings, updateMailSettings } from './mail-settings'
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
interface Fixture { cwd: string, env: string, privateKey: null, backupPath: string, envPath: string }
|
|
8
|
+
|
|
9
|
+
/** A project with `.env` and, optionally, more env files, read as the `staging` environment with no private key. */
|
|
10
|
+
async function fixture(content: string, more: Record<string, string> = {}): Promise<Fixture> {
|
|
11
|
+
const cwd = await mkdtemp(join(tmpdir(), 'stacks-dashboard-mail-'))
|
|
9
12
|
const options = {
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
cwd,
|
|
14
|
+
env: 'staging',
|
|
15
|
+
privateKey: null,
|
|
16
|
+
backupPath: join(cwd, 'runtime', 'environment.backup'),
|
|
17
|
+
envPath: join(cwd, '.env'),
|
|
12
18
|
}
|
|
13
19
|
await writeFile(options.envPath, content)
|
|
20
|
+
for (const [file, text] of Object.entries(more))
|
|
21
|
+
await writeFile(join(cwd, file), text)
|
|
14
22
|
return options
|
|
15
23
|
}
|
|
16
24
|
|
|
@@ -150,6 +158,38 @@ describe('dashboard mail settings', () => {
|
|
|
150
158
|
await expect(readMailSettings(invalidEncryption)).rejects.toThrow('MAIL_ENCRYPTION must be empty, tls, or ssl')
|
|
151
159
|
|
|
152
160
|
const malformedEnvironment = await fixture('MAIL_MAILER=log\nMAIL_MAILER=smtp\n')
|
|
153
|
-
await expect(readMailSettings(malformedEnvironment)).rejects.toThrow('environment file is invalid on line 2')
|
|
161
|
+
await expect(readMailSettings(malformedEnvironment)).rejects.toThrow('environment file is invalid on line 2 of .env')
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('reads a value from the env file the app reads it from, and saves it there (stacksjs/stacks#3340)', async () => {
|
|
165
|
+
const options = await fixture('MAIL_MAILER=log\nMAIL_FROM_ADDRESS=hello@example.com\nMAIL_HOST=base.example.com\n', {
|
|
166
|
+
'.env.staging': 'MAIL_MAILER=smtp\nMAIL_HOST=staging.example.com\n',
|
|
167
|
+
})
|
|
168
|
+
const current = await readMailSettings(options)
|
|
169
|
+
expect(current).toMatchObject({ driver: 'smtp', sources: ['.env', '.env.staging'], overridden: [] })
|
|
170
|
+
expect(current.smtp.host).toBe('staging.example.com')
|
|
171
|
+
|
|
172
|
+
const result = await updateMailSettings({
|
|
173
|
+
revision: current.revision,
|
|
174
|
+
driver: 'smtp',
|
|
175
|
+
fromName: 'Stacks',
|
|
176
|
+
fromAddress: 'hello@example.com',
|
|
177
|
+
smtp: { host: 'saved.example.com', port: '587', username: '', encryption: '', password: '' },
|
|
178
|
+
}, options)
|
|
179
|
+
expect('state' in result && result.state.smtp.host).toBe('saved.example.com')
|
|
180
|
+
const staging = await readFile(join(options.cwd, '.env.staging'), 'utf8')
|
|
181
|
+
expect(staging).toContain('MAIL_HOST=saved.example.com')
|
|
182
|
+
expect(staging).toContain('MAIL_MAILER=smtp')
|
|
183
|
+
expect(await readFile(options.envPath, 'utf8')).toContain('MAIL_HOST=base.example.com')
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('names the mail keys a later file keeps encrypted beyond this server\'s reach', async () => {
|
|
187
|
+
const options = await fixture('MAIL_MAILER=log\nMAIL_FROM_ADDRESS=hello@example.com\n', {
|
|
188
|
+
'.env.staging': 'MAIL_MAILER="encrypted:not-readable-here"\n',
|
|
189
|
+
})
|
|
190
|
+
const current = await readMailSettings(options)
|
|
191
|
+
expect(current.driver).toBe('log')
|
|
192
|
+
expect(current.sources).toEqual(['.env'])
|
|
193
|
+
expect(current.overridden).toEqual([{ key: 'MAIL_MAILER', file: '.env.staging' }])
|
|
154
194
|
})
|
|
155
195
|
})
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
parseEnvironmentEntries,
|
|
4
|
-
readEnvironmentFile,
|
|
5
|
-
updateEnvironmentEntries,
|
|
6
|
-
validateEnvironmentFile,
|
|
7
|
-
} from '../Infrastructure/environment-file'
|
|
8
|
-
import type { EnvironmentFileOptions } from '../Infrastructure/environment-file'
|
|
1
|
+
import { environmentLayerIssues, readEnvironmentLayers, updateEnvironmentLayers } from '../Infrastructure/environment-layers'
|
|
2
|
+
import type { EnvironmentLayersOptions } from '../Infrastructure/environment-layers'
|
|
9
3
|
|
|
10
4
|
export const MAIL_DRIVERS = [
|
|
11
5
|
'log',
|
|
@@ -21,6 +15,19 @@ export type MailDriver = typeof MAIL_DRIVERS[number]
|
|
|
21
15
|
|
|
22
16
|
export interface MailSettingsState {
|
|
23
17
|
revision: string
|
|
18
|
+
/**
|
|
19
|
+
* The env files the shown values come from, in the order the app loads
|
|
20
|
+
* them; `.env` when none sets any. A save writes each value back to the
|
|
21
|
+
* file it came from (stacksjs/stacks#3340).
|
|
22
|
+
*/
|
|
23
|
+
sources: string[]
|
|
24
|
+
/**
|
|
25
|
+
* Mail keys a later env file sets with ciphertext this server cannot
|
|
26
|
+
* decrypt. The app skips those entries here, so the values shown are the
|
|
27
|
+
* ones in use; wherever that file's key is present, its values apply
|
|
28
|
+
* instead of what is saved.
|
|
29
|
+
*/
|
|
30
|
+
overridden: Array<{ key: string, file: string }>
|
|
24
31
|
driver: MailDriver
|
|
25
32
|
fromName: string
|
|
26
33
|
fromAddress: string
|
|
@@ -88,6 +95,49 @@ const SECRET_KEYS = [
|
|
|
88
95
|
'MAILTRAP_TOKEN',
|
|
89
96
|
] as const
|
|
90
97
|
|
|
98
|
+
/** The form field each written key is entered in, so a refusal about it shows there. */
|
|
99
|
+
const KEY_FIELDS: Record<string, string> = {
|
|
100
|
+
MAIL_MAILER: 'driver',
|
|
101
|
+
MAIL_FROM_NAME: 'fromName',
|
|
102
|
+
MAIL_FROM_ADDRESS: 'fromAddress',
|
|
103
|
+
MAIL_HOST: 'smtp.host',
|
|
104
|
+
MAIL_PORT: 'smtp.port',
|
|
105
|
+
MAIL_USERNAME: 'smtp.username',
|
|
106
|
+
MAIL_ENCRYPTION: 'smtp.encryption',
|
|
107
|
+
AWS_SES_REGION: 'ses.region',
|
|
108
|
+
AWS_ACCESS_KEY_ID: 'ses.accessKeyId',
|
|
109
|
+
MAILGUN_DOMAIN: 'mailgun.domain',
|
|
110
|
+
MAILGUN_ENDPOINT: 'mailgun.endpoint',
|
|
111
|
+
MAILTRAP_HOST: 'mailtrap.host',
|
|
112
|
+
MAILTRAP_INBOX_ID: 'mailtrap.inboxId',
|
|
113
|
+
MAIL_PASSWORD: 'smtp.password',
|
|
114
|
+
AWS_SECRET_ACCESS_KEY: 'ses.secretAccessKey',
|
|
115
|
+
SENDGRID_API_KEY: 'sendgrid.apiKey',
|
|
116
|
+
MAILGUN_API_KEY: 'mailgun.apiKey',
|
|
117
|
+
MAILTRAP_TOKEN: 'mailtrap.token',
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Every env key the page reads. */
|
|
121
|
+
const MAIL_KEYS = [
|
|
122
|
+
'APP_NAME',
|
|
123
|
+
'MAIL_MAILER',
|
|
124
|
+
'MAIL_DRIVER',
|
|
125
|
+
'MAIL_FROM_NAME',
|
|
126
|
+
'MAIL_FROM_ADDRESS',
|
|
127
|
+
'MAIL_HOST',
|
|
128
|
+
'MAIL_PORT',
|
|
129
|
+
'MAIL_USERNAME',
|
|
130
|
+
'MAIL_ENCRYPTION',
|
|
131
|
+
'AWS_SES_REGION',
|
|
132
|
+
'AWS_DEFAULT_REGION',
|
|
133
|
+
'AWS_ACCESS_KEY_ID',
|
|
134
|
+
'MAILGUN_DOMAIN',
|
|
135
|
+
'MAILGUN_ENDPOINT',
|
|
136
|
+
'MAILTRAP_HOST',
|
|
137
|
+
'MAILTRAP_INBOX_ID',
|
|
138
|
+
...SECRET_KEYS,
|
|
139
|
+
] as const
|
|
140
|
+
|
|
91
141
|
function stringValue(value: unknown): string {
|
|
92
142
|
return typeof value === 'string' ? value.trim() : ''
|
|
93
143
|
}
|
|
@@ -125,18 +175,23 @@ function portValue(value: string | undefined): number {
|
|
|
125
175
|
return parsed
|
|
126
176
|
}
|
|
127
177
|
|
|
128
|
-
export async function readMailSettings(options:
|
|
129
|
-
const
|
|
130
|
-
const environmentIssues =
|
|
178
|
+
export async function readMailSettings(options: EnvironmentLayersOptions = {}): Promise<MailSettingsState> {
|
|
179
|
+
const layers = await readEnvironmentLayers(options)
|
|
180
|
+
const environmentIssues = environmentLayerIssues(layers)
|
|
131
181
|
if (environmentIssues.length) {
|
|
132
182
|
const issue = environmentIssues[0]!
|
|
133
|
-
const location = issue.line > 0 ? ` on line ${issue.line}` :
|
|
183
|
+
const location = issue.line > 0 ? ` on line ${issue.line} of ${issue.file}` : ` (${issue.file})`
|
|
134
184
|
throw new TypeError(`The environment file is invalid${location}: ${issue.message}`)
|
|
135
185
|
}
|
|
136
|
-
const entries =
|
|
186
|
+
const { entries } = layers
|
|
187
|
+
const read = MAIL_KEYS.filter(key => layers.sources[key])
|
|
188
|
+
const sources = layers.layers.map(layer => layer.file).filter(file => read.some(key => layers.sources[key] === file))
|
|
189
|
+
const overridden = MAIL_KEYS.flatMap(key => layers.unreadable[key] ? [{ key, file: layers.unreadable[key]! }] : [])
|
|
137
190
|
|
|
138
191
|
return {
|
|
139
|
-
revision:
|
|
192
|
+
revision: layers.revision,
|
|
193
|
+
sources: sources.length ? sources : ['.env'],
|
|
194
|
+
overridden,
|
|
140
195
|
driver: normalizedDriver(entries.MAIL_MAILER ?? entries.MAIL_DRIVER),
|
|
141
196
|
fromName: entries.MAIL_FROM_NAME ?? entries.APP_NAME ?? 'Stacks',
|
|
142
197
|
fromAddress: entries.MAIL_FROM_ADDRESS ?? '',
|
|
@@ -179,38 +234,16 @@ function validateSecret(value: unknown, field: string, fields: Record<string, st
|
|
|
179
234
|
return secret
|
|
180
235
|
}
|
|
181
236
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
)
|
|
189
|
-
|
|
190
|
-
const encryptedFile = Object.values(entries).some(entry =>
|
|
191
|
-
entry.startsWith('encrypted:') || entry.startsWith('enc:'),
|
|
192
|
-
)
|
|
193
|
-
if (!encryptedFile)
|
|
194
|
-
return value
|
|
195
|
-
|
|
196
|
-
const publicKey = entries.DOTENV_PUBLIC_KEY
|
|
197
|
-
if (!publicKey) {
|
|
198
|
-
fields[key] = 'The encrypted environment file has no public key. Repair it before changing secrets.'
|
|
199
|
-
return undefined
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
try {
|
|
203
|
-
return encryptValue(value, publicKey)
|
|
204
|
-
}
|
|
205
|
-
catch {
|
|
206
|
-
fields[key] = 'The secret could not be encrypted with the environment public key.'
|
|
207
|
-
return undefined
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
237
|
+
/**
|
|
238
|
+
* What to write for a secret: a new value, an empty one to clear it, or
|
|
239
|
+
* nothing to keep the stored one. Encrypting it, where its file is encrypted,
|
|
240
|
+
* is `updateEnvironmentLayers`'s job, under that file's own public key.
|
|
241
|
+
*/
|
|
242
|
+
function secretUpdate(value: string, clear: boolean): string | undefined {
|
|
243
|
+
if (value)
|
|
244
|
+
return value
|
|
211
245
|
if (clear)
|
|
212
246
|
return ''
|
|
213
|
-
|
|
214
247
|
return undefined
|
|
215
248
|
}
|
|
216
249
|
|
|
@@ -224,7 +257,7 @@ function validHost(value: string): boolean {
|
|
|
224
257
|
|
|
225
258
|
export async function updateMailSettings(
|
|
226
259
|
input: MailSettingsInput,
|
|
227
|
-
options:
|
|
260
|
+
options: EnvironmentLayersOptions = {},
|
|
228
261
|
): Promise<MailSettingsUpdateResult> {
|
|
229
262
|
const fields: Record<string, string> = {}
|
|
230
263
|
const revision = stringValue(input.revision)
|
|
@@ -241,11 +274,11 @@ export async function updateMailSettings(
|
|
|
241
274
|
if (!validEmail(fromAddress))
|
|
242
275
|
fields.fromAddress = 'Enter a valid sender email address.'
|
|
243
276
|
|
|
244
|
-
const
|
|
245
|
-
if (
|
|
277
|
+
const currentLayers = await readEnvironmentLayers(options)
|
|
278
|
+
if (currentLayers.revision !== revision)
|
|
246
279
|
return { conflict: true }
|
|
247
280
|
|
|
248
|
-
const currentEntries =
|
|
281
|
+
const currentEntries = currentLayers.entries
|
|
249
282
|
const updates: Record<string, string> = {
|
|
250
283
|
MAIL_MAILER: driver,
|
|
251
284
|
MAIL_FROM_NAME: fromName,
|
|
@@ -258,13 +291,7 @@ export async function updateMailSettings(
|
|
|
258
291
|
const username = stringValue(input.smtp?.username)
|
|
259
292
|
const encryption = stringValue(input.smtp?.encryption)
|
|
260
293
|
const password = validateSecret(input.smtp?.password, 'smtp.password', fields)
|
|
261
|
-
const nextPassword = secretUpdate(
|
|
262
|
-
currentEntries,
|
|
263
|
-
'MAIL_PASSWORD',
|
|
264
|
-
password,
|
|
265
|
-
input.smtp?.clearPassword === true,
|
|
266
|
-
fields,
|
|
267
|
-
)
|
|
294
|
+
const nextPassword = secretUpdate(password, input.smtp?.clearPassword === true)
|
|
268
295
|
|
|
269
296
|
if (!validHost(host))
|
|
270
297
|
fields['smtp.host'] = 'Enter a valid SMTP host.'
|
|
@@ -284,13 +311,7 @@ export async function updateMailSettings(
|
|
|
284
311
|
const region = stringValue(input.ses?.region)
|
|
285
312
|
const accessKeyId = stringValue(input.ses?.accessKeyId)
|
|
286
313
|
const secret = validateSecret(input.ses?.secretAccessKey, 'ses.secretAccessKey', fields)
|
|
287
|
-
const nextSecret = secretUpdate(
|
|
288
|
-
currentEntries,
|
|
289
|
-
'AWS_SECRET_ACCESS_KEY',
|
|
290
|
-
secret,
|
|
291
|
-
input.ses?.clearSecretAccessKey === true,
|
|
292
|
-
fields,
|
|
293
|
-
)
|
|
314
|
+
const nextSecret = secretUpdate(secret, input.ses?.clearSecretAccessKey === true)
|
|
294
315
|
|
|
295
316
|
if (!/^[a-z]{2}(?:-gov)?-[a-z]+-\d$/.test(region))
|
|
296
317
|
fields['ses.region'] = 'Enter a valid AWS region, such as us-east-1.'
|
|
@@ -302,13 +323,7 @@ export async function updateMailSettings(
|
|
|
302
323
|
}
|
|
303
324
|
else if (driver === 'sendgrid') {
|
|
304
325
|
const apiKey = validateSecret(input.sendgrid?.apiKey, 'sendgrid.apiKey', fields)
|
|
305
|
-
const nextApiKey = secretUpdate(
|
|
306
|
-
currentEntries,
|
|
307
|
-
'SENDGRID_API_KEY',
|
|
308
|
-
apiKey,
|
|
309
|
-
input.sendgrid?.clearApiKey === true,
|
|
310
|
-
fields,
|
|
311
|
-
)
|
|
326
|
+
const nextApiKey = secretUpdate(apiKey, input.sendgrid?.clearApiKey === true)
|
|
312
327
|
|
|
313
328
|
if (!apiKey && input.sendgrid?.clearApiKey !== true && !configured(currentEntries.SENDGRID_API_KEY))
|
|
314
329
|
fields['sendgrid.apiKey'] = 'Enter a SendGrid API key.'
|
|
@@ -321,13 +336,7 @@ export async function updateMailSettings(
|
|
|
321
336
|
const domain = stringValue(input.mailgun?.domain)
|
|
322
337
|
const endpoint = stringValue(input.mailgun?.endpoint)
|
|
323
338
|
const apiKey = validateSecret(input.mailgun?.apiKey, 'mailgun.apiKey', fields)
|
|
324
|
-
const nextApiKey = secretUpdate(
|
|
325
|
-
currentEntries,
|
|
326
|
-
'MAILGUN_API_KEY',
|
|
327
|
-
apiKey,
|
|
328
|
-
input.mailgun?.clearApiKey === true,
|
|
329
|
-
fields,
|
|
330
|
-
)
|
|
339
|
+
const nextApiKey = secretUpdate(apiKey, input.mailgun?.clearApiKey === true)
|
|
331
340
|
|
|
332
341
|
if (!validHost(domain))
|
|
333
342
|
fields['mailgun.domain'] = 'Enter the verified Mailgun domain.'
|
|
@@ -347,13 +356,7 @@ export async function updateMailSettings(
|
|
|
347
356
|
const host = stringValue(input.mailtrap?.host)
|
|
348
357
|
const inboxId = stringValue(input.mailtrap?.inboxId)
|
|
349
358
|
const token = validateSecret(input.mailtrap?.token, 'mailtrap.token', fields)
|
|
350
|
-
const nextToken = secretUpdate(
|
|
351
|
-
currentEntries,
|
|
352
|
-
'MAILTRAP_TOKEN',
|
|
353
|
-
token,
|
|
354
|
-
input.mailtrap?.clearToken === true,
|
|
355
|
-
fields,
|
|
356
|
-
)
|
|
359
|
+
const nextToken = secretUpdate(token, input.mailtrap?.clearToken === true)
|
|
357
360
|
|
|
358
361
|
// Both optional, as in the driver: an inbox id captures mail in that
|
|
359
362
|
// sandbox inbox, no inbox id delivers it through the Sending API, and a
|
|
@@ -386,14 +389,16 @@ export async function updateMailSettings(
|
|
|
386
389
|
if (Object.keys(fields).length)
|
|
387
390
|
return { validation: { fields } }
|
|
388
391
|
|
|
389
|
-
|
|
392
|
+
// Each value goes back to the env file the app reads it from, encrypted
|
|
393
|
+
// where that file keeps it encrypted (stacksjs/stacks#3340).
|
|
394
|
+
const result = await updateEnvironmentLayers(updates, revision, { ...options, secrets: SECRET_KEYS })
|
|
390
395
|
if (result.conflict)
|
|
391
396
|
return { conflict: true }
|
|
392
397
|
if (result.issues?.length) {
|
|
393
398
|
return {
|
|
394
399
|
validation: {
|
|
395
|
-
fields: Object.fromEntries(result.issues.map(
|
|
396
|
-
issue.
|
|
400
|
+
fields: Object.fromEntries(result.issues.map(issue => [
|
|
401
|
+
KEY_FIELDS[issue.key] ?? issue.key,
|
|
397
402
|
issue.message,
|
|
398
403
|
])),
|
|
399
404
|
},
|
package/ide/vscode/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"publisher": "Stacks",
|
|
3
3
|
"name": "vscode-stacks",
|
|
4
4
|
"displayName": "Stacks",
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.630",
|
|
6
6
|
"description": "Stacks, built into VS Code: stx templates, pickier linting and formatting, encrypted env files, the dev server preview and every buddy command. No other extensions required.",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/defaults",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.630",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
@@ -45,14 +45,14 @@
|
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@iconify-json/f7": "^1.2.2",
|
|
47
47
|
"@iconify-json/hugeicons": "^1.2.35",
|
|
48
|
-
"@stacksjs/mobile": "0.75.
|
|
48
|
+
"@stacksjs/mobile": "0.75.630",
|
|
49
49
|
"@stacksjs/sanitizer": "^0.2.908",
|
|
50
50
|
"@stacksjs/stx": "^0.2.908",
|
|
51
|
-
"@stacksjs/strings": "0.75.
|
|
51
|
+
"@stacksjs/strings": "0.75.630",
|
|
52
52
|
"@xterm/addon-fit": "^0.11.0",
|
|
53
53
|
"@xterm/xterm": "^6.0.0",
|
|
54
54
|
"ts-qr-codes": "^0.1.8",
|
|
55
|
-
"@stacksjs/support": "0.75.
|
|
56
|
-
"@stacksjs/browser": "0.75.
|
|
55
|
+
"@stacksjs/support": "0.75.630",
|
|
56
|
+
"@stacksjs/browser": "0.75.630"
|
|
57
57
|
}
|
|
58
58
|
}
|
|
@@ -5,6 +5,8 @@ type MailDriver = 'log' | 'smtp' | 'ses' | 'sendgrid' | 'mailgun' | 'mailtrap' |
|
|
|
5
5
|
|
|
6
6
|
interface MailSettings {
|
|
7
7
|
revision: string
|
|
8
|
+
sources: string[]
|
|
9
|
+
overridden: Array<{ key: string, file: string }>
|
|
8
10
|
driver: MailDriver
|
|
9
11
|
fromName: string
|
|
10
12
|
fromAddress: string
|
|
@@ -55,6 +57,8 @@ const encryptionOptions = [
|
|
|
55
57
|
]
|
|
56
58
|
|
|
57
59
|
const revision = state('')
|
|
60
|
+
const sources = state<string[]>(['.env'])
|
|
61
|
+
const overridden = state<Array<{ key: string, file: string }>>([])
|
|
58
62
|
const driver = state<MailDriver>('log')
|
|
59
63
|
const fromName = state('')
|
|
60
64
|
const fromAddress = state('')
|
|
@@ -178,6 +182,8 @@ function resetSecrets(): void {
|
|
|
178
182
|
|
|
179
183
|
function applySettings(settings: MailSettings): void {
|
|
180
184
|
revision.set(settings.revision)
|
|
185
|
+
sources.set(settings.sources)
|
|
186
|
+
overridden.set(settings.overridden)
|
|
181
187
|
driver.set(settings.driver)
|
|
182
188
|
fromName.set(settings.fromName)
|
|
183
189
|
fromAddress.set(settings.fromAddress)
|
|
@@ -310,7 +316,7 @@ onMount(() => void loadSettings())
|
|
|
310
316
|
</div>
|
|
311
317
|
<div class="p-4 bg-white dark:bg-neutral-900 border border-stone-200 rounded-xl dark:border-neutral-700">
|
|
312
318
|
<p class="font-medium text-stone-500 text-xs tracking-wide uppercase dark:text-neutral-400">Source</p>
|
|
313
|
-
<p class="mt-1 font-mono text-sm text-stone-900 dark:text-white"
|
|
319
|
+
<p class="mt-1 font-mono text-sm text-stone-900 dark:text-white" :text="sources().join(', ')"></p>
|
|
314
320
|
</div>
|
|
315
321
|
<div class="p-4 bg-white dark:bg-neutral-900 border border-stone-200 rounded-xl dark:border-neutral-700">
|
|
316
322
|
<p class="font-medium text-stone-500 text-xs tracking-wide uppercase dark:text-neutral-400">Configuration</p>
|
|
@@ -323,6 +329,12 @@ onMount(() => void loadSettings())
|
|
|
323
329
|
<Button :if="isLoading() === false && !revision()" class="mt-3" size="sm" variant="secondary" @click="loadSettings">Try again</Button>
|
|
324
330
|
</div>
|
|
325
331
|
|
|
332
|
+
<div :if="overridden().length" class="p-4 text-amber-800 text-sm dark:text-amber-200 bg-amber-50 dark:bg-amber-950/30 border border-amber-200 rounded-xl dark:border-amber-900" role="note" data-mail-overridden>
|
|
333
|
+
<p class="font-medium">Some values are also set, encrypted, in a file this server cannot decrypt.</p>
|
|
334
|
+
<p class="mt-1">This server uses the values shown here. Where that file's private key is present, its values apply instead, whatever is saved here:</p>
|
|
335
|
+
<p class="mt-1 font-mono text-xs" :text="overridden().map(entry => entry.key + ' in ' + entry.file).join(', ')"></p>
|
|
336
|
+
</div>
|
|
337
|
+
|
|
326
338
|
<div :if="successMessage()" class="p-4 text-emerald-700 text-sm dark:text-emerald-300 bg-emerald-50 dark:bg-emerald-950/30 border border-emerald-200 rounded-xl dark:border-emerald-900" role="status" :text="successMessage()"></div>
|
|
327
339
|
|
|
328
340
|
<form :if="revision()" class="space-y-6" @submit.prevent="saveSettings">
|
|
@@ -501,7 +513,7 @@ onMount(() => void loadSettings())
|
|
|
501
513
|
</section>
|
|
502
514
|
|
|
503
515
|
<div class="flex flex-col gap-3 items-start justify-between sm:flex-row sm:items-center">
|
|
504
|
-
<p class="text-stone-500 text-xs dark:text-neutral-400">Saving
|
|
516
|
+
<p class="text-stone-500 text-xs dark:text-neutral-400">Saving writes each value to the file it comes from, and keeps a machine-local backup of the previous .env content.</p>
|
|
505
517
|
<Button type="submit" :loading="isSaving()" :disabled="isBusy() || !isDirty()">Save mail settings</Button>
|
|
506
518
|
</div>
|
|
507
519
|
</form>
|