Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Revert "feat: Improves SafeMarkdown HTML sanitization (apache#21895)"
Browse files Browse the repository at this point in the history
This reverts commit 7d1df3b.
  • Loading branch information
john-bodley committed Jan 17, 2023
1 parent f226804 commit d2ae204
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 1,991 deletions.
1 change: 0 additions & 1 deletion UPDATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ assists people when migrating to a new version.
## Next

- [22022](https://github.com/apache/superset/pull/22022): HTTP API endpoints `/superset/approve` and `/superset/request_access` have been deprecated and their HTTP methods were changed from GET to POST
- [21895](https://github.com/apache/superset/pull/21895): Markdown components had their security increased by adhering to the same sanitization process enforced by GitHub. This means that some HTML elements found in markdowns are not allowed anymore due to the security risks they impose. If you're deploying Superset in a trusted environment and wish to use some of the blocked elements, then you can use the HTML_SANITIZATION_SCHEMA_EXTENSIONS configuration to extend the default sanitization schema. There's also the option to disable HTML sanitization using the HTML_SANITIZATION configuration but we do not recommend this approach because of the security risks. Given the provided configurations, we don't view the improved sanitization as a breaking change but as a security patch.
- [20606](https://github.com/apache/superset/pull/20606): When user clicks on chart title or "Edit chart" button in Dashboard page, Explore opens in the same tab. Clicking while holding cmd/ctrl opens Explore in a new tab. To bring back the old behaviour (always opening Explore in a new tab), flip feature flag `DASHBOARD_EDIT_CHART_IN_NEW_TAB` to `True`.
- [20799](https://github.com/apache/superset/pull/20799): Presto and Trino engine will now display tracking URL for running queries in SQL Lab. If for some reason you don't want to show the tracking URL (for example, when your data warehouse hasn't enabled access for to Presto or Trino UI), update `TRACKING_URL_TRANSFORMER` in `config.py` to return `None`.
- [21002](https://github.com/apache/superset/pull/21002): Support Python 3.10 and bump pandas 1.4 and pyarrow 6.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ describe('Dashboard edit', () => {
cy.getBySel('dashboard-markdown-editor')
.should(
'have.text',
'✨Header 1\n✨Header 2\n✨Header 3\n\nClick here to learn more about markdown formatting',
'✨Header 1✨Header 2✨Header 3Click here to learn more about markdown formatting',
)
.click(10, 10);

Expand Down
2,145 changes: 355 additions & 1,790 deletions superset-frontend/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions superset-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
"react-jsonschema-form": "^1.2.0",
"react-lines-ellipsis": "^0.15.0",
"react-loadable": "^5.5.0",
"react-markdown": "^4.3.1",
"react-query": "^3.39.2",
"react-redux": "^7.2.0",
"react-resize-detector": "^6.7.6",
Expand Down
48 changes: 23 additions & 25 deletions superset-frontend/packages/superset-ui-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@
"name": "@superset-ui/core",
"version": "0.18.25",
"description": "Superset UI core",
"keywords": [
"superset"
],
"homepage": "https://github.com/apache-superset/superset-ui#readme",
"bugs": {
"url": "https://github.com/apache-superset/superset-ui/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/apache-superset/superset-ui.git"
},
"license": "Apache-2.0",
"author": "Superset",
"sideEffects": false,
"main": "lib/index.js",
"module": "esm/index.js",
"files": [
"esm",
"lib"
],
"repository": {
"type": "git",
"url": "git+https://github.com/apache-superset/superset-ui.git"
},
"keywords": [
"superset"
],
"author": "Superset",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/apache-superset/superset-ui/issues"
},
"homepage": "https://github.com/apache-superset/superset-ui#readme",
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@emotion/styled": "^11.3.0",
"fetch-mock": "^6.5.2",
"jest-mock-console": "^1.0.0",
"resize-observer-polyfill": "1.5.1"
},
"dependencies": {
"@babel/runtime": "^7.1.2",
"@types/d3-format": "^1.3.0",
Expand Down Expand Up @@ -51,20 +60,12 @@
"math-expression-evaluator": "^1.3.8",
"pretty-ms": "^7.0.0",
"react-error-boundary": "^1.2.5",
"react-markdown": "^8.0.3",
"rehype-raw": "^6.1.1",
"rehype-sanitize": "^5.0.1",
"react-markdown": "^4.3.1",
"reselect": "^4.0.0",
"rison": "^0.1.1",
"seedrandom": "^3.0.5",
"whatwg-fetch": "^3.0.0"
},
"devDependencies": {
"@emotion/styled": "^11.3.0",
"fetch-mock": "^6.5.2",
"jest-mock-console": "^1.0.0",
"resize-observer-polyfill": "1.5.1"
},
"peerDependencies": {
"@emotion/cache": "^11.4.0",
"@emotion/react": "^11.4.1",
Expand All @@ -75,8 +76,5 @@
"react": "^16.13.1",
"react-loadable": "^5.5.0",
"tinycolor2": "*"
},
"publishConfig": {
"access": "public"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,38 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, { useMemo } from 'react';
import ReactMarkdown from 'react-markdown';
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
import rehypeRaw from 'rehype-raw';
import { merge } from 'lodash';

import React from 'react';
import ReactMarkdown, { MarkdownAbstractSyntaxTree } from 'react-markdown';
// @ts-ignore no types available
import htmlParser from 'react-markdown/plugins/html-parser';

import { FeatureFlag, isFeatureEnabled } from '../utils';

interface SafeMarkdownProps {
source: string;
htmlSanitization?: boolean;
htmlSchemaOverrides?: typeof defaultSchema;
}

function SafeMarkdown({
source,
htmlSanitization = true,
htmlSchemaOverrides = {},
}: SafeMarkdownProps) {
const displayHtml = isFeatureEnabled(FeatureFlag.DISPLAY_MARKDOWN_HTML);
const escapeHtml = isFeatureEnabled(FeatureFlag.ESCAPE_MARKDOWN_HTML);

const rehypePlugins = useMemo(() => {
const rehypePlugins: any = [];
if (displayHtml && !escapeHtml) {
rehypePlugins.push(rehypeRaw);
if (htmlSanitization) {
const schema = merge(defaultSchema, htmlSchemaOverrides);
rehypePlugins.push([rehypeSanitize, schema]);
}
}
return rehypePlugins;
}, [displayHtml, escapeHtml, htmlSanitization, htmlSchemaOverrides]);
function isSafeMarkup(node: MarkdownAbstractSyntaxTree) {
return node.type === 'html' && node.value
? !/(href|src)="(javascript|vbscript|file):.*"/gim.test(node.value)
: true;
}

// React Markdown escapes HTML by default
function SafeMarkdown({ source }: SafeMarkdownProps) {
return (
<ReactMarkdown rehypePlugins={rehypePlugins} skipHtml={!displayHtml}>
{source}
</ReactMarkdown>
<ReactMarkdown
source={source}
escapeHtml={isFeatureEnabled(FeatureFlag.ESCAPE_MARKDOWN_HTML)}
skipHtml={!isFeatureEnabled(FeatureFlag.DISPLAY_MARKDOWN_HTML)}
allowNode={isSafeMarkup}
astPlugins={[
htmlParser({
isValidNode: (node: MarkdownAbstractSyntaxTree) =>
node.type !== 'script',
}),
]}
/>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only';
Expand Down Expand Up @@ -84,9 +83,4 @@ jest.mock('src/hooks/useTabId', () => ({
useTabId: () => 1,
}));

// Check https://github.com/remarkjs/react-markdown/issues/635
jest.mock('react-markdown', () => (props: any) => <>{props.children}</>);
jest.mock('rehype-sanitize', () => () => jest.fn());
jest.mock('rehype-raw', () => () => jest.fn());

process.env.WEBPACK_MODE = 'test';
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ const propTypes = {
deleteComponent: PropTypes.func.isRequired,
handleComponentDrop: PropTypes.func.isRequired,
updateComponents: PropTypes.func.isRequired,

// HTML sanitization
htmlSanitization: PropTypes.bool,
htmlSchemaOverrides: PropTypes.object,
};

const defaultProps = {};
Expand Down Expand Up @@ -269,8 +265,6 @@ class Markdown extends React.PureComponent {
? MARKDOWN_ERROR_MESSAGE
: this.state.markdownSource || MARKDOWN_PLACE_HOLDER
}
htmlSanitization={this.props.htmlSanitization}
htmlSchemaOverrides={this.props.htmlSchemaOverrides}
/>
);
}
Expand Down Expand Up @@ -379,8 +373,6 @@ function mapStateToProps(state) {
return {
undoLength: state.dashboardLayout.past.length,
redoLength: state.dashboardLayout.future.length,
htmlSanitization: state.common.conf.HTML_SANITIZATION,
htmlSchemaOverrides: state.common.conf.HTML_SANITIZATION_SCHEMA_EXTENSIONS,
};
}
export default connect(mapStateToProps)(Markdown);
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { Provider } from 'react-redux';
import React from 'react';
import { styledMount as mount } from 'spec/helpers/theming';
import sinon from 'sinon';
import ReactMarkdown from 'react-markdown';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { SafeMarkdown } from '@superset-ui/core';

import { act } from 'react-dom/test-utils';
import { MarkdownEditor } from 'src/components/AsyncAceEditor';
Expand Down Expand Up @@ -112,34 +112,34 @@ describe('Markdown', () => {
it('should render an Markdown when NOT focused', () => {
const wrapper = setup();
expect(wrapper.find(MarkdownEditor)).not.toExist();
expect(wrapper.find(SafeMarkdown)).toExist();
expect(wrapper.find(ReactMarkdown)).toExist();
});

it('should render an AceEditor when focused and editMode=true and editorMode=edit', async () => {
const wrapper = setup({ editMode: true });
expect(wrapper.find(MarkdownEditor)).not.toExist();
expect(wrapper.find(SafeMarkdown)).toExist();
expect(wrapper.find(ReactMarkdown)).toExist();
act(() => {
wrapper.find(WithPopoverMenu).simulate('click'); // focus + edit
});
await waitForComponentToPaint(wrapper);
expect(wrapper.find(MarkdownEditor)).toExist();
expect(wrapper.find(SafeMarkdown)).not.toExist();
expect(wrapper.find(ReactMarkdown)).not.toExist();
});

it('should render a ReactMarkdown when focused and editMode=true and editorMode=preview', () => {
const wrapper = setup({ editMode: true });
wrapper.find(WithPopoverMenu).simulate('click'); // focus + edit
expect(wrapper.find(MarkdownEditor)).toExist();
expect(wrapper.find(SafeMarkdown)).not.toExist();
expect(wrapper.find(ReactMarkdown)).not.toExist();

// we can't call setState on Markdown bc it's not the root component, so call
// the mode dropdown onchange instead
const dropdown = wrapper.find(MarkdownModeDropdown);
dropdown.prop('onChange')('preview');
wrapper.update();

expect(wrapper.find(SafeMarkdown)).toExist();
expect(wrapper.find(ReactMarkdown)).toExist();
expect(wrapper.find(MarkdownEditor)).not.toExist();
});

Expand Down
13 changes: 7 additions & 6 deletions superset-frontend/src/explore/components/SaveModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import React from 'react';
import { Dispatch } from 'redux';
import { SelectValue } from 'antd/lib/select';
import { connect } from 'react-redux';
import ReactMarkdown from 'react-markdown';
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls';
import {
Expand All @@ -44,6 +45,7 @@ import { SaveActionType } from 'src/explore/types';

// Session storage key for recent dashboard
const SK_DASHBOARD_ID = 'save_chart_recent_dashboard';
const SELECT_PLACEHOLDER = t('**Select** a dashboard OR **create** a new one');

interface SaveModalProps extends RouteComponentProps {
addDangerToast: (msg: string) => void;
Expand Down Expand Up @@ -349,12 +351,11 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> {
onChange={this.onDashboardSelectChange}
value={dashboardSelectValue || undefined}
placeholder={
<div>
<b>{t('Select')}</b>
{t(' a dashboard OR ')}
<b>{t('create')}</b>
{t(' a new one')}
</div>
// Using markdown to allow for good i18n
<ReactMarkdown
source={SELECT_PLACEHOLDER}
renderers={{ paragraph: 'span' }}
/>
}
/>
</FormItem>
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if (!isDevMode) {

const plugins = [
new webpack.ProvidePlugin({
process: 'process/browser.js',
process: 'process/browser',
}),

// creates a manifest.json mapping of name to hashed output used in template files
Expand Down
22 changes: 0 additions & 22 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,28 +644,6 @@ def _try_json_readsha(filepath: str, length: int) -> Optional[str]:
ENABLE_CORS = False
CORS_OPTIONS: Dict[Any, Any] = {}

# Sanitizes the HTML content used in markdowns to allow its rendering in a safe manner.
# Disabling this option is not recommended for security reasons. If you wish to allow
# valid safe elements that are not included in the default sanitization schema, use the
# HTML_SANITIZATION_SCHEMA_EXTENSIONS configuration.
HTML_SANITIZATION = True

# Use this configuration to extend the HTML sanitization schema.
# By default we use the Gihtub schema defined in
# https://github.com/syntax-tree/hast-util-sanitize/blob/main/lib/schema.js
# For example, the following configuration would allow the rendering of the
# style attribute for div elements and the ftp protocol in hrefs:
# HTML_SANITIZATION_SCHEMA_EXTENSIONS = {
# "attributes": {
# "div": ["style"],
# },
# "protocols": {
# "href": ["ftp"],
# }
# }
# Be careful when extending the default schema to avoid XSS attacks.
HTML_SANITIZATION_SCHEMA_EXTENSIONS: Dict[str, Any] = {}

# Chrome allows up to 6 open connections per domain at a time. When there are more
# than 6 slices in dashboard, a lot of time fetch requests are queued up and wait for
# next available socket. PR #5039 is trying to allow domain sharding for Superset,
Expand Down
Loading

0 comments on commit d2ae204

Please sign in to comment.