Skip to content

Commit 3188662

Browse files
hollesseclaude
andcommitted
Centralize room name validation and add not-found page
Extract the room name regex into Room.NAME_REGEX as the single source of truth and reference it from all controllers. Validate the room name in IndexController before building the redirect target, and fall back to the start page for input that does not match. Add a friendly 404 page for paths that do not correspond to a valid room. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 56c77cf commit 3188662

5 files changed

Lines changed: 47 additions & 6 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package sh.mob.timer.web;
22

3+
import java.util.regex.Pattern;
34
import org.springframework.beans.factory.annotation.Value;
45
import org.springframework.stereotype.Controller;
56
import org.springframework.ui.Model;
@@ -12,6 +13,8 @@
1213
@RequestMapping({"/", ""})
1314
public class IndexController {
1415

16+
private static final Pattern VALID_ROOM = Pattern.compile(Room.NAME_REGEX);
17+
1518
private final RoomRepository roomRepository;
1619
private final String url;
1720

@@ -30,6 +33,9 @@ public String index(Model model) {
3033

3134
@PostMapping
3235
public String post(@ModelAttribute Form form) {
36+
if (form.room == null || !VALID_ROOM.matcher(form.room).matches()) {
37+
return "redirect:/";
38+
}
3339
return String.format("redirect:/%s", form.room);
3440
}
3541

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ final class Room {
1919

2020
private static final Logger log = LoggerFactory.getLogger(Room.class);
2121

22+
/** The only definition of what constitutes a valid room name. */
23+
public static final String NAME_REGEX = "[A-Za-z0-9-_]{1,64}";
24+
2225
public static final TimerRequest NULL_TIMER_REQUEST =
2326
new TimerRequest(0L, null, null, null, null);
2427
private final String name;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public RoomApiController(RoomRepository roomRepository, Clock clock, Stats stats
3232

3333
@GetMapping
3434
@RequestMapping(
35-
value = "/{roomId:[A-Za-z0-9-_]{1,64}}/events",
35+
value = "/{roomId:" + Room.NAME_REGEX + "}/events",
3636
produces = MediaType.TEXT_EVENT_STREAM_VALUE)
3737
public Flux<ServerSentEvent<Object>> getEventStream(
3838
@PathVariable String roomId, ServerHttpResponse response) {
@@ -70,7 +70,7 @@ public Flux<ServerSentEvent<Object>> getEventStream(
7070
return Flux.concat(initialHistory, keepAliveFlux.mergeWith(timerRequestFlux).mergeWith(goalRequestFlux));
7171
}
7272

73-
@PutMapping("/{roomId:[A-Za-z0-9-_]{1,64}}")
73+
@PutMapping("/{roomId:" + Room.NAME_REGEX + "}")
7474
@ResponseStatus(HttpStatus.ACCEPTED)
7575
public void publishEvent(@PathVariable String roomId, @RequestBody PutTimerRequest timerRequest) {
7676
var room = roomRepository.get(roomId);
@@ -98,7 +98,7 @@ public void publishEvent(@PathVariable String roomId, @RequestBody PutTimerReque
9898
}
9999
}
100100

101-
@PutMapping("/{roomId:[A-Za-z0-9-_]{1,64}}/goal")
101+
@PutMapping("/{roomId:" + Room.NAME_REGEX + "}/goal")
102102
@ResponseStatus(HttpStatus.ACCEPTED)
103103
public void putGoal(@PathVariable String roomId, @RequestBody PutGoalRequest goalRequest) {
104104
var room = roomRepository.get(roomId);
@@ -117,14 +117,14 @@ public void putGoal(@PathVariable String roomId, @RequestBody PutGoalRequest goa
117117
}
118118
}
119119

120-
@DeleteMapping("/{roomId:[A-Za-z0-9-_]{1,64}}/goal")
120+
@DeleteMapping("/{roomId:" + Room.NAME_REGEX + "}/goal")
121121
@ResponseStatus(HttpStatus.ACCEPTED)
122122
public void deleteGoal(@PathVariable String roomId, @RequestBody DeleteGoalRequest deleteGoalRequest) {
123123
var room = roomRepository.get(roomId);
124124
room.deleteGoal(deleteGoalRequest.user(), Instant.now(clock));
125125
}
126126

127-
@GetMapping("/{roomId:[A-Za-z0-9-_]{1,64}}/goal")
127+
@GetMapping("/{roomId:" + Room.NAME_REGEX + "}/goal")
128128
@ResponseStatus(HttpStatus.NO_CONTENT)
129129
public ResponseEntity<GoalResponse> getGoal(@PathVariable String roomId) {
130130
var room = roomRepository.get(roomId);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public RoomController(RoomRepository roomRepository) {
1919
}
2020

2121
@GetMapping
22-
@RequestMapping(value = "/{roomId:[A-Za-z0-9-_]{1,64}}")
22+
@RequestMapping(value = "/{roomId:" + Room.NAME_REGEX + "}")
2323
public String get(@PathVariable String roomId, Model model) {
2424
model.addAttribute("room", roomRepository.get(roomId));
2525
return "room";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en" data-bs-theme="light">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
7+
<link href="/bootstrap.min.css" rel="stylesheet">
8+
<script src="/bootstrap.bundle.min.js"></script>
9+
<link rel="icon" href="/favicon.ico" type="image/x-icon">
10+
11+
<title>Mob Timer</title>
12+
<script src="/darkmode.js"></script>
13+
</head>
14+
<body>
15+
16+
<div class="col-lg-8 mx-auto p-3 py-md-5">
17+
<main class="text-center">
18+
<a class="text-decoration-none" href="/"><h1>Mob Timer</h1></a>
19+
20+
<div class="alert alert-warning mt-5" role="alert">
21+
<h4 class="alert-heading">Page not found</h4>
22+
<p>This page does not exist. Room names may only contain letters, digits, <code>-</code> and <code>_</code> and must be at most 64 characters long.</p>
23+
</div>
24+
25+
<a class="btn btn-primary" href="/">Back to start page</a>
26+
</main>
27+
<footer th:replace="~{fragments/footer}"></footer>
28+
</div>
29+
30+
<div th:replace="~{fragments/github-corner}"></div>
31+
</body>
32+
</html>

0 commit comments

Comments
 (0)