Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): Make the integration more secure, fix tests
- Loading branch information
Emanuel Tesar
committed
Sep 9, 2019
1 parent
7a443c5
commit 1740085
Showing
6 changed files
with
152 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
/* @flow */ | ||
import Vue from 'core/index' | ||
import {getTrustedTypes, isTrustedValue} from 'shared/util' | ||
|
||
type TrustedTypePolicy = { | ||
// value returned is actually an object with toString method returning the wrapped value | ||
createHTML: (value: any) => string; | ||
}; | ||
|
||
let policy: TrustedTypePolicy | ||
export function convertToTrustedType(value: any) { | ||
let policy: ?TrustedTypePolicy | ||
// we need this function to clear the policy in tests | ||
Vue.prototype.$clearTrustedTypesPolicy = function() { | ||
policy = undefined | ||
} | ||
|
||
export function maybeCreateDangerousSvgHTML(value: any): string { | ||
// create policy lazily to simplify testing | ||
const tt = getTrustedTypes() | ||
if (tt && !policy) { | ||
policy = tt.createPolicy('vue', {createHTML: (s) => s}); | ||
policy = tt.createPolicy(Vue.config.trustedTypesPolicyName, {createHTML: (s) => s}); | ||
} | ||
|
||
if (!tt) return value; | ||
else return policy.createHTML(value); | ||
if (!tt) return `<svg>${value}</svg>`; | ||
else if (!isTrustedValue(value)) throw new Error('Expected svg innerHTML to be TrustedHTML!'); | ||
// flow complains 'policy' may be undefined | ||
else return (policy: any).createHTML(`<svg>${value}</svg>`); | ||
} | ||
|
||
export function getTrustedTypes() { | ||
// TrustedTypes have been renamed to trustedTypes https://github.com/WICG/trusted-types/issues/177 | ||
return window.trustedTypes || window.TrustedTypes; | ||
} | ||
export function getTrustedShouldDecodeInnerHTML(href: boolean): string { | ||
return href ? `<a href="\n"/>` : `<div a="\n"/>` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters