Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs_website/docs/configurations/infra_config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Otherwise you can also pass the environment variable directly when launching the

### Iframe Embedding

`IFRAME_ALLOWED_ORIGINS`: This is the allowed list for embedding Querybook in an iframe. By default it can only be embedded by itself.
`IFRAME_ALLOWED_ORIGINS`: This is the allowed list for embedding Querybook in an iframe. By default it can only be embedded by itself. When set, this also restricts which origins can send queries to the [embedded editor](/docs/integrations/embedded_iframe) via `postMessage`.

### Database

Expand Down
6 changes: 6 additions & 0 deletions querybook/server/datasources/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from app.flask_app import limiter
from app.datasource import register
from env import QuerybookSettings
from lib.change_log import get_change_log_list, get_change_log_content_by_date


Expand All @@ -22,3 +23,8 @@ def test_ratelimit():
Endpoint to ensure ratelimit works in prod
"""
return "yes"


@register("/utils/embedded/allowed_origins/", methods=["GET"])
def get_embedded_allowed_origins():
return QuerybookSettings.IFRAME_ALLOWED_ORIGINS or []
56 changes: 45 additions & 11 deletions querybook/webapp/components/EmbeddedQueryPage/EmbeddedQueryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ import React from 'react';
import { useDispatch, useSelector } from 'react-redux';

import QueryComposer from 'components/QueryComposer/QueryComposer';
import { useResource } from 'hooks/useResource';
import { IAdhocQuery } from 'const/adhocQuery';
import { receiveAdhocQuery } from 'redux/adhocQuery/action';
import { Dispatch, IStoreState } from 'redux/store/types';
import { EmbeddedResource } from 'resource/embedded';
import { Button } from 'ui/Button/Button';
import { FullHeight } from 'ui/FullHeight/FullHeight';

import './EmbeddedQueryPage.scss';

function isOriginAllowed(origin: string, allowedOrigins: string[]): boolean {
if (allowedOrigins.length === 0 || origin === window.location.origin) {
return true;
}

return allowedOrigins.some((allowed) => origin === new URL(allowed).origin);
}

const EmbeddedQueryPage: React.FunctionComponent = () => {
const environmentId = useSelector(
(state: IStoreState) => state.environment.currentEnvironmentId
Expand All @@ -18,15 +28,30 @@ const EmbeddedQueryPage: React.FunctionComponent = () => {
(state: IStoreState) => state.adhocQuery[environmentId]?.query ?? ''
);

const { data: allowedOrigins } = useResource(
EmbeddedResource.getAllowedOrigins
);

const dispatch: Dispatch = useDispatch();
const setQuery = React.useCallback(
(query: IAdhocQuery) =>
dispatch(receiveAdhocQuery(query, environmentId)),
[]
(adhocQuery: IAdhocQuery) =>
dispatch(receiveAdhocQuery(adhocQuery, environmentId)),
[dispatch, environmentId]
);

const onMessage = React.useCallback((e) => {
if (e.data && e.data.type === 'SET_QUERY') {
const onMessage = React.useCallback(
(e: MessageEvent) => {
if (!e.data || e.data.type !== 'SET_QUERY') {
return;
}

if (!isOriginAllowed(e.origin, allowedOrigins)) {
console.warn(
`[EmbeddedQueryPage] Blocked postMessage from untrusted origin: ${e.origin}`
);
return;
}

const query: IAdhocQuery = {};
if (e.data.value) {
query.query = e.data.value;
Expand All @@ -36,21 +61,30 @@ const EmbeddedQueryPage: React.FunctionComponent = () => {
}

setQuery(query);
}
}, []);
},
[allowedOrigins, setQuery]
);

React.useEffect(() => {
// Tell the parent we are ready to receive query
if (!allowedOrigins) {
return;
}

// Tell the parent we are ready to receive query after allowed origins are loaded
window.parent.postMessage({ type: 'SEND_QUERY' }, '*');
}, []);
}, [allowedOrigins]);

// Setup query receiver
React.useEffect(() => {
if (!allowedOrigins) {
return;
}

// Attach event listener to receive query from parent
window.addEventListener('message', onMessage, false);
return () => {
window.removeEventListener('message', onMessage);
};
}, [onMessage]);
}, [allowedOrigins, onMessage]);

return (
<FullHeight flex={'column'} className="EmbeddedQueryPage">
Expand Down
6 changes: 6 additions & 0 deletions querybook/webapp/resource/embedded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ds from 'lib/datasource';

export const EmbeddedResource = {
getAllowedOrigins: () =>
ds.fetch<string[]>(`/utils/embedded/allowed_origins/`),
};
Loading