markdown-up 5.2.0 → 5.2.2

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.
Files changed (3) hide show
  1. package/README.md +21 -12
  2. package/lib/app.js +45 -1
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # MarkdownUp
1
+ # markdown-up
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/markdown-up)](https://www.npmjs.com/package/markdown-up)
4
4
  [![GitHub](https://img.shields.io/github/license/craigahobbs/markdown-up)](https://github.com/craigahobbs/markdown-up/blob/main/LICENSE)
@@ -79,12 +79,29 @@ application stub file, index.html. For example:
79
79
  To view a different Markdown resource, set the application's "url" hash parameter (i.e.,
80
80
  "http://127.0.0.1:8000#url=other.md").
81
81
 
82
+ A Markdown resource's scripts run with the page's origin access (e.g., they can fetch same-origin
83
+ resources and read local storage). For this reason, the "url" hash parameter only allows same-origin
84
+ resource URLs by default. To change this, set the application's "hashURL" option to one of the
85
+ following:
86
+
87
+ - `'sameOrigin'` - only same-origin resource URLs are allowed (the default)
88
+
89
+ - `'any'` - any resource URL is allowed. Only use this on an origin where there is nothing to protect -
90
+ the option applies to the page, but a script's access applies to the page's entire origin.
91
+
92
+ - `'none'` - the "url" hash parameter is not allowed
93
+
94
+ ~~~ javascript
95
+ const app = new MarkdownUp(window, {'hashURL': 'none'});
96
+ ~~~
97
+
82
98
 
83
99
  ## MarkdownUp Applications
84
100
 
85
- With MarkdownUp, you can write client-rendered frontend applications and backend APIs using
86
- [BareScript](https://craigahobbs.github.io/bare-script/language/).
87
- These applications are called *MarkdownUp Applications*. You can learn more from the
101
+ MarkdownUp applications are Markdown files that contain code. Add a `markdown-script` fenced code
102
+ block of [BareScript](https://craigahobbs.github.io/bare-script/language/) to any Markdown file, and
103
+ when MarkdownUp renders the file, it runs the script and renders its output in place of the code
104
+ block. You can learn more from the
88
105
  [MarkdownUp backend application's README](https://craigahobbs.github.io/markdown-up-py/#markdownup-applications).
89
106
 
90
107
 
@@ -96,14 +113,6 @@ code analysis on your code and reports warnings for any issues found.
96
113
  To turn on debug mode, click the debug button in the MarkdownUp menu in the upper-right of the page.
97
114
 
98
115
 
99
- ## The MarkdownUp Package
100
-
101
- The [markdown-up package](https://www.npmjs.com/package/markdown-up)
102
- exposes various functionality of the MarkdownUp application, such as rendering data tables and
103
- charts. For more information, refer to the
104
- [MarkdownUp package documentation](https://craigahobbs.github.io/markdown-up/doc/).
105
-
106
-
107
116
  ## Development
108
117
 
109
118
  This package is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme).
package/lib/app.js CHANGED
@@ -69,6 +69,9 @@ enum MarkdownUpView
69
69
  * @property {?boolean} [darkMode = false] - If true, use dark mode by default
70
70
  * @property {?number} [fontSize = 12] - The font size, in points
71
71
  * @property {?Object} [globals = null] - Global script runtime variables
72
+ * @property {?string} [hashURL = 'sameOrigin'] - The "url" hash parameter policy. A resource's scripts run with the
73
+ * page's origin access (e.g., fetch, local storage), so by default only same-origin resource URLs are allowed
74
+ * ("sameOrigin"). Use "any" to allow any resource URL or "none" to disallow the "url" hash parameter.
72
75
  * @property {?string} [helpURL] - The help URL
73
76
  * @property {?number} [lineHeight = 1.3] - The line height, in em
74
77
  * @property {?string} [markdownText = null] - The default Markdown text
@@ -101,6 +104,7 @@ export class MarkdownUp {
101
104
  (this.window.matchMedia && this.window.matchMedia('(prefers-color-scheme: dark)').matches);
102
105
  this.fontSize = (options !== null ? options.fontSize : null) ?? 12;
103
106
  this.globals = (options !== null ? options.globals : null) ?? null;
107
+ this.hashURL = (options !== null ? options.hashURL : null) ?? 'sameOrigin';
104
108
  this.helpURL = (options !== null ? options.helpURL : null) ?? defaultHelpURL;
105
109
  this.lineHeight = (options !== null ? options.lineHeight : null) ?? 1.3;
106
110
  this.markdownText = (options !== null ? options.markdownText : null) ?? null;
@@ -344,12 +348,13 @@ export class MarkdownUp {
344
348
 
345
349
  async main() {
346
350
  const url = this.getResourceURL();
351
+ const isHashURL = 'url' in this.params && this.params.url !== '';
347
352
  const scriptOptions = this.createScriptOptions();
348
353
 
349
354
  // Get the Markdown text
350
355
  let markdownText;
351
356
  let timeBegin;
352
- if (this.markdownText !== null && !('url' in this.params && this.params.url !== '')) {
357
+ if (this.markdownText !== null && !isHashURL) {
353
358
  ({markdownText} = this);
354
359
 
355
360
  // Log Markdown render begin
@@ -358,6 +363,11 @@ export class MarkdownUp {
358
363
  scriptOptions.logFn('MarkdownUp: ===== Rendering Markdown text');
359
364
  }
360
365
  } else {
366
+ // Is the "url" hash parameter's resource URL allowed?
367
+ if (isHashURL && !this.isHashURLAllowed(url)) {
368
+ return hashURLError(url);
369
+ }
370
+
361
371
  // Log Markdown render begin
362
372
  if (scriptOptions.debug) {
363
373
  scriptOptions.logFn(`MarkdownUp: ===== Rendering Markdown document "${url}"`);
@@ -372,6 +382,13 @@ export class MarkdownUp {
372
382
 
373
383
  // Fetch the Markdown text resource URL
374
384
  const response = await this.window.fetch(url);
385
+
386
+ // Was the "url" hash parameter's resource URL redirected to a resource URL that's not allowed?
387
+ const responseURL = response.url ?? '';
388
+ if (isHashURL && responseURL !== '' && !this.isHashURLAllowed(responseURL)) {
389
+ return hashURLError(responseURL);
390
+ }
391
+
375
392
  if (!response.ok) {
376
393
  const status = response.statusText;
377
394
  return {
@@ -467,6 +484,24 @@ export class MarkdownUp {
467
484
  }
468
485
 
469
486
 
487
+ // Is the "url" hash parameter's resource URL allowed? The resource's scripts run with the page's origin access.
488
+ isHashURLAllowed(url) {
489
+ if (this.hashURL === 'any') {
490
+ return true;
491
+ } else if (this.hashURL !== 'sameOrigin') {
492
+ return false;
493
+ }
494
+
495
+ // Same-origin? Resolve the URL as fetch does - this also excludes "data:" and "blob:" URLs.
496
+ try {
497
+ const {protocol, host} = new URL(url, this.window.document.baseURI);
498
+ return protocol === this.window.location.protocol && host === this.window.location.host;
499
+ } catch {
500
+ return false;
501
+ }
502
+ }
503
+
504
+
470
505
  createScriptOptions() {
471
506
  // Create the markdown options object
472
507
  const markdownOptions = {
@@ -809,6 +844,15 @@ export class MarkdownUp {
809
844
  }
810
845
 
811
846
 
847
+ // Create the main result for a "url" hash parameter resource URL that's not allowed
848
+ function hashURLError(url) {
849
+ return {
850
+ 'title': 'MarkdownUp',
851
+ 'elements': {'html': 'p', 'elem': {'text': `Error: The "url" hash parameter resource URL "${url}" is not allowed`}}
852
+ };
853
+ }
854
+
855
+
812
856
  // Get a URL's header ID
813
857
  function getHeaderId(url) {
814
858
  const matchId = url.match(rHeaderId);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "5.2.0",
4
+ "version": "5.2.2",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",
@@ -28,8 +28,8 @@
28
28
  "devDependencies": {
29
29
  "@eslint/js": "~10.0",
30
30
  "c8": "~12.0",
31
- "eslint": "~10.12",
32
- "globals": "~17.13",
31
+ "eslint": "~10.14",
32
+ "globals": "~17.14",
33
33
  "jsdoc": "~4.0",
34
34
  "jsdom": "~30.1"
35
35
  }