Skip to content

Commit

Permalink
Merge pull request #84 from MeasureAuthoringTool/MAT-7204a
Browse files Browse the repository at this point in the history
MAT-7204: add waf intercept to axios calls
  • Loading branch information
ethankaplan authored Jul 1, 2024
2 parents fed3d56 + fd304b8 commit 3c1e294
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/Config/Config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from "axios";
import { wafIntercept } from "../madie-madie-util";

export interface OktaConfig {
baseUrl: string;
Expand Down Expand Up @@ -32,6 +33,9 @@ export async function getServiceConfig(): Promise<ServiceConfig> {
) {
throw new Error("Invalid Service Config");
}
axios.interceptors.response.use((response) => {
return response;
}, wafIntercept);

return serviceConfig;
}
5 changes: 5 additions & 0 deletions src/api/useOrganizationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from "axios";
import { ServiceConfig, getServiceConfig } from "../Config/Config";
import useOktaTokens from "../hooks/useOktaTokens";
import { Organization } from "@madie/madie-models";
import { wafIntercept } from "../madie-madie-util";

export class OrganizationApi {
constructor(private getAccessToken: () => string) {}
Expand Down Expand Up @@ -35,6 +36,10 @@ export const getServiceUrl = async () => {
return serviceUrl;
};

axios.interceptors.response.use((response) => {
return response;
}, wafIntercept);

export default function useOrganizationApi(): OrganizationApi {
const { getAccessToken } = useOktaTokens();
return new OrganizationApi(getAccessToken);
Expand Down
4 changes: 4 additions & 0 deletions src/api/useTerminologyServiceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from "axios";
import { ServiceConfig, getServiceConfig } from "../Config/Config";

import useOktaTokens from "../hooks/useOktaTokens";
import { wafIntercept } from "../madie-madie-util";

export class TerminologyServiceApi {
constructor(private getAccessToken: () => string) {}
Expand Down Expand Up @@ -55,6 +56,9 @@ export const getServiceUrl = async () => {

return serviceUrl;
};
axios.interceptors.response.use((response) => {
return response;
}, wafIntercept);

export default function useTerminologyServiceApi(): TerminologyServiceApi {
const { getAccessToken } = useOktaTokens();
Expand Down
7 changes: 7 additions & 0 deletions src/util/axios-instance.ts
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);
51 changes: 22 additions & 29 deletions src/util/wafIntercept.ts
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;

0 comments on commit 3c1e294

Please sign in to comment.