Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAT-7204: Add WAF Intercept #83

Merged
merged 8 commits into from
Jun 17, 2024
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
39 changes: 25 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"@babel/runtime": "^7.15.3",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
"@types/testing-library__jest-dom": "^5.14.1",
"@types/jest": "^27.0.1",
"@types/react": "^17.0.19",
"@types/react-dom": "^17.0.9",
"@types/systemjs": "^6.1.1",
"@types/testing-library__jest-dom": "^5.14.1",
"@types/webpack-env": "^1.16.2",
"babel-jest": "^27.0.6",
"babel-plugin-macros": "^3.1.0",
Expand Down Expand Up @@ -57,14 +57,15 @@
},
"dependencies": {
"@madie/madie-models": "^1.3.11",
"axios": "^1.6.7",
"dompurify": "^3.1.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"rxjs": "^7.5.5",
"single-spa": "^5.9.3",
"single-spa-react": "^4.3.1",
"styled-components": "^5.3.5",
"twin.macro": "^2.8.2",
"axios": "^1.6.7"
"twin.macro": "^2.8.2"
},
"types": "dist/madie-madie-util.d.ts",
"overrides": {
Expand Down
2 changes: 2 additions & 0 deletions src/madie-madie-util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getServiceConfig } from "./Config/Config";
import { default as useKeyPress } from "./hooks/useKeyPress";
import { default as useOktaTokens } from "./hooks/useOktaTokens";
import { default as useOnClickOutside } from "./hooks/useOnClickOutside";
import { default as wafIntercept } from "./util/wafIntercept";
import { measureStore } from "./Store/measureStore";
import { cqlLibraryStore } from "./Store/cqlLibraryStore";
import { routeHandlerStore } from "./Store/routeHandlerStore";
Expand Down Expand Up @@ -37,4 +38,5 @@ export {
checkUserCanDelete,
useFeatureFlags,
getOidFromString,
wafIntercept,
};
35 changes: 35 additions & 0 deletions src/util/wafIntercept.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import axios from "axios";
import DOMPurify from "dompurify";

const wafIntercept = () =>
axios.interceptors.response.use(
(response) => {
return response;
},
(error) => {
// Check for WAF block
if (
error?.response?.status === 403 &&
error?.response?.headers["content-type"].includes("text/html") &&
JSON.stringify(error.response.data).includes("[email protected]")
) {
// eslint-disable-next-line no-console
console.log("WAF Interceptor Triggered");

const supportID = error.response.data.includes("ID:")
? error.response.data.split("ID:")[1].split("<br>")[0].trim()
: "";
const body = error.response.data.split("<body>")[1].split("<br>")[0];
const purifiedBody = DOMPurify.sanitize(body, { ALLOWED_TAGS: [] });

const wafEvent = new CustomEvent("wafReject", {
detail: { message: purifiedBody, supportId: supportID },
});
document.dispatchEvent(wafEvent);
throw new Error(purifiedBody); // no tags allowed, removes all HTML tags.
}

return Promise.reject(error);
}
);
export default wafIntercept;
Loading