athena-ui-js 2.7.0 → 2.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,7 +5,7 @@ layout (header, menu, content wrapper, footer) from a single `init()` call, load
5
5
  and theme, and ships the building blocks of a multipurpose application: forms, tables, charts,
6
6
  feedback, navigation, a router and more.
7
7
 
8
- Current version: **2.7.0**
8
+ Current version: **2.7.1**
9
9
 
10
10
  [Quick start](#usage) · [API](#api) · [Components](#charts) · [Themes](#themes) · [Tests](#tests) · [Release notes](#releases)
11
11
 
@@ -517,11 +517,27 @@ editor.getValue(); // safe HTML, "" when empty
517
517
  The date range uses the native date pickers; the end picker starts at the chosen start date and a reversed range typed by
518
518
  hand is reported. The rich text editor produces a restricted set of HTML (paragraphs, headings, bold / italic / underline /
519
519
  strike, lists, quotes, code, links). Everything else (scripts, styles, iframes, images, event attributes, `javascript:` and
520
- `data:` links) is removed when the content is set, pasted, dropped and read, so the value is safe to store and show with
521
- `innerHTML`; links get `rel="noopener noreferrer"`. Toolbar buttons: `bold`, `italic`, `underline`, `strike`, `h2`, `h3`,
520
+ `data:` links) is removed when the content is set, pasted, dropped and read; links get `rel="noopener noreferrer"`. This
521
+ is a layer of defense in the browser, not a guarantee for what you store: see [Sanitizing HTML](#sanitizing-html). Toolbar buttons: `bold`, `italic`, `underline`, `strike`, `h2`, `h3`,
522
522
  `quote`, `ul`, `ol`, `link`, `unlink`, `clear`, `undo`, `redo`. The editor is built on `contenteditable` and
523
523
  `document.execCommand`, which every browser still supports; sanitize on the server as well, as with any HTML you accept.
524
524
 
525
+ ##### Sanitizing HTML
526
+
527
+ The sanitizer (`sanitizeHtml`, `isSafeUrl`) is an allow-list that rebuilds the markup: the input is parsed in an inert
528
+ document, only the listed elements are copied (`p`, `br`, `strong`, `em`, `u`, `s`, lists, headings `h1` to `h4`,
529
+ `blockquote`, `code`, `pre`, `a`, `hr`, `sub`, `sup`), every attribute is dropped except a checked `href`, and scripts,
530
+ styles, frames, SVG, MathML, forms and the like are removed with their content. A link may point to `http(s):`, `mailto:`,
531
+ `tel:`, `#...` or a path (`/path`, `./a`, `../a`); a path that a browser would read as another site (`//host`, and `/\host`,
532
+ because browsers treat a backslash in an http(s) URL as a slash) is refused.
533
+
534
+ **Treat it as defense in depth.** It runs in the page of the person who is typing, which an attacker controls: a request that
535
+ bypasses the page, or a stored value from another source, was never sanitized. So **sanitize again on the server before you
536
+ store or render content that came from users**, with a maintained server-side sanitizer, and keep a Content-Security-Policy
537
+ without `'unsafe-inline'` (Athena UI needs none). The sanitizer relies on the parser of the browser it runs in.
538
+ `htmlToText` returns text only. The regression tests (`tests/browser`) feed it malformed markup, SVG and MathML, DOM-clobbering
539
+ names and dangerous URL schemes.
540
+
525
541
  ```js
526
542
  generateDropZone({ parent: "wrapper", accept: "image/*", maxSize: 2 * 1024 * 1024, maxFiles: 3,
527
543
  onDrop: files => {}, onReject: rejected => rejected.forEach(({ file, reason }) => {}) });
@@ -888,6 +904,12 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
888
904
 
889
905
  ## Releases
890
906
 
907
+ ### 2.7.1
908
+
909
+ - **Security fix in `isSafeUrl` / the sanitizer:** a link such as `/\evil.com` was accepted as a path of the same site, but browsers read a backslash in an http(s) URL as a slash, so it led to `evil.com` (a phishing or redirect link; no script ran). It is refused now, like `//host`.
910
+ - README: the section *Sanitizing HTML* says what the sanitizer does and that it is defense in depth: sanitize again on the server.
911
+ - New sanitizer regression cases (hostile markup, SVG/MathML, DOM clobbering, URL schemes) in the browser suite.
912
+
891
913
  ### 2.7.0
892
914
  (what moving Heimdall onto Athena UI 2.x showed was missing; each item removed a workaround of that application)
893
915
  #### Added:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "athena-ui-js",
3
- "version": "2.7.0",
3
+ "version": "2.7.1",
4
4
  "description": "A small, dependency-free UI framework: forms, tables, charts, menus, dialogs, themes and a router, written as ES modules.",
5
5
  "type": "module",
6
6
  "main": "./src/athena-ui.js",
package/src/athena-ui.js CHANGED
@@ -2,7 +2,7 @@
2
2
  //Author: Csaba Tóth https://github.com/csabatibortoth/
3
3
  //LICENCE: MIT
4
4
  //Repository: https://github.com/csabatibortoth/athena-ui
5
- //Version: 2.7.0
5
+ //Version: 2.7.1
6
6
 
7
7
  // The whole API as ES module exports. There are no side effects: importing this file does not touch the page, does not load a script and does not
8
8
  // put anything on globalThis. For pages that need globals use `athena-ui-js/classic`, and to download only what you use import from the entry points
@@ -6,7 +6,7 @@
6
6
  export const BORDERED_CLASS = "default-cointainer-bordered";
7
7
 
8
8
  export const defaults = Object.freeze({
9
- version: "2.7.0",
9
+ version: "2.7.1",
10
10
  container_class: "container",
11
11
  header_class: "header-container",
12
12
  header_menu_class: "header-menu menu",
@@ -1,65 +1,67 @@
1
- //sanitize for Athena UI
2
- //Author: Csaba Tóth https://github.com/csabatibortoth/
3
- //LICENCE: MIT
4
- //Repository: https://github.com/csabatibortoth/athena-ui
5
-
6
- // Allow-list HTML sanitizer for the rich text editor. Everything that is not listed is unwrapped
7
- // (its text stays), dangerous elements are removed with their content, and every attribute is dropped
8
- // except a checked `href` on links. The input is parsed in an inert document, so nothing runs
9
- // while it is being cleaned.
10
-
11
- const ALLOWED = new Set([
12
- "p", "br", "b", "strong", "i", "em", "u", "s", "strike", "del", "ul", "ol", "li",
13
- "h1", "h2", "h3", "h4", "blockquote", "code", "pre", "a", "hr", "sub", "sup"
14
- ]);
15
- const RENAMED = { div: "p", strike: "s", del: "s", b: "strong", i: "em" };
16
- const DROPPED = new Set(["script", "style", "iframe", "object", "embed", "template", "noscript", "svg", "math", "link", "meta", "title", "form", "textarea", "select", "button", "input", "head"]);
17
- const SAFE_URL = /^(?:https?:|mailto:|tel:|#|\/(?!\/)|\.{1,2}\/)/i;
18
-
19
- /** True for http(s), mailto, tel, anchors and relative paths. Rejects javascript:, data:, vbscript: and the like. */
20
- export function isSafeUrl(url) {
21
- // Browsers ignore control characters and spaces inside the scheme ("java\tscript:").
22
- const compact = String(url ?? "").replace(/[\u0000- \u007f-\u009f]/g, "");
23
- return compact !== "" && SAFE_URL.test(compact);
24
- }
25
-
26
- function clean(node, target, doc) {
27
- for (const child of [...node.childNodes]) {
28
- if (child.nodeType === 3) {
29
- target.append(doc.createTextNode(child.nodeValue));
30
- } else if (child.nodeType === 1) {
31
- const tag = child.tagName.toLowerCase();
32
- if (DROPPED.has(tag)) continue;
33
- const name = RENAMED[tag] ?? tag;
34
- if (!ALLOWED.has(name)) {
35
- clean(child, target, doc); // unwrap: keep the text, lose the element
36
- continue;
37
- }
38
- const copy = doc.createElement(name);
39
- if (name === "a") {
40
- const href = child.getAttribute("href");
41
- if (href && isSafeUrl(href)) {
42
- copy.setAttribute("href", href.trim());
43
- copy.setAttribute("rel", "noopener noreferrer");
44
- if (!href.trim().startsWith("#")) copy.setAttribute("target", "_blank");
45
- }
46
- }
47
- clean(child, copy, doc);
48
- target.append(copy);
49
- }
50
- }
51
- }
52
-
53
- /** Cleans HTML from the editor, the clipboard or the application. Returns the safe HTML string. */
54
- export function sanitizeHtml(html) {
55
- const parsed = new DOMParser().parseFromString(`<body>${html ?? ""}</body>`, "text/html");
56
- const out = parsed.implementation.createHTMLDocument("");
57
- clean(parsed.body, out.body, out);
58
- return out.body.innerHTML;
59
- }
60
-
61
- /** The text of an HTML string, without tags. */
62
- export function htmlToText(html) {
63
- const parsed = new DOMParser().parseFromString(`<body>${html ?? ""}</body>`, "text/html");
64
- return parsed.body.textContent ?? "";
65
- }
1
+ //sanitize for Athena UI
2
+ //Author: Csaba Tóth https://github.com/csabatibortoth/
3
+ //LICENCE: MIT
4
+ //Repository: https://github.com/csabatibortoth/athena-ui
5
+
6
+ // Allow-list HTML sanitizer for the rich text editor. Everything that is not listed is unwrapped
7
+ // (its text stays), dangerous elements are removed with their content, and every attribute is dropped
8
+ // except a checked `href` on links. The input is parsed in an inert document, so nothing runs
9
+ // while it is being cleaned.
10
+
11
+ const ALLOWED = new Set([
12
+ "p", "br", "b", "strong", "i", "em", "u", "s", "strike", "del", "ul", "ol", "li",
13
+ "h1", "h2", "h3", "h4", "blockquote", "code", "pre", "a", "hr", "sub", "sup"
14
+ ]);
15
+ const RENAMED = { div: "p", strike: "s", del: "s", b: "strong", i: "em" };
16
+ const DROPPED = new Set(["script", "style", "iframe", "object", "embed", "template", "noscript", "svg", "math", "link", "meta", "title", "form", "textarea", "select", "button", "input", "head"]);
17
+ // A path starts with one slash that is not followed by another slash OR a backslash: a browser reads a backslash in an
18
+ // http(s) URL as a slash, so "/\evil.com" is "//evil.com" (another site), not a path of this one.
19
+ const SAFE_URL = /^(?:https?:|mailto:|tel:|#|\/(?![\/\\])|\.{1,2}\/)/i;
20
+
21
+ /** True for http(s), mailto, tel, anchors and relative paths. Rejects javascript:, data:, vbscript: and the like. */
22
+ export function isSafeUrl(url) {
23
+ // Browsers ignore control characters and spaces inside the scheme ("java\tscript:").
24
+ const compact = String(url ?? "").replace(/[\u0000- \u007f-\u009f]/g, "");
25
+ return compact !== "" && SAFE_URL.test(compact);
26
+ }
27
+
28
+ function clean(node, target, doc) {
29
+ for (const child of [...node.childNodes]) {
30
+ if (child.nodeType === 3) {
31
+ target.append(doc.createTextNode(child.nodeValue));
32
+ } else if (child.nodeType === 1) {
33
+ const tag = child.tagName.toLowerCase();
34
+ if (DROPPED.has(tag)) continue;
35
+ const name = RENAMED[tag] ?? tag;
36
+ if (!ALLOWED.has(name)) {
37
+ clean(child, target, doc); // unwrap: keep the text, lose the element
38
+ continue;
39
+ }
40
+ const copy = doc.createElement(name);
41
+ if (name === "a") {
42
+ const href = child.getAttribute("href");
43
+ if (href && isSafeUrl(href)) {
44
+ copy.setAttribute("href", href.trim());
45
+ copy.setAttribute("rel", "noopener noreferrer");
46
+ if (!href.trim().startsWith("#")) copy.setAttribute("target", "_blank");
47
+ }
48
+ }
49
+ clean(child, copy, doc);
50
+ target.append(copy);
51
+ }
52
+ }
53
+ }
54
+
55
+ /** Cleans HTML from the editor, the clipboard or the application. Returns the safe HTML string. */
56
+ export function sanitizeHtml(html) {
57
+ const parsed = new DOMParser().parseFromString(`<body>${html ?? ""}</body>`, "text/html");
58
+ const out = parsed.implementation.createHTMLDocument("");
59
+ clean(parsed.body, out.body, out);
60
+ return out.body.innerHTML;
61
+ }
62
+
63
+ /** The text of an HTML string, without tags. */
64
+ export function htmlToText(html) {
65
+ const parsed = new DOMParser().parseFromString(`<body>${html ?? ""}</body>`, "text/html");
66
+ return parsed.body.textContent ?? "";
67
+ }
@@ -1,5 +1,5 @@
1
1
  export function applyConfig(...sources: any[]): {
2
- version: "2.7.0";
2
+ version: "2.7.1";
3
3
  container_class: "container";
4
4
  header_class: "header-container";
5
5
  header_menu_class: "header-menu menu";
@@ -32,7 +32,7 @@ export function applyConfig(...sources: any[]): {
32
32
  export function withBorder(className: any): any;
33
33
  export const BORDERED_CLASS: "default-cointainer-bordered";
34
34
  export const defaults: Readonly<{
35
- version: "2.7.0";
35
+ version: "2.7.1";
36
36
  container_class: "container";
37
37
  header_class: "header-container";
38
38
  header_menu_class: "header-menu menu";
@@ -64,7 +64,7 @@ export const defaults: Readonly<{
64
64
  }>;
65
65
  export default config;
66
66
  declare namespace config {
67
- let version: "2.7.0";
67
+ let version: "2.7.1";
68
68
  let container_class: "container";
69
69
  let header_class: "header-container";
70
70
  let header_menu_class: "header-menu menu";