feat(pages): block link names and #name links in services-counter

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-03 09:18:15 +08:00
1 parent c20a0c105c
commit 1efdfe97d0
4 files changed
+56 -2

No files matched your search

@@ -4,7 +4,7 @@ Design: `design.md`. Every task is TDD. Work is serial in the main session, with
## 1. services-counter
- [ ] 1.1 `CmsBlock.anchor`; `CmsBlockValidator`: link-name pattern and uniqueness (`PagesBadAnchor`, `PagesAnchorTaken` on `blocks[i].anchor`), `#name` accepted by `safeLink`.
- [x] 1.1 `CmsBlock.anchor`; `CmsBlockValidator`: link-name pattern and uniqueness (`PagesBadAnchor`, `PagesAnchorTaken` on `blocks[i].anchor`), `#name` accepted by `safeLink`.
- Verify: `mvn -T 1C -B -pl solvway-services-counter -am verify`
## 2. Portal
@@ -25,6 +25,8 @@ public class CmsBlock {
private String imagePosition;
private String buttonLabel;
private String buttonUrl;
/** Optional link name: the block's id on the public page, the target of a #name link (add-page-anchor-links). */
private String anchor;
private List<CmsFaqItem> items;
/** A Snowflake value beyond JavaScript number precision: written as a string, read from either. */
@JsonSerialize(using = ToStringSerializer.class)
@@ -5,7 +5,9 @@ import com.solvway.services.counter.dto.CmsFaqItem;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.LongPredicate;
import java.util.regex.Pattern;
@@ -18,6 +20,8 @@ import java.util.regex.Pattern;
*/
public final class CmsBlockValidator {
private static final int MAX_BLOCKS = 30;
/** A block link name (add-page-anchor-links); a same-page link is "#" plus one. */
static final Pattern ANCHOR = Pattern.compile("[a-z0-9][a-z0-9-]{0,39}");
private static final Pattern SCRIPT = Pattern.compile("(?i)<\\s*script|\\son[a-z]+\\s*=|javascript\\s*:");
private CmsBlockValidator() {
@@ -30,12 +34,22 @@ public final class CmsBlockValidator {
if (blocks.size() > MAX_BLOCKS) {
throw refuse("blocks", "PagesTooManyBlocks");
}
Set<String> anchors = new HashSet<>();
for (int i = 0; i < blocks.size(); i++) {
CmsBlock b = blocks.get(i);
String path = "blocks[" + i + "]";
if (b == null || b.getType() == null) {
throw refuse(path + ".type", "PagesUnknownType");
}
String anchor = b.getAnchor();
if (anchor != null && !anchor.isEmpty()) {
if (!ANCHOR.matcher(anchor).matches()) {
throw refuse(path + ".anchor", "PagesBadAnchor");
}
if (!anchors.add(anchor)) {
throw refuse(path + ".anchor", "PagesAnchorTaken");
}
}
switch (b.getType()) {
case "HERO" -> {
image(b, path, ownImage);
@@ -132,13 +146,16 @@ public final class CmsBlockValidator {
}
/**
* A same-site path, https, mailto or tel link, at most 1000 characters. Browsers turn "\" into "/" and drop
* A same-site path, https, mailto or tel link, or "#" plus a block link name; at most 1000 characters. Browsers turn "\" into "/" and drop
* whitespace inside URLs, so "/\evil" would become protocol-relative: such characters are refused outright.
*/
static boolean safeLink(String url) {
if (url.length() > 1000 || url.chars().anyMatch(c -> c == '\\' || c <= 0x20 || c == 0x7f || Character.isSpaceChar(c) || c == 0xFEFF)) {
return false;
}
if (url.startsWith("#")) {
return ANCHOR.matcher(url.substring(1)).matches();
}
if (url.startsWith("//")) {
return false;
}
@@ -166,4 +166,39 @@ class CmsBlockValidatorTest {
refused(List.of(custom("x".repeat(200_001))), "blocks[0].html", "PagesTooLong");
refused(List.of(custom(" ")), "blocks[0].html", "PagesRequired");
}
@Test
void buttonsMayLinkToANamedBlockOnTheSamePage() {
for (String good : List.of("#signup", "#top", "#a", "#2-rooms")) {
CmsBlock b = hero();
b.setButtonLabel("Go");
b.setButtonUrl(good);
assertThatCode(() -> validate(List.of(b))).as(good).doesNotThrowAnyException();
}
for (String bad : List.of("#", "#Sign Up", "#-x", "#signup/x", "#" + "a".repeat(41))) {
CmsBlock b = hero();
b.setButtonLabel("Go");
b.setButtonUrl(bad);
refused(List.of(b), "blocks[0].buttonUrl", "PagesBadLink");
}
}
@Test
void linkNamesFollowThePatternAndAreUniqueOnThePage() {
CmsBlock named = form(OWN_EVENT);
named.setAnchor("signup");
CmsBlock other = rich("<p>x</p>");
other.setAnchor("a".repeat(40));
CmsBlock blank = rich("<p>y</p>");
blank.setAnchor("");
assertThatCode(() -> validate(List.of(hero(), named, other, blank))).as("a blank name is no name").doesNotThrowAnyException();
for (String bad : List.of("Sign Up", "-x", " x", "a".repeat(41), "sign_up")) {
CmsBlock b = rich("<p>x</p>");
b.setAnchor(bad);
refused(List.of(b), "blocks[0].anchor", "PagesBadAnchor");
}
CmsBlock again = custom("<p>x</p>");
again.setAnchor("signup");
refused(List.of(named, again), "blocks[1].anchor", "PagesAnchorTaken");
}
}