markdown-up 5.2.1 → 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.
- package/README.md +16 -0
- package/lib/app.js +45 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -79,6 +79,22 @@ 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
|
|
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 && !
|
|
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.
|
|
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.
|
|
32
|
-
"globals": "~17.
|
|
31
|
+
"eslint": "~10.14",
|
|
32
|
+
"globals": "~17.14",
|
|
33
33
|
"jsdoc": "~4.0",
|
|
34
34
|
"jsdom": "~30.1"
|
|
35
35
|
}
|