Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions modules/playground/components/playground-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ const PlaygroundEditor = ({
.padEnd(6, "0")
: "#30bced";

const sanitizeForCss = (value: unknown): string =>
typeof value === "string"
? value.replace(/[^a-zA-Z0-9 _\-#.]/g, "")
: "";

const SAFE_HEX_COLOR = /^[0-9a-fA-F]{6}$/;
const sanitizeColor = (value: unknown, fallback: string): string => {
const raw = typeof value === "string" ? value : "";
const stripped = raw.replace(/^#/, "");
return SAFE_HEX_COLOR.test(stripped) ? `#${stripped}` : fallback;
};

provider.awareness.setLocalStateField("user", {
name: session?.user?.name || "Anonymous",
color: userColor,
Expand All @@ -350,8 +362,8 @@ const PlaygroundEditor = ({

for (const [clientId, state] of states) {
if (state.user) {
const color = state.user.color || "orange";
const name = state.user.name || "Anonymous";
const color = sanitizeColor(state.user.color, "#30bced");
const name = sanitizeForCss(state.user.name) || "Anonymous";

css += `
.yRemoteSelection-${clientId} {
Expand Down Expand Up @@ -388,7 +400,7 @@ const PlaygroundEditor = ({
`;
}
}
styleEl.innerHTML = css;
styleEl.textContent = css;
};

provider.awareness.on("update", handleAwarenessUpdate);
Expand Down