Skip to content

Commit

Permalink
PPS-588 guppy csrf (#1464)
Browse files Browse the repository at this point in the history
* guppy csrf

* update pkg

* update action

* update guppy

* update guppy

* update guppy

* fix return

* update pkg
  • Loading branch information
mfshao authored and PlanXCyborg committed Dec 21, 2023
1 parent 84a235b commit bb3af3d
Show file tree
Hide file tree
Showing 7 changed files with 479 additions and 440 deletions.
854 changes: 455 additions & 399 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@fortawesome/free-brands-svg-icons": "^5.14.0",
"@fortawesome/free-solid-svg-icons": "^5.2.0",
"@fortawesome/react-fontawesome": "^0.2.0",
"@gen3/guppy": "^0.17.1",
"@gen3/guppy": "^0.18.0",
"@gen3/ui-component": "^0.11.4",
"@reactour/tour": "^2.12.0",
"@upsetjs/venn.js": "^1.4.2",
Expand Down
3 changes: 3 additions & 0 deletions src/GuppyDataExplorer/ExplorerFilter/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class ExplorerFilter extends React.Component {
accessibleFieldCheckList: this.props.accessibleFieldCheckList,
hideEmptyFilterSection: explorerHideEmptyFilterSection,
filterValuesToHide: explorerFilterValuesToHide,
csrfToken: this.props.csrfToken,
};
let filterFragment;
switch (this.state.selectedAccessFilter) {
Expand Down Expand Up @@ -215,6 +216,7 @@ ExplorerFilter.propTypes = {
unaccessibleFieldObject: PropTypes.object, // inherit from GuppyWrapper
adminAppliedPreFilters: PropTypes.object, // inherit from GuppyWrapper
accessibleFieldCheckList: PropTypes.arrayOf(PropTypes.string), // inherit from GuppyWrapper
csrfToken: PropTypes.string, // inherit from GuppyWrapper
getAccessButtonLink: PropTypes.string,
hideGetAccessButton: PropTypes.bool,
userFilterFromURL: PropTypes.object,
Expand All @@ -234,6 +236,7 @@ ExplorerFilter.defaultProps = {
unaccessibleFieldObject: {},
adminAppliedPreFilters: {},
accessibleFieldCheckList: [],
csrfToken: '',
getAccessButtonLink: undefined,
hideGetAccessButton: false,
userFilterFromURL: {},
Expand Down
2 changes: 2 additions & 0 deletions src/GuppyDataExplorer/GuppyDataExplorer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import GuppyWrapper from '@gen3/guppy/dist/components/GuppyWrapper';
import ExplorerVisualization from './ExplorerVisualization';
import ExplorerFilter from './ExplorerFilter';
import ExplorerTopMessageBanner from './ExplorerTopMessageBanner';
import { csrfToken } from '../configs';
import { labelToPlural, getQueryParameter, IsValidJSONString } from './utils';
import {
GuppyConfigType,
Expand Down Expand Up @@ -117,6 +118,7 @@ class GuppyDataExplorer extends React.Component {
onFilterChange={this.handleFilterChangeForQueryStateUrl}
rawDataFields={this.props.tableConfig.fields}
accessibleFieldCheckList={this.props.guppyConfig.accessibleFieldCheckList}
csrfToken={csrfToken}
>
<ExplorerTopMessageBanner
className='guppy-data-explorer__top-banner'
Expand Down
51 changes: 14 additions & 37 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,45 +206,22 @@ export const fetchWrapper = ({
// We first update the session so that the user will be notified
// if their auth is insufficient to perform the query.
export const fetchGraphQL = (graphQLParams) => sessionMonitor.updateSession()
.then(() => {
const request = {
credentials: 'include',
headers: { ...headers },
method: 'POST',
body: JSON.stringify(graphQLParams),
};

return fetch(graphqlPath, request)
.then((response) => response.text())
.then((responseBody) => {
try {
return JSON.parse(responseBody);
} catch (error) {
return responseBody;
}
});
});
.then(() => fetchWithCreds({ path: graphqlPath, body: JSON.stringify(graphQLParams), method: 'POST' })
.then((response) => {
if (response.status === 200 && response.data) {
return response.data;
}
return response;
}));

export const fetchFlatGraphQL = (graphQLParams) => sessionMonitor.updateSession()
.then(() => {
const request = {
credentials: 'include',
headers: { ...headers },
method: 'POST',
body: JSON.stringify(graphQLParams),
};

const graphqlUrl = guppyGraphQLUrl;
return fetch(graphqlUrl, request)
.then((response) => response.text())
.then((responseBody) => {
try {
return JSON.parse(responseBody);
} catch (error) {
return responseBody;
}
});
});
.then(() => fetchWithCreds({ path: guppyGraphQLUrl, body: JSON.stringify(graphQLParams), method: 'POST' })
.then((response) => {
if (response.status === 200 && response.data) {
return response.data;
}
return response;
}));

export const handleResponse = (type) => ({ data, status }) => {
switch (status) {
Expand Down
4 changes: 2 additions & 2 deletions src/configs.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { hostname } from './localconf';

export * from './localconf'; // / eslint-disable-line
const csrftoken = document.cookie.replace(/(?:(?:^|.*;\s*)csrftoken\s*=\s*([^;]*).*$)|^.*$/, '$1');
export const csrfToken = document.cookie.replace(/(?:(?:^|.*;\s*)csrftoken\s*=\s*([^;]*).*$)|^.*$/, '$1');

export const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
'x-csrf-token': csrftoken,
'x-csrf-token': csrfToken,
};

/**
Expand Down
3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const fs = require('fs');

const basename = process.env.BASENAME || '/';
const basenameWithTrailingSlash = basename.endsWith('/') ? basename : `${basename}/`;
const pathPrefix = basename.endsWith('/') ? basename.slice(0, basename.length - 1) : basename;
const app = process.env.APP || 'dev';

Expand Down Expand Up @@ -249,7 +250,7 @@ module.exports = {
output: {
path: __dirname,
filename: '[name].js',
publicPath: basename,
publicPath: basenameWithTrailingSlash,
},
optimization,
devtool,
Expand Down

0 comments on commit bb3af3d

Please sign in to comment.