Skip to content

CSRF header should not be sent to cross domain sites #1469 #1470

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

Merged
merged 2 commits into from
Jan 26, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,21 @@ private String addParameter(String html, String key, String value) {
*/
protected String addCSRF(String html) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("requestInterceptor: (request) => {\n");
stringBuilder.append("const value = `; ${document.cookie}`;\n");
stringBuilder.append("const parts = value.split(`; ");
stringBuilder.append(swaggerUiConfig.getCsrf().getCookieName());
stringBuilder.append("=`);\n");
stringBuilder.append("if (parts.length === 2)\n");
stringBuilder.append("request.headers['");
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
stringBuilder.append("'] = parts.pop().split(';').shift();\n");
stringBuilder.append("return request;\n");
stringBuilder.append("},\n");
stringBuilder.append(PRESETS);
stringBuilder.append("requestInterceptor: (request) => {\n");
stringBuilder.append("\t\t\tconst value = `; ${document.cookie}`;\n");
stringBuilder.append("\t\t\tconst parts = value.split(`; ");
stringBuilder.append(swaggerUiConfig.getCsrf().getCookieName());
stringBuilder.append("=`);\n");
stringBuilder.append("\t\t\tconst currentURL = new URL(document.URL);\n");
stringBuilder.append("\t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
stringBuilder.append("\t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
stringBuilder.append("\t\t\tif (isSameOrigin && parts.length === 2) ");
stringBuilder.append("request.headers['");
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
stringBuilder.append("'] = parts.pop().split(';').shift();\n");
stringBuilder.append("\t\t\treturn request;\n");
stringBuilder.append("\t\t},\n");
stringBuilder.append("\t\t" + PRESETS);
return html.replace(PRESETS, stringBuilder.toString());
}

Expand All @@ -223,14 +226,18 @@ protected String addCSRF(String html) {
protected String addCSRFLocalStorage(String html) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("requestInterceptor: (request) => {\n");
stringBuilder.append("const value = window.localStorage.getItem('");
stringBuilder.append("t\t\tconst value = window.localStorage.getItem('");
stringBuilder.append(swaggerUiConfig.getCsrf().getLocalStorageKey() + "');\n");
stringBuilder.append("t\t\tconst currentURL = new URL(document.URL);\n");
stringBuilder.append("t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
stringBuilder.append("t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
stringBuilder.append("t\t\tif (isSameOrigin) ");
stringBuilder.append("request.headers['");
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
stringBuilder.append("'] = value;\n");
stringBuilder.append("return request;\n");
stringBuilder.append("},\n");
stringBuilder.append(PRESETS);
stringBuilder.append("t\t\treturn request;\n");
stringBuilder.append("\t\t},\n");
stringBuilder.append("\t\t" + PRESETS);
return html.replace(PRESETS, stringBuilder.toString());
}

Expand Down