Code RoomSettings patch creates arbitrary keys
HardPrep Room Coding #2075

Settings patch creates arbitrary keys

Code reviewSecuritySenior–Staff~24 min

Review this Node settings updater that applies a client-supplied dot-path patch.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 24 min
Mark a line and say what kind of problem it is.0 findings
1function applyPatch(target, patch) {
2 for (const { path, value } of patch) {
3 const keys = path.split('.');
4 let obj = target;
5 for (let i = 0; i < keys.length - 1; i++) {
6 const k = keys[i];
7 if (!(k in obj)) obj[k] = {};
8 obj = obj[k];
9 }
10 obj[keys[keys.length - 1]] = value;
11 }
12 return target;
13}
14 
15app.patch('/settings', (req, res) => {
16 applyPatch(currentSettings, req.body.ops);
17 res.json(currentSettings);
18});
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.