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:
1 parent
c20a0c105c
commit
1efdfe97d0
4 files changed
+56
-2
No files matched your search
@@ -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)
|
||||
|
||||
+18
-1
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user