-
Notifications
You must be signed in to change notification settings - Fork 4
ENG-1427-Custom Query Builder Layout (HTML Templates, MVP0) #779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import React, { useMemo } from "react"; | ||
| import { compileTemplate, sanitizeHtml } from "~/utils/compileTemplate"; | ||
| import type { Result } from "~/utils/types"; | ||
|
|
||
| export const DEFAULT_TEMPLATE = `<ul> | ||
| {{#each results}} | ||
| <li>{{result.text}}</li> | ||
| {{/each}} | ||
| </ul>`; | ||
|
|
||
| type CustomViewProps = { | ||
| results: Result[]; | ||
| template?: string; | ||
| }; | ||
|
|
||
| const CustomView = ({ results, template = DEFAULT_TEMPLATE }: CustomViewProps) => { | ||
| const html = useMemo(() => { | ||
| const compiled = compileTemplate({ template, results }); | ||
| return sanitizeHtml({ html: compiled }); | ||
| }, [results, template]); | ||
|
|
||
| return ( | ||
| <div | ||
| className="roamjs-custom-results-view p-4" | ||
| dangerouslySetInnerHTML={{ __html: html }} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default CustomView; | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||||||||||||||||||||||||||||
| import type { Result } from "./types"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const EACH_BLOCK = /\{\{\s*#each\s+results\s*\}\}([\s\S]*?)\{\{\s*\/each\s*\}\}/; | ||||||||||||||||||||||||||||||||
| const RESULT_KEY = /\{\{\s*result\.([\w-]+)\s*\}\}/g; | ||||||||||||||||||||||||||||||||
| const RESULT_IF_CHANGED = /\{\{\s*resultIfChanged\.([\w-]+)\s*\}\}/g; | ||||||||||||||||||||||||||||||||
| const IF_CHANGED_BLOCK = | ||||||||||||||||||||||||||||||||
| /\{\{\s*#ifChanged\s+result\.([\w-]+)\s*\}\}([\s\S]*?)\{\{\s*\/ifChanged\s*\}\}/g; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const escapeHtml = (value: unknown): string => | ||||||||||||||||||||||||||||||||
| String(value ?? "") | ||||||||||||||||||||||||||||||||
| .replace(/&/g, "&") | ||||||||||||||||||||||||||||||||
| .replace(/</g, "<") | ||||||||||||||||||||||||||||||||
| .replace(/>/g, ">") | ||||||||||||||||||||||||||||||||
| .replace(/"/g, """); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const substituteResult = ({ | ||||||||||||||||||||||||||||||||
| body, | ||||||||||||||||||||||||||||||||
| result, | ||||||||||||||||||||||||||||||||
| previousResult, | ||||||||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||||||||
| body: string; | ||||||||||||||||||||||||||||||||
| result: Result; | ||||||||||||||||||||||||||||||||
| previousResult?: Result; | ||||||||||||||||||||||||||||||||
| }): string => | ||||||||||||||||||||||||||||||||
| body | ||||||||||||||||||||||||||||||||
| .replace(IF_CHANGED_BLOCK, (_, key: string, sectionBody: string) => { | ||||||||||||||||||||||||||||||||
| const currentValue = String(result[key] ?? ""); | ||||||||||||||||||||||||||||||||
| const previousValue = String(previousResult?.[key] ?? ""); | ||||||||||||||||||||||||||||||||
| return currentValue === previousValue ? "" : sectionBody; | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| .replace(RESULT_IF_CHANGED, (_, key: string) => { | ||||||||||||||||||||||||||||||||
| const currentValue = String(result[key] ?? ""); | ||||||||||||||||||||||||||||||||
| const previousValue = String(previousResult?.[key] ?? ""); | ||||||||||||||||||||||||||||||||
| return currentValue === previousValue ? "" : escapeHtml(result[key]); | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| .replace(RESULT_KEY, (_, key: string) => { | ||||||||||||||||||||||||||||||||
| return escapeHtml(result[key]); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| export const compileTemplate = ({ | ||||||||||||||||||||||||||||||||
| template, | ||||||||||||||||||||||||||||||||
| results, | ||||||||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||||||||
| template: string; | ||||||||||||||||||||||||||||||||
| results: Result[]; | ||||||||||||||||||||||||||||||||
| }): string => { | ||||||||||||||||||||||||||||||||
| let output = template; | ||||||||||||||||||||||||||||||||
| let match = output.match(EACH_BLOCK); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| while (match) { | ||||||||||||||||||||||||||||||||
| const [fullMatch, body] = match; | ||||||||||||||||||||||||||||||||
| const replacement = results | ||||||||||||||||||||||||||||||||
| .map((result, index) => | ||||||||||||||||||||||||||||||||
| substituteResult({ body, result, previousResult: results[index - 1] }), | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| .join(""); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| output = output.replace(fullMatch, replacement); | ||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Dollar-sign special replacement patterns in result data cause infinite loop or corrupted output When result data contains Root Cause and ImpactIn output = output.replace(fullMatch, replacement);where If any result value contains Verified with: const fullMatch = '{{#each results}}<li>{{result.text}}</li>{{/each}}';
const replacement = '<li>$&</li>'; // result.text was '$&'
const output = '<ul>{{#each results}}<li>{{result.text}}</li>{{/each}}</ul>';
output.replace(fullMatch, replacement);
// → '<ul><li>{{#each results}}<li>{{result.text}}</li>{{/each}}</li></ul>'
// The #each block is re-introduced → infinite loopImpact: Any query result containing
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||||||||||||||||||||||||
| match = output.match(EACH_BLOCK); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return output; | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| export const sanitizeHtml = ({ html }: { html: string }): string => { | ||||||||||||||||||||||||||||||||
| if (typeof document === "undefined") return html; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const container = document.createElement("div"); | ||||||||||||||||||||||||||||||||
| container.innerHTML = html; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| container | ||||||||||||||||||||||||||||||||
| .querySelectorAll("script, iframe, object, embed, style[type='text/javascript']") | ||||||||||||||||||||||||||||||||
| .forEach((el) => el.remove()); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| container.querySelectorAll("*").forEach((el) => { | ||||||||||||||||||||||||||||||||
| [...el.attributes].forEach((attr) => { | ||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||
| /^on\w+/i.test(attr.name) || | ||||||||||||||||||||||||||||||||
| ((attr.name === "href" || attr.name === "src") && | ||||||||||||||||||||||||||||||||
| /^\s*javascript:/i.test(attr.value)) | ||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||
| el.removeAttribute(attr.name); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+77
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 sanitizeHtml fails to strip javascript: URLs on formaction, action, and other URL-bearing attributes The Detailed explanation of the bypassThe attribute check at lines 59-65 is: (attr.name === "href" || attr.name === "src") &&
/^\s*javascript:/i.test(attr.value)This misses at least:
Since the rendered HTML is injected via Impact: XSS in shared query templates via attributes not covered by the sanitizer's allowlist.
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return container.innerHTML; | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent default template handling: The textarea uses
?? ""to default to empty string, but the actual CustomView rendering (lines 1525-1527) uses|| DEFAULT_TEMPLATE. Iflayout.templateis an array with empty string[""], the user sees an empty textarea but the renderer displays DEFAULT_TEMPLATE, creating a confusing mismatch.Fix: Use consistent logic:
Spotted by Graphite Agent

Is this helpful? React 👍 or 👎 to let us know.