Merge pull request #184148 from microsoft/alexd/disciplinary-macaw

Fix usage of types coming outside of TS base lib & allow embedders to intercept TT calls also in the worker
This commit is contained in:
Alexandru Dima 2023-06-02 13:52:48 +02:00 committed by GitHub
commit 8092bb26d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 26 deletions

View file

@ -34,12 +34,25 @@ declare namespace monaco {
*/
getWorkerUrl?(workerId: string, label: string): string;
/**
* Create a trust types policy (same API as window.trustedTypes.createPolicy)
* Create a trusted types policy (same API as window.trustedTypes.createPolicy)
*/
createTrustedTypesPolicy<Options extends TrustedTypePolicyOptions>(
createTrustedTypesPolicy(
policyName: string,
policyOptions?: Options,
): undefined | Pick<TrustedTypePolicy<Options>, 'name' | Extract<keyof Options, keyof TrustedTypePolicyOptions>>;
policyOptions?: ITrustedTypePolicyOptions,
): undefined | ITrustedTypePolicy;
}
export interface ITrustedTypePolicyOptions {
createHTML?: (input: string, ...arguments: any[]) => string;
createScript?: (input: string, ...arguments: any[]) => string;
createScriptURL?: (input: string, ...arguments: any[]) => string;
}
export interface ITrustedTypePolicy {
readonly name: string;
createHTML?(input: string): any;
createScript?(input: string): any;
createScriptURL?(input: string): any;
}
export interface IDisposable {

View file

@ -5,25 +5,50 @@
(function () {
const MonacoEnvironment = (<any>globalThis).MonacoEnvironment;
const monacoBaseUrl = MonacoEnvironment && MonacoEnvironment.baseUrl ? MonacoEnvironment.baseUrl : '../../../';
interface IMonacoEnvironment {
baseUrl?: string;
createTrustedTypesPolicy<Options extends TrustedTypePolicyOptions>(
policyName: string,
policyOptions?: Options,
): undefined | Pick<TrustedTypePolicy<Options>, 'name' | Extract<keyof Options, keyof TrustedTypePolicyOptions>>;
}
const monacoEnvironment: IMonacoEnvironment | undefined = (globalThis as any).MonacoEnvironment;
const monacoBaseUrl = monacoEnvironment && monacoEnvironment.baseUrl ? monacoEnvironment.baseUrl : '../../../';
const trustedTypesPolicy = (
typeof self.trustedTypes?.createPolicy === 'function'
? self.trustedTypes?.createPolicy('amdLoader', {
createScriptURL: value => value,
createScript: (_, ...args: string[]) => {
// workaround a chrome issue not allowing to create new functions
// see https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
const fnArgs = args.slice(0, -1).join(',');
const fnBody = args.pop()!.toString();
// Do not add a new line to fnBody, as this will confuse source maps.
const body = `(function anonymous(${fnArgs}) { ${fnBody}\n})`;
return body;
}
})
: undefined
);
function createTrustedTypesPolicy<Options extends TrustedTypePolicyOptions>(
policyName: string,
policyOptions?: Options,
): undefined | Pick<TrustedTypePolicy<Options>, 'name' | Extract<keyof Options, keyof TrustedTypePolicyOptions>> {
if (monacoEnvironment?.createTrustedTypesPolicy) {
try {
return monacoEnvironment.createTrustedTypesPolicy(policyName, policyOptions);
} catch (err) {
console.warn(err);
return undefined;
}
}
try {
return self.trustedTypes?.createPolicy(policyName, policyOptions);
} catch (err) {
console.warn(err);
return undefined;
}
}
const trustedTypesPolicy = createTrustedTypesPolicy('amdLoader', {
createScriptURL: value => value,
createScript: (_, ...args: string[]) => {
// workaround a chrome issue not allowing to create new functions
// see https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
const fnArgs = args.slice(0, -1).join(',');
const fnBody = args.pop()!.toString();
// Do not add a new line to fnBody, as this will confuse source maps.
const body = `(function anonymous(${fnArgs}) { ${fnBody}\n})`;
return body;
}
});
function canUseEval(): boolean {
try {

21
src/vs/monaco.d.ts vendored
View file

@ -34,12 +34,25 @@ declare namespace monaco {
*/
getWorkerUrl?(workerId: string, label: string): string;
/**
* Create a trust types policy (same API as window.trustedTypes.createPolicy)
* Create a trusted types policy (same API as window.trustedTypes.createPolicy)
*/
createTrustedTypesPolicy<Options extends TrustedTypePolicyOptions>(
createTrustedTypesPolicy(
policyName: string,
policyOptions?: Options,
): undefined | Pick<TrustedTypePolicy<Options>, 'name' | Extract<keyof Options, keyof TrustedTypePolicyOptions>>;
policyOptions?: ITrustedTypePolicyOptions,
): undefined | ITrustedTypePolicy;
}
export interface ITrustedTypePolicyOptions {
createHTML?: (input: string, ...arguments: any[]) => string;
createScript?: (input: string, ...arguments: any[]) => string;
createScriptURL?: (input: string, ...arguments: any[]) => string;
}
export interface ITrustedTypePolicy {
readonly name: string;
createHTML?(input: string): any;
createScript?(input: string): any;
createScriptURL?(input: string): any;
}
export interface IDisposable {