Skip to content

Commit 31069b7

Browse files
hollesseclaude
andcommitted
Build request payloads with JSON.stringify and sanitize log output
Construct client JSON via JSON.stringify instead of template literals so usernames and goals containing quotes or backslashes are encoded correctly. Strip carriage returns and newlines from user and goal values before logging. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 3188662 commit 31069b7

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎src/main/java/sh/mob/timer/web/RoomApiController.java‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void publishEvent(@PathVariable String roomId, @RequestBody PutTimerReque
8181
log.info(
8282
"Add timer {} by user {} for room {}",
8383
timerRequest.timer,
84-
timerRequest.user,
84+
sanitizeForLog(timerRequest.user),
8585
room.name());
8686
stats.incrementTimer(room.name(), timer);
8787
} else if (timerRequest.breaktimer() != null) {
@@ -90,7 +90,7 @@ public void publishEvent(@PathVariable String roomId, @RequestBody PutTimerReque
9090
log.info(
9191
"Add break timer {} by user {} for room {}",
9292
timerRequest.breaktimer(),
93-
timerRequest.user,
93+
sanitizeForLog(timerRequest.user),
9494
room.name());
9595
stats.incrementBreaktimer(room.name(), breaktimer);
9696
} else {
@@ -108,8 +108,8 @@ public void putGoal(@PathVariable String roomId, @RequestBody PutGoalRequest goa
108108
goal, goalRequest.user(), Instant.now(clock));
109109
log.info(
110110
"Add goal \"{}\" by user {} for room {}",
111-
goalRequest.goal(),
112-
goalRequest.user(),
111+
sanitizeForLog(goalRequest.goal()),
112+
sanitizeForLog(goalRequest.user()),
113113
room.name());
114114
stats.incrementGoalCount(room.name());
115115
} else {
@@ -134,6 +134,13 @@ public ResponseEntity<GoalResponse> getGoal(@PathVariable String roomId) {
134134
return ResponseEntity.ofNullable(GoalResponse.of(room.currentGoal()));
135135
}
136136

137+
private static String sanitizeForLog(String value) {
138+
if (value == null) {
139+
return null;
140+
}
141+
return value.replaceAll("[\\r\\n]", "_");
142+
}
143+
137144
private static long truncateTooLongTimers(Long timer) {
138145
return Math.min(60 * 24, Math.max(0, timer));
139146
}

‎src/main/resources/templates/room.html‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ <h5>Integration with the mob tool</h5>
554554
function sendTimer(type, timer, user) {
555555
let method = "PUT";
556556
let url = "/" + room;
557-
let json = `{"${type}": ${timer},"user": "${user}"}`;
557+
let json = JSON.stringify({ [type]: timer, user: user });
558558

559559
console.log(`${method} ${url} ${json}`)
560560
var xhr = new XMLHttpRequest();
@@ -579,7 +579,7 @@ <h5>Integration with the mob tool</h5>
579579
let method = "PUT";
580580
let url = "/" + room + "/goal";
581581
const user = localStorage.getItem('user');
582-
let json = `{"goal": "${goal}","user": "${user}"}`;
582+
let json = JSON.stringify({ goal: goal, user: user });
583583

584584
console.log(`${method} ${url} ${json}`)
585585
var xhr = new XMLHttpRequest();
@@ -598,7 +598,7 @@ <h5>Integration with the mob tool</h5>
598598
let method = "DELETE";
599599
let url = "/" + room + "/goal";
600600
let user = localStorage.getItem('user');
601-
let json = `{"user": "${user}"}`;
601+
let json = JSON.stringify({ user: user });
602602

603603
console.log(`${method} ${url} ${json}`)
604604
var xhr = new XMLHttpRequest();

0 commit comments

Comments
 (0)