Skip to content

Commit

Permalink
[vscode] Add WebviewOptions.enableForms
Browse files Browse the repository at this point in the history
Added the optional `enableForms` field to `WebviewOptions`.
This allows the `allow-forms` sandbox attribute to be set.

Contributed on behalf of STMicroelectronics
  • Loading branch information
sgraband committed Dec 13, 2022
1 parent ac7a9ff commit 6e6ea20
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## v1.33.0 - unreleased

- [scripts] integrated start-up performance scripts into nightly master build [#10463](https://github.com/eclipse-theia/theia/pull/10463) - Contributed on behalf of STMicroelectronics
- [plugin] added `enableForms` field to `WebviewOptions` [#11983](https://github.com/eclipse-theia/theia/pull/11983) - Contributed on behalf of STMicroelectronics

<a name="breaking_changes_1.33.0">[Breaking Changes:](#breaking_changes_1.33.0)</a>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,12 @@ export class CustomEditorsMainImpl implements CustomEditorsMain, Disposable {
const view = await this.widgetManager.getOrCreateWidget<CustomEditorWidget>(CustomEditorWidget.FACTORY_ID, <WebviewWidgetIdentifier>{ id: panelId });
this.webviewsMain.hookWebview(view);
view.title.label = title;
const { enableFindWidget, retainContextWhenHidden, enableScripts, localResourceRoots, ...contentOptions } = options;
const { enableFindWidget, retainContextWhenHidden, enableScripts, enableForms, localResourceRoots, ...contentOptions } = options;
view.viewColumn = ViewColumn.One; // behaviour might be overridden later using widgetOpenerOptions (if available)
view.options = { enableFindWidget, retainContextWhenHidden };
view.setContentOptions({
allowScripts: enableScripts,
allowForms: enableForms,
localResourceRoots: localResourceRoots && localResourceRoots.map(root => root.toString()),
...contentOptions,
...view.contentOptions
Expand Down
9 changes: 8 additions & 1 deletion packages/plugin-ext/src/main/browser/webview/pre/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,14 @@
const newFrame = document.createElement('iframe');
newFrame.setAttribute('id', 'pending-frame');
newFrame.setAttribute('frameborder', '0');
newFrame.setAttribute('sandbox', options.allowScripts ? 'allow-scripts allow-forms allow-same-origin allow-downloads' : 'allow-same-origin');
const sandboxOptions = ['allow-same-origin'];
if (options.allowScripts) {
sandboxOptions.push('allow-scripts', 'allow-downloads');
}
if (options.allowForms || (options.allowScripts && options.allowForms === undefined)) {
sandboxOptions.push('allow-forms');
}
newFrame.setAttribute('sandbox', sandboxOptions.join(' '));
if (host.fakeLoad) {
// We should just be able to use srcdoc, but I wasn't
// seeing the service worker applying properly.
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/src/main/browser/webview/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const enum WebviewMessageChannels {

export interface WebviewContentOptions {
readonly allowScripts?: boolean;
readonly allowForms?: boolean;
readonly localResourceRoots?: ReadonlyArray<string>;
readonly portMapping?: ReadonlyArray<WebviewPortMapping>;
readonly enableCommandUris?: boolean;
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-ext/src/main/browser/webviews-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable {
this.hookWebview(view);
view.viewType = viewType;
view.title.label = title;
const { enableFindWidget, retainContextWhenHidden, enableScripts, localResourceRoots, ...contentOptions } = options;
const { enableFindWidget, retainContextWhenHidden, enableScripts, enableForms, localResourceRoots, ...contentOptions } = options;
view.options = { enableFindWidget, retainContextWhenHidden };
view.setContentOptions({
allowScripts: enableScripts,
allowForms: enableForms,
localResourceRoots: localResourceRoots && localResourceRoots.map(root => root.toString()),
...contentOptions
});
Expand Down Expand Up @@ -171,9 +172,10 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable {

async $setOptions(handle: string, options: WebviewOptions): Promise<void> {
const webview = await this.getWebview(handle);
const { enableScripts, localResourceRoots, ...contentOptions } = options;
const { enableScripts, enableForms, localResourceRoots, ...contentOptions } = options;
webview.setContentOptions({
allowScripts: enableScripts,
allowForms: enableForms,
localResourceRoots: localResourceRoots && localResourceRoots.map(root => root.toString()),
...contentOptions
});
Expand Down Expand Up @@ -210,10 +212,11 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable {
}

const options = widget.options;
const { allowScripts, localResourceRoots, ...contentOptions } = widget.contentOptions;
const { allowScripts, allowForms, localResourceRoots, ...contentOptions } = widget.contentOptions;
this.updateViewState(widget);
await this.proxy.$deserializeWebviewPanel(handle, widget.viewType, title, state, widget.viewState, {
enableScripts: allowScripts,
enableForms: allowForms,
localResourceRoots: localResourceRoots && localResourceRoots.map(root => URI.parse(root)),
...contentOptions,
...options
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3768,6 +3768,14 @@ export module '@theia/plugin' {
*/
readonly enableScripts?: boolean;

/**
* Controls whether forms are enabled in the webview content or not.
*
* Defaults to true if {@link WebviewOptions.enableScripts scripts are enabled}. Otherwise defaults to false.
* Explicitly setting this property to either true or false overrides the default.
*/
readonly enableForms?: boolean;

/**
* Controls whether command uris are enabled in webview content or not.
*
Expand Down

0 comments on commit 6e6ea20

Please sign in to comment.