fix(pages): reserve the reg- prefix for the sign-up form's field ids

A block named reg-email would have duplicated the form's input id, so its
label and #reg-email links would bind to the wrong element.

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:43:23 +08:00
1 parent 93acebf162
commit 92e62fb3fd
13 files changed
+25 -20

No files matched your search

@@ -24,7 +24,7 @@ Marketing wants a button near the top of a landing page to jump to the sign-up f
## Design
- **Link name.** Each block has an optional "Link name": lowercase letters, digits and `-`, starting with a letter or digit, at most 40 characters, unique on the page. services-counter checks it. The public page renders it as the block's `id`.
- **Link name.** Each block has an optional "Link name": lowercase letters, digits and `-`, starting with a letter or digit and not with `reg-` (the sign-up form's field ids), at most 40 characters, unique on the page. services-counter checks it. The public page renders it as the block's `id`.
- **`#name` links.** Button links and rich-text links also accept `#` followed by a name of that pattern. The same rule is in services-counter, the portal's `isSafeLink`, the rich-text editor and the public sanitiser.
- **Frame bridge.** The frame document's injected script, which already reports its height, also catches clicks on `#name` links inside the frame and posts the name and, when the frame holds an element with that `id`, the element's offset. The page acts on such a message only from its own frame and only for a name of the pattern. It scrolls to the frame's top plus that offset when the frame has the target; otherwise to a block on the page with that link name; otherwise it does nothing. The frame link's default action is prevented, so the frame never navigates.
+2 -2
View File
@@ -1631,8 +1631,8 @@ INSERT INTO message (code, content, area) SELECT 'PagesAnchor', JSON_OBJECT('en_
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '链接名称(可选)') WHERE LOWER(code) = LOWER('PagesAnchor') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesAnchorHint', JSON_OBJECT('en_US', 'Link to this block with #name, for example #signup. Lowercase letters, digits and - only.', 'zh_CN', '用 #名称 链接到此区块,例如 #signup。仅限小写字母、数字和 -。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesAnchorHint') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '用 #名称 链接到此区块,例如 #signup。仅限小写字母、数字和 -。') WHERE LOWER(code) = LOWER('PagesAnchorHint') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesBadAnchor', JSON_OBJECT('en_US', 'Use lowercase letters, digits and - (up to 40), starting with a letter or digit.', 'zh_CN', '请使用小写字母、数字和 -(最多 40 个),并以字母或数字开头。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesBadAnchor') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '请使用小写字母、数字和 -(最多 40 个),并以字母或数字开头。') WHERE LOWER(code) = LOWER('PagesBadAnchor') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesBadAnchor', JSON_OBJECT('en_US', 'Use lowercase letters, digits and - (up to 40), starting with a letter or digit and not with reg-.', 'zh_CN', '请使用小写字母、数字和 -(最多 40 个),以字母或数字开头,且不能以 reg- 开头。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesBadAnchor') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '请使用小写字母、数字和 -(最多 40 个),以字母或数字开头,且不能以 reg- 开头。') WHERE LOWER(code) = LOWER('PagesBadAnchor') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesAnchorTaken', JSON_OBJECT('en_US', 'Another block on this page already has this link name.', 'zh_CN', '本页已有其他区块使用此链接名称。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesAnchorTaken') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '本页已有其他区块使用此链接名称。') WHERE LOWER(code) = LOWER('PagesAnchorTaken') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesScript', JSON_OBJECT('en_US', 'Scripts and event handlers are not allowed.', 'zh_CN', '不允许使用脚本或事件处理程序。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesScript') AND area = 'A');
@@ -10,7 +10,7 @@ See `docs/superpowers/specs/2026-12-03-page-anchor-links-design.md`. Blocks are
## Decisions
1. **Field `anchor` on the block**, pattern `^[a-z0-9][a-z0-9-]{0,39}$`, unique per page. Stored in the blocks JSON, so no DDL. Refusal codes: `PagesBadAnchor`, `PagesAnchorTaken`.
1. **Field `anchor` on the block**, pattern `^(?!reg-)[a-z0-9][a-z0-9-]{0,39}$`, unique per page. `reg-` is reserved because the sign-up form renders its fields with literal `reg-*` ids. Stored in the blocks JSON, so no DDL. Refusal codes: `PagesBadAnchor`, `PagesAnchorTaken`.
2. **Link rule.** `#` plus the same pattern is a safe link everywhere a link is checked: `CmsBlockValidator.safeLink`, `isSafeLink`, the editor's `SAFE_LINK`, and the sanitiser (sanitize-html already keeps scheme-less hrefs; a test pins it).
3. **Ids.** The block wrapper in `CmsBlocks` gets `id={anchor}` for every type, the HTML frame included. Rendered only when the name matches the pattern, so rows written another way cannot inject odd ids.
4. **Frame bridge.** The injected script adds one click listener: for `a[href^="#"]` with a valid name it prevents the default and posts `{type:"colite-frame-anchor", name, y}`, where `y` is the in-frame element's document offset or `null`. `HtmlFrame` checks the source window as for heights, validates the message in a pure `frameAnchor()` helper, then scrolls the window to `frame top + y`, or to `document.getElementById(name)`, else ignores it. Smooth scroll.
@@ -19,7 +19,7 @@ See `docs/superpowers/specs/2026-12-03-page-anchor-links-design.md`. Blocks are
## Risks / Trade-offs
- A frame script could post anchor requests at will → it can only scroll its own page, which it could already make tall. Accepted.
- A link name equal to an id the portal already uses on that page (e.g. `main`) would clash → the public page renders no ids of its own apart from block anchors; the preview shell may, so only behaviour on the public page is specified.
- A link name equal to an id the portal already uses on that page (e.g. `main`) would clash → the sign-up form's field ids all start with `reg-`, which link names may not; other ids on the page come from `useId` and cannot match the pattern. The staff preview runs inside the back-office shell, whose ids are not checked against link names.
- Pages saved by an older release lose link names → only on rollback; noted in the proposal.
## Migration Plan
@@ -33,7 +33,7 @@ A button SHALL have both a label and a link or neither. The link SHALL start wit
### Requirement: Block Link Names
Any block MAY carry a link name. A link name SHALL be 1 to 40 characters of lowercase letters, digits and `-`, starting with a letter or digit, and SHALL be unique within the page. A save breaking either rule SHALL be refused on that block's link-name field, writing nothing. The public page and the staff preview SHALL give the block that name as its element id, so a `#name` link scrolls to it.
Any block MAY carry a link name. A link name SHALL be 1 to 40 characters of lowercase letters, digits and `-`, starting with a letter or digit, SHALL NOT start with `reg-` (the sign-up form's own field ids), and SHALL be unique within the page. A save breaking either rule SHALL be refused on that block's link-name field, writing nothing. The public page and the staff preview SHALL give the block that name as its element id, so a `#name` link scrolls to it.
#### Scenario: Hero button jumps to the form
@@ -46,6 +46,11 @@ Any block MAY carry a link name. A link name SHALL be 1 to 40 characters of lowe
- **WHEN** a page is saved with two blocks named `signup`
- **THEN** the save is refused on field `blocks[1].anchor` and nothing is written
#### Scenario: Reserved link name
- **WHEN** a page is saved with a block named `reg-email`
- **THEN** the save is refused on field `blocks[0].anchor` and nothing is written
#### Scenario: Bad link name
- **WHEN** a page is saved with a block named `Sign Up`
@@ -9,8 +9,8 @@ INSERT INTO message (code, content, area) SELECT 'PagesAnchor', JSON_OBJECT('en_
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '链接名称(可选)') WHERE LOWER(code) = LOWER('PagesAnchor') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesAnchorHint', JSON_OBJECT('en_US', 'Link to this block with #name, for example #signup. Lowercase letters, digits and - only.', 'zh_CN', '用 #名称 链接到此区块,例如 #signup。仅限小写字母、数字和 -。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesAnchorHint') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '用 #名称 链接到此区块,例如 #signup。仅限小写字母、数字和 -。') WHERE LOWER(code) = LOWER('PagesAnchorHint') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesBadAnchor', JSON_OBJECT('en_US', 'Use lowercase letters, digits and - (up to 40), starting with a letter or digit.', 'zh_CN', '请使用小写字母、数字和 -(最多 40 个),并以字母或数字开头。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesBadAnchor') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '请使用小写字母、数字和 -(最多 40 个),并以字母或数字开头。') WHERE LOWER(code) = LOWER('PagesBadAnchor') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesBadAnchor', JSON_OBJECT('en_US', 'Use lowercase letters, digits and - (up to 40), starting with a letter or digit and not with reg-.', 'zh_CN', '请使用小写字母、数字和 -(最多 40 个),以字母或数字开头,且不能以 reg- 开头。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesBadAnchor') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '请使用小写字母、数字和 -(最多 40 个),以字母或数字开头,且不能以 reg- 开头。') WHERE LOWER(code) = LOWER('PagesBadAnchor') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesAnchorTaken', JSON_OBJECT('en_US', 'Another block on this page already has this link name.', 'zh_CN', '本页已有其他区块使用此链接名称。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesAnchorTaken') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '本页已有其他区块使用此链接名称。') WHERE LOWER(code) = LOWER('PagesAnchorTaken') AND area = 'A';
-- PagesLinkPrompt and PagesBadLink already exist: their English changes too.
@@ -32,8 +32,8 @@ describe("cmsPages helpers", () => {
it("a same-page link is # plus a block link name", () => {
for (const ok of ["#signup", "#top", "#2-rooms", "#" + "a".repeat(40)]) expect(isSafeLink(ok), ok).toBe(true);
for (const bad of ["#", "#Sign Up", "#-x", "#signup/x", "#a\"b", "#" + "a".repeat(41)]) expect(isSafeLink(bad), bad).toBe(false);
for (const bad of ["#", "#Sign Up", "#-x", "#signup/x", "#a\"b", "#" + "a".repeat(41), "#reg-consent"]) expect(isSafeLink(bad), bad).toBe(false);
expect(anchorId("signup")).toBe("signup");
for (const bad of [undefined, "", "Sign Up", "x\" onclick=\"y"]) expect(anchorId(bad), String(bad)).toBeUndefined();
for (const bad of [undefined, "", "Sign Up", "x\" onclick=\"y", "reg-email"]) expect(anchorId(bad), String(bad)).toBeUndefined();
});
});
+2 -2
View File
@@ -133,8 +133,8 @@ export function contrastText(hex: string): "#000000" | "#FFFFFF" {
return (lum + 0.05) / 0.05 > 1.05 / (lum + 0.05) ? "#000000" : "#FFFFFF";
}
/** A block link name, as services-counter checks it. */
export const ANCHOR = /^[a-z0-9][a-z0-9-]{0,39}$/;
/** A block link name, as services-counter checks it. "reg-" is reserved: the sign-up form's fields use it as ids. */
export const ANCHOR = /^(?!reg-)[a-z0-9][a-z0-9-]{0,39}$/;
/** The link name when it is valid, for use as an element id. */
export const anchorId = (name: string | undefined) => (name && ANCHOR.test(name) ? name : undefined);
@@ -60,7 +60,7 @@ describe("frameHtml", () => {
expect(frameAnchor({ type: "colite-frame-anchor", name: "signup", y: null })).toEqual({ name: "signup", y: null });
expect(frameAnchor({ type: "colite-frame-anchor", name: "top", y: 120.4 })).toEqual({ name: "top", y: 120 });
expect(frameAnchor({ type: "colite-frame-anchor", name: "top", y: -5 })).toEqual({ name: "top", y: 0 });
for (const junk of [null, "x", { type: "colite-frame-height", height: 5 }, { type: "colite-frame-anchor", name: "Sign Up", y: null },
for (const junk of [null, "x", { type: "colite-frame-height", height: 5 }, { type: "colite-frame-anchor", name: "Sign Up", y: null }, { type: "colite-frame-anchor", name: "reg-email", y: null },
{ type: "colite-frame-anchor", name: "a", y: "1" }, { type: "colite-frame-anchor", name: "a", y: Infinity }, { type: "colite-frame-anchor", y: null }]) {
expect(frameAnchor(junk), JSON.stringify(junk)).toBeNull();
}
+1 -1
View File
@@ -12,7 +12,7 @@ const MAX_HEIGHT = 20000;
export const FRAME_ANCHOR_MESSAGE = "colite-frame-anchor";
/** A block link name, as in cmsPages (kept here as a string for the frame script). */
const ANCHOR_SOURCE = "^[a-z0-9][a-z0-9-]{0,39}$";
const ANCHOR_SOURCE = "^(?!reg-)[a-z0-9][a-z0-9-]{0,39}$";
const ANCHOR = new RegExp(ANCHOR_SOURCE);
// Posts the document height to the parent page on load and whenever it changes. A click on a #name link
@@ -828,7 +828,7 @@ export const DEFAULT_MESSAGES: Record<string, string> = {
PagesBadLink: "Use a link that starts with https://, mailto:, tel:, / or #.",
PagesAnchor: "Link name (optional)",
PagesAnchorHint: "Link to this block with #name, for example #signup. Lowercase letters, digits and - only.",
PagesBadAnchor: "Use lowercase letters, digits and - (up to 40), starting with a letter or digit.",
PagesBadAnchor: "Use lowercase letters, digits and - (up to 40), starting with a letter or digit and not with reg-.",
PagesAnchorTaken: "Another block on this page already has this link name.",
PagesScript: "Scripts and event handlers are not allowed.",
PagesBadImage: "Upload the image again.",
@@ -812,7 +812,7 @@
"PagesBadLink": "链接须以 https://、mailto:、tel:、/ 或 # 开头。",
"PagesAnchor": "链接名称(可选)",
"PagesAnchorHint": "用 #名称 链接到此区块,例如 #signup。仅限小写字母、数字和 -。",
"PagesBadAnchor": "请使用小写字母、数字和 -(最多 40 个),并以字母或数字开头。",
"PagesBadAnchor": "请使用小写字母、数字和 -(最多 40 个),以字母或数字开头,且不能以 reg- 开头。",
"PagesAnchorTaken": "本页已有其他区块使用此链接名称。",
"PagesScript": "不允许使用脚本或事件处理程序。",
"PagesBadImage": "请重新上传图片。",
@@ -20,8 +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}");
/** A block link name (add-page-anchor-links); a same-page link is "#" plus one. "reg-" is the sign-up form's own ids. */
static final Pattern ANCHOR = Pattern.compile("(?!reg-)[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() {
@@ -175,7 +175,7 @@ class CmsBlockValidatorTest {
b.setButtonUrl(good);
assertThatCode(() -> validate(List.of(b))).as(good).doesNotThrowAnyException();
}
for (String bad : List.of("#", "#Sign Up", "#-x", "#signup/x", "#" + "a".repeat(41))) {
for (String bad : List.of("#", "#Sign Up", "#-x", "#signup/x", "#" + "a".repeat(41), "#reg-consent")) {
CmsBlock b = hero();
b.setButtonLabel("Go");
b.setButtonUrl(bad);
@@ -192,7 +192,7 @@ class CmsBlockValidatorTest {
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")) {
for (String bad : List.of("Sign Up", "-x", " x", "a".repeat(41), "sign_up", "reg-email")) {
CmsBlock b = rich("<p>x</p>");
b.setAnchor(bad);
refused(List.of(b), "blocks[0].anchor", "PagesBadAnchor");