Skip to content

Commit 18116b7

Browse files
committed
fix(prettier-plugin-toml): harden config merge and narrow watch docs
Skip `__proto__`, `constructor` and `prototype` when deep merging a discovered config, so a `__proto__` key can no longer change the merged object's prototype. Also narrow the README and changeset claim: only edits to discovered configuration files are watched; a newly created project configuration applies on the next run.
1 parent 658048e commit 18116b7

4 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎.changeset/tombi-toml-formatter.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ The parser/printer is now powered by `@tombi-toml/wasm-lib`, which supports TOML
2424
- Node.js `v18.0.0` or later is required.
2525

2626
Tombi's schema lookup is always disabled, and discovered configuration files are
27-
watched so edits and newly created files are picked up without restarting the
28-
process.
27+
watched so edits to them are picked up without restarting the process.
2928

3029
See the `prettier-plugin-toml` README for the full list of supported options.

‎packages/toml/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ The rules are resolved in order, each step overriding the previous one:
7575
- Tombi's schema lookup is always disabled, so formatting stays offline and
7676
deterministic.
7777
- Discovered configuration files are cached per directory and polled (best
78-
effort), so edits and newly created files apply without restarting the
79-
process.
78+
effort), so edits to them apply without restarting the process. A newly
79+
created project configuration applies on the next run.
8080

8181
## Parser Options
8282

‎packages/toml/src/config.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,16 @@ export function getTombiOverrides(options: PrettierOptions): TombiConfig {
185185
const isPlainObject = (value: unknown): value is Record<string, unknown> =>
186186
typeof value === 'object' && value !== null && !Array.isArray(value)
187187

188+
/** Keys that could mutate the prototype chain when assigned to an object. */
189+
const UNSAFE_KEYS = new Set(['__proto__', 'constructor', 'prototype'])
190+
188191
/** Recursively merge configs, later ones taking precedence. */
189192
const deepMerge = (...configs: TombiConfig[]): TombiConfig =>
190193
configs.reduce<TombiConfig>((result, config) => {
191194
for (const [key, value] of Object.entries(config)) {
195+
if (UNSAFE_KEYS.has(key)) {
196+
continue
197+
}
192198
result[key] =
193199
isPlainObject(value) && isPlainObject(result[key])
194200
? deepMerge(result[key], value)

‎packages/toml/test/config.spec.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stringify } from 'smol-toml'
1+
import { parse, stringify } from 'smol-toml'
22

33
import {
44
getTombiConfig,
@@ -99,4 +99,15 @@ describe('tombi config', () => {
9999
enabled: false,
100100
})
101101
})
102+
103+
it('should ignore prototype-sensitive keys', () => {
104+
const merged = mergeTombiConfig(
105+
getTombiConfig(options),
106+
parse('__proto__ = { polluted = true }\n'),
107+
getTombiOverrides(options),
108+
)
109+
110+
// Would be `true` if `__proto__` had been merged as the prototype.
111+
expect(merged.polluted).toBeUndefined()
112+
})
102113
})

0 commit comments

Comments
 (0)