diff --git a/src/lib/config-doc.ts b/src/lib/config-doc.ts index bf743a27..92f87eb1 100644 --- a/src/lib/config-doc.ts +++ b/src/lib/config-doc.ts @@ -318,6 +318,18 @@ export function hideSecretValues(answer: T, ...texts: string[]): T { return walk(answer) as T; } +/** Mask complete INI values when they contain a credential, so they can be restored intact. */ +export function redactConfigDocument(text: string): string { + const redacted = redactSecrets(text); + const { eol, lines } = splitLines(redacted); + const masked = splitLines(hideSecretValues(redacted, text)).lines; + return lines.map((line, i) => { + if (line === masked[i]) return line; + const equals = line.indexOf('='); + return equals < 0 ? masked[i] : line.slice(0, equals + 1) + SECRET_PLACEHOLDER; + }).join(eol); +} + /** * The other half of redactSecrets: `text` came back from someone who was shown placeholders, and * every credential line still holding one gets the value that line has in `live` (the document as @@ -328,7 +340,12 @@ export function restoreSecrets(text: string, live: string): string { const { eol, lines } = splitLines(text); const held = secretLines(lines).filter((s) => s.value === SECRET_PLACEHOLDER); if (!held.length) return text; - const real = secretLines(splitLines(live).lines); + // Recreate the document the caller saw, and restore only values that the + // current server document actually masked, including non-credential fields. + const original = splitLines(live).lines; + const shown = redactConfigDocument(live); + const real = secretLines(splitLines(shown).lines).filter(s => s.value === SECRET_PLACEHOLDER) + .map(s => ({ ...s, value: original[s.index].slice(original[s.index].indexOf('=') + 1).trim() })); const taken = new Map(); for (const s of held) { const nth = taken.get(s.slot) ?? 0; diff --git a/src/lib/server/actions.test.ts b/src/lib/server/actions.test.ts index 865daf04..906d74ed 100644 --- a/src/lib/server/actions.test.ts +++ b/src/lib/server/actions.test.ts @@ -495,6 +495,16 @@ test('config hides the RCON password and the feed token from whoever reads it', expect(r.revision).toBe('r1'); }); +test('config edits preserve welcome text that contains a credential value', async () => { + const live = `${SESSION}\n.ServerWelcomeMessageLines="Welcome secret friend"\n${RCON}\nPassword=secret\n`; + const f = reservedClient({ route: false, text: live }); + const shown = ((await ACTIONS.config.run(f.client, {})) as any).text as string; + expect(shown).toContain('.ServerWelcomeMessageLines=(hidden)'); + expect(shown).not.toContain('secret'); + await ACTIONS.configApply.run(f.client, { text: shown.replace(SESSION, `${SESSION}\nRotationEnabled=True`), revision: 'r1' }); + expect(f.text()).toBe(live.replace(SESSION, `${SESSION}\nRotationEnabled=True`)); +}); + // CL-516586 reads the kill feed from the RCON block: each endpoint carries an address and a token. const WITH_FEED = `${RCON}\r\nPassword=hunter2\r\nbEventFeedEnabled=True\r\n!EventFeedEndpoints=ClearArray\r\n.EventFeedEndpoints=(Url="https://feed.example",Token="wkf_live")\r\n`; diff --git a/src/lib/server/actions.ts b/src/lib/server/actions.ts index 17ca7470..d1cde848 100644 --- a/src/lib/server/actions.ts +++ b/src/lib/server/actions.ts @@ -10,7 +10,7 @@ import { steamIdRefusal } from './steam'; import { reservedFromText, reservedIntoText } from '../reserved-doc'; import { saneScores } from '../format'; import { MAX_CHAT } from '../chat'; -import { hideSecretValues, redactSecrets, restoreSecrets, SECRET_PLACEHOLDER } from '../config-doc'; +import { hideSecretValues, redactConfigDocument, restoreSecrets, SECRET_PLACEHOLDER } from '../config-doc'; import { feedReportOf } from '../feed-report'; import { CHAT_MAX_LIMIT, CHAT_PAGE, chatReadOf, chatTermsOf } from '../chat-read'; @@ -619,7 +619,7 @@ export const ACTIONS: Record = { mutating: false, run: async (c) => { const doc = await readConfig(c); - return hideSecretValues({ ...doc, text: redactSecrets(doc.text) }, doc.text); + return { ...hideSecretValues({ ...doc, text: '' }, doc.text), text: redactConfigDocument(doc.text) }; } }, // Season 2's report on its kill feed (CL-516586 on; $lib/feed-report): on or not, and per