Skip to content

Commit 97c987b

Browse files
Merge commit from fork
* fix: only open http and https editor urls Validate the editor url protocol before opening it, so only absolute http and https urls are used. * fix: address review feedback - drop the redundant guard in post.ts; the editor action in embed.ts already gates the url - return early rather than nesting the editor-link code - condense the isValidEditorURL docstring * fix: rework editor action guard and condense docstring - return early rather than nesting the editor-link code in embed.ts - condense the isValidEditorURL docstring These were staged but missing from 620b96f; committing them now.
1 parent ea3e7b8 commit 97c987b

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/embed.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {Handler, Options as TooltipOptions} from 'vega-tooltip';
2929
import post from './post.js';
3030
import embedStyle from './style.js';
3131
import {Config, ExpressionFunction, Mode} from './types.js';
32-
import {mergeDeep} from './util.js';
32+
import {isValidEditorURL, mergeDeep} from './util.js';
3333
import pkg from '../package.json';
3434

3535
export const version = pkg.version;
@@ -517,8 +517,12 @@ async function _embed(
517517
}
518518

519519
// add 'Open in Vega Editor' action
520-
if (actions === true || actions.editor !== false) {
521-
const editorUrl = opts.editorUrl ?? 'https://vega.github.io/editor/';
520+
const editorUrl = opts.editorUrl ?? 'https://vega.github.io/editor/';
521+
const showEditorAction = actions === true || actions.editor !== false;
522+
523+
if (showEditorAction && !isValidEditorURL(editorUrl)) {
524+
logger.warn(`Ignoring the editor action since editorUrl is not an http or https url: ${editorUrl}`);
525+
} else if (showEditorAction) {
522526
const editorLink = document.createElement('a');
523527

524528
editorLink.text = i18n.EDITOR_ACTION;

src/util.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ export function isURL(s: string): boolean {
44
return s.startsWith('http://') || s.startsWith('https://') || s.startsWith('//');
55
}
66

7+
/**
8+
* Return true if the string is a valid URL using a supported protocol (absolute paths using HTTP or HTTPS only).
9+
*/
10+
export function isValidEditorURL(url: string): boolean {
11+
let protocol: string;
12+
13+
try {
14+
({protocol} = new URL(url));
15+
} catch {
16+
return false;
17+
}
18+
19+
return protocol === 'http:' || protocol === 'https:';
20+
}
21+
722
export type DeepPartial<T> = {[P in keyof T]?: P extends unknown ? unknown : DeepPartial<T[P]>};
823

924
export function mergeDeep<T>(dest: T, ...src: readonly DeepPartial<T>[]): T {

0 commit comments

Comments
 (0)