generated from MeasureAuthoringTool/madie-frontend-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from MeasureAuthoringTool/MAT-7204a
MAT-7204: add waf intercept to axios calls
- Loading branch information
Showing
5 changed files
with
42 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import axios from "axios"; | ||
import wafIntercept from "./wafIntercept"; | ||
|
||
export const axiosInstance = axios.create(); | ||
axiosInstance.interceptors.response.use((response) => { | ||
return response; | ||
}, wafIntercept); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,28 @@ | ||
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 wafIntercept = (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 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. | ||
} | ||
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); | ||
} | ||
); | ||
return Promise.reject(error); | ||
}; | ||
export default wafIntercept; |