Skip to content

Commit

Permalink
Add opt out option allowDynamicStyling for CSP enforcers (#261)
Browse files Browse the repository at this point in the history
* add readme

* accept allowDynamicStyling

* changeset

* Update packages/explorer/src/EmbeddedExplorer.ts

Co-authored-by: Trevor Scheer <[email protected]>

* Update packages/sandbox/src/EmbeddedSandbox.ts

Co-authored-by: Trevor Scheer <[email protected]>

---------

Co-authored-by: Trevor Scheer <[email protected]>
  • Loading branch information
mayakoneval and trevor-scheer authored Jun 15, 2023
1 parent 3c6301a commit 7212121
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/quick-points-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@apollo/explorer': minor
'@apollo/sandbox': minor
---

Allow opting out of adding dynamic styles to the iframe rendered by Embedded Explorer / Sandbox
10 changes: 10 additions & 0 deletions packages/explorer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ me {
}
...
// style the iframe for your site
<style>
iframe {
height: 100%;
width: 100%;
border: none;
}
</style>
<div id="embeddableExplorer" />
```

### Examples from the raw cdn hosted umd file
Expand Down
20 changes: 15 additions & 5 deletions packages/explorer/src/EmbeddedExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ export interface BaseEmbeddableExplorerOptions {
accountId: string;
inviteToken: string;
};

/**
* optional. defaults to true.
* If false, the `width: 100%` and `height: 100%` are not applied to the iframe dynamically.
* You might pass false here if you enforce a Content Security Policy that disallows dynamic
* style injection.
*/
allowDynamicStyles?: boolean;
}

interface EmbeddableExplorerOptionsWithSchema
Expand Down Expand Up @@ -153,11 +161,13 @@ export class EmbeddedExplorer {
iframeElement.src = this.embeddedExplorerURL;

iframeElement.id = IFRAME_DOM_ID(this.uniqueEmbedInstanceId);
iframeElement.setAttribute(
'style',
'height: 100%; width: 100%; border: none;'
);

// default to `true` (`true` and `undefined` both ok)
if (this.options.allowDynamicStyles !== false) {
iframeElement.setAttribute(
'style',
'height: 100%; width: 100%; border: none;'
);
}
element?.appendChild(iframeElement);

// inject the Apollo favicon if there is not one on this page
Expand Down
8 changes: 8 additions & 0 deletions packages/sandbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ function App() {
}
...
// style the iframe for your site
<style>
iframe {
height: 100%;
width: 100%;
border: none;
}
</style>
<div id="embeddableSandbox" />
```
Expand Down
18 changes: 14 additions & 4 deletions packages/sandbox/src/EmbeddedSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export interface EmbeddableSandboxOptions {
* If false, the endpoint box at the top of sandbox will be `initialEndpoint` permanently
*/
endpointIsEditable?: boolean;
/**
* optional. defaults to true.
* If false, the `width: 100%` and `height: 100%` are not applied to the iframe dynamically.
* You might pass false here if you enforce a Content Security Policy that disallows dynamic
* style injection.
*/
allowDynamicStyles?: boolean;
}

type InternalEmbeddableSandboxOptions = EmbeddableSandboxOptions & {
Expand Down Expand Up @@ -180,10 +187,13 @@ export class EmbeddedSandbox {
)}?${queryString}`;

iframeElement.id = IFRAME_DOM_ID(this.uniqueEmbedInstanceId);
iframeElement.setAttribute(
'style',
'height: 100%; width: 100%; border: none;'
);
// default to `true` (`true` and `undefined` both ok)
if (this.options.allowDynamicStyles !== false) {
iframeElement.setAttribute(
'style',
'height: 100%; width: 100%; border: none;'
);
}

element?.appendChild(iframeElement);

Expand Down

0 comments on commit 7212121

Please sign in to comment.