From 18af3f8a109da3ff6fc443132951ab19a299be3f Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Wed, 9 Jan 2019 14:16:50 +0100 Subject: [PATCH 1/3] Add trusted-types --- types/trusted-types/index.d.ts | 31 ++++++++++++++++++++++ types/trusted-types/trusted-types-tests.ts | 17 ++++++++++++ types/trusted-types/tsconfig.json | 23 ++++++++++++++++ types/trusted-types/tslint.json | 1 + 4 files changed, 72 insertions(+) create mode 100644 types/trusted-types/index.d.ts create mode 100644 types/trusted-types/trusted-types-tests.ts create mode 100644 types/trusted-types/tsconfig.json create mode 100644 types/trusted-types/tslint.json diff --git a/types/trusted-types/index.d.ts b/types/trusted-types/index.d.ts new file mode 100644 index 0000000000..64ed96b713 --- /dev/null +++ b/types/trusted-types/index.d.ts @@ -0,0 +1,31 @@ +// Type definitions for trusted-types 1.0 +// Project: https://github.com/WICG/trusted-types +// Definitions by: Jakub Vrana +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare class TrustedHTML {} +declare class TrustedScript {} +declare class TrustedScriptURL {} +declare class TrustedURL {} + +declare class TrustedTypePolicy { + createHTML(s: string): TrustedHTML; + createScript(s: string): TrustedScript; + createScriptURL(s: string): TrustedScriptURL; + createURL(s: string): TrustedURL; +} + +interface TrustedTypeInnerPolicy { + createHTML(s: string): string; + createScript(s: string): string; + createScriptURL(s: string): string; + createURL(s: string): string; +} + +declare class TrustedTypePolicyFactory { + createPolicy(name: string, policy: TrustedTypeInnerPolicy, expose?: boolean): TrustedTypePolicy; + getExposedPolicy(name: string): TrustedTypePolicy; + getPolicyNames(): string[]; +} + +declare const TrustedTypes: TrustedTypePolicyFactory; diff --git a/types/trusted-types/trusted-types-tests.ts b/types/trusted-types/trusted-types-tests.ts new file mode 100644 index 0000000000..79f79db4b7 --- /dev/null +++ b/types/trusted-types/trusted-types-tests.ts @@ -0,0 +1,17 @@ +const policy = { + createHTML: (s: string) => s, + createScript: (s: string) => s, + createScriptURL: (s: string) => s, + createURL: (s: string) => s, +}; + +TrustedTypes.getPolicyNames(); +TrustedTypes.createPolicy('default', policy, true); +TrustedTypes.getExposedPolicy('default'); + +const trustedTypes = TrustedTypes.createPolicy('test', policy); + +trustedTypes.createHTML('') instanceof TrustedHTML; +trustedTypes.createScript('') instanceof TrustedScript; +trustedTypes.createScriptURL('') instanceof TrustedScriptURL; +trustedTypes.createURL('') instanceof TrustedURL; diff --git a/types/trusted-types/tsconfig.json b/types/trusted-types/tsconfig.json new file mode 100644 index 0000000000..8bfe83a032 --- /dev/null +++ b/types/trusted-types/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "trusted-types-tests.ts" + ] +} diff --git a/types/trusted-types/tslint.json b/types/trusted-types/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/trusted-types/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 71cbfeded447b7d6b7da29d03a701dc26660d070 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Wed, 9 Jan 2019 14:46:52 +0100 Subject: [PATCH 2/3] trusted-types: Prevent structural typing --- types/trusted-types/index.d.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/types/trusted-types/index.d.ts b/types/trusted-types/index.d.ts index 64ed96b713..2d2db62999 100644 --- a/types/trusted-types/index.d.ts +++ b/types/trusted-types/index.d.ts @@ -3,10 +3,21 @@ // Definitions by: Jakub Vrana // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare class TrustedHTML {} -declare class TrustedScript {} -declare class TrustedScriptURL {} -declare class TrustedURL {} +declare class TrustedHTML { + private readonly _TrustedHTMLBrand: true; // To prevent structural typing. +} + +declare class TrustedScript { + private readonly _TrustedScriptBrand: true; // To prevent structural typing. +} + +declare class TrustedScriptURL { + private readonly _TrustedScriptURLBrand: true; // To prevent structural typing. +} + +declare class TrustedURL { + private readonly _TrustedURL: trueBrand; // To prevent structural typing. +} declare class TrustedTypePolicy { createHTML(s: string): TrustedHTML; From e34aaeda276d127870a6fa0e2ec74cc2e5f0ea55 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Wed, 9 Jan 2019 15:05:02 +0100 Subject: [PATCH 3/3] trusted-types: Fix a typo --- types/trusted-types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/trusted-types/index.d.ts b/types/trusted-types/index.d.ts index 2d2db62999..e6e410dfed 100644 --- a/types/trusted-types/index.d.ts +++ b/types/trusted-types/index.d.ts @@ -16,7 +16,7 @@ declare class TrustedScriptURL { } declare class TrustedURL { - private readonly _TrustedURL: trueBrand; // To prevent structural typing. + private readonly _TrustedURLBrand: true; // To prevent structural typing. } declare class TrustedTypePolicy {