Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/lib/config-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ export function hideSecretValues<T>(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
Expand All @@ -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<string, number>();
for (const s of held) {
const nth = taken.get(s.slot) ?? 0;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/server/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -619,7 +619,7 @@ export const ACTIONS: Record<string, ActionDef> = {
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
Expand Down