Skip to content

Commit

Permalink
Fixing CSRF issues
Browse files Browse the repository at this point in the history
Since turning CSRF across the site with Flask-WTF, a few POST request
have been failing. This PR addresses these issues.
  • Loading branch information
mistercrunch committed Apr 7, 2017
1 parent f3a0802 commit c29395f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion superset/assets/javascripts/SqlLab/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from 'react';
import TabbedSqlEditors from './TabbedSqlEditors';
import QueryAutoRefresh from './QueryAutoRefresh';
import QuerySearch from './QuerySearch';
import AlertsWrapper from './AlertsWrapper';
import AlertsWrapper from '../../components/AlertsWrapper';

import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
Expand Down
8 changes: 7 additions & 1 deletion superset/assets/javascripts/explorev2/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import { now } from '../modules/dates';
import { initEnhancer } from '../reduxUtils';
import AlertsWrapper from '../components/AlertsWrapper';
import { getControlsState, getFormDataFromControls } from './stores/store';
import { initJQueryAjaxCSRF } from '../modules/utils';


// jquery and bootstrap required to make bootstrap dropdown menu's work
const $ = window.$ = require('jquery'); // eslint-disable-line
const jQuery = window.jQuery = require('jquery'); // eslint-disable-line
require('bootstrap');
require('./main.css');
initJQueryAjaxCSRF();

const exploreViewContainer = document.getElementById('js-explore-view-container');
const bootstrapData = JSON.parse(exploreViewContainer.getAttribute('data-bootstrap'));
Expand Down Expand Up @@ -47,7 +50,10 @@ const store = createStore(exploreReducer, bootstrappedState,

ReactDOM.render(
<Provider store={store}>
<ExploreViewContainer />
<div>
<ExploreViewContainer />
<AlertsWrapper />
</div>
</Provider>,
exploreViewContainer
);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import AlertsWrapper from '../../../javascripts/SqlLab/components/AlertsWrapper';
import AlertsWrapper from '../../../javascripts/components/AlertsWrapper';
import { describe, it } from 'mocha';
import { expect } from 'chai';

Expand Down
9 changes: 3 additions & 6 deletions superset/assets/utils/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global notify */
/* eslint global-require: 0 */
import $ from 'jquery';
const d3 = window.d3 || require('d3');
Expand Down Expand Up @@ -78,12 +79,8 @@ export function getShortUrl(longUrl, callback) {
success: (data) => {
callback(data);
},
error: (error) => {
/* eslint no-console: 0 */
if (console && console.warn) {
console.warn('Something went wrong...');
console.warn(error);
}
error: () => {
notify.error('Error getting the short URL');
callback(longUrl);
},
});
Expand Down
12 changes: 6 additions & 6 deletions superset/templates/superset/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
{% include "superset/partials/_script_tag.html" %}
{% endwith %}
{% endblock %}
<input
type="hidden"
name="csrf_token"
id="csrf_token"
value="{{ csrf_token() if csrf_token else '' }}"
>
</head>

<body>
Expand All @@ -38,12 +44,6 @@
<div id="app" data-bootstrap="{{ bootstrap_data }}" >
<img src="/static/assets/images/loading.gif" style="width: 50px; margin: 10px;">
</div>
<input
type="hidden"
name="csrf_token"
id="csrf_token"
value="{{ csrf_token() if csrf_token else '' }}"
>
{% endblock %}

<!-- Modal for misc messages / alerts -->
Expand Down
6 changes: 0 additions & 6 deletions superset/templates/superset/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,4 @@
<div id="grid-container" class="slice-grid gridster"></div>

</div>
<input
type="hidden"
name="csrf_token"
id="csrf_token"
value="{{ csrf_token() if csrf_token else '' }}"
>
{% endblock %}
11 changes: 10 additions & 1 deletion superset/templates/superset/models/database/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
$("#testconn").click(function(e) {
e.preventDefault();
var url = "/superset/testconn";
var csrf_token = "{{ csrf_token() }}";

$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrf_token);
}
}
});

var data = {};
try{
data = JSON.stringify({
uri: $("#sqlalchemy_uri").val(),
name: $('#database_name').val(),
extras: JSON.parse($("#extra").val())
extras: JSON.parse($("#extra").val()),
})
} catch(parse_error){
alert("Malformed JSON in the extras field: " + parse_error);
Expand Down

0 comments on commit c29395f

Please sign in to comment.