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

Add custom request header to prevent CSRF #4987

Merged
merged 1 commit into from
Aug 10, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.net.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.glassfish.jersey.client.filter.CsrfProtectionFilter;
import org.graylog2.cluster.Node;
import org.graylog2.security.realm.SessionAuthenticator;
import retrofit2.Retrofit;
Expand All @@ -46,6 +47,7 @@ public <T> T get(Node node, final String authorizationToken, Class<T> interfaceC

Request.Builder builder = original.newBuilder()
.header(HttpHeaders.ACCEPT, MediaType.JSON_UTF_8.toString())
.header(CsrfProtectionFilter.HEADER_NAME, "Graylog Server")
.method(original.method(), original.body());

if (authorizationToken != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.filter.CsrfProtectionFilter;
import org.glassfish.jersey.server.model.Resource;
import org.graylog2.audit.PluginAuditEventTypes;
import org.graylog2.audit.jersey.AuditEventModelProcessor;
Expand Down Expand Up @@ -239,6 +240,7 @@ private ResourceConfig buildResourceConfig(final boolean enableCors,
.register(new PrefixAddingModelProcessor(packagePrefixes))
.register(new AuditEventModelProcessor(pluginAuditEventTypes))
.registerClasses(
CsrfProtectionFilter.class,
JacksonJaxbJsonProvider.class,
JsonProcessingExceptionMapper.class,
JacksonPropertyExceptionMapper.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void filter(ContainerRequestContext requestContext, ContainerResponseCont
if (origin != null && !origin.isEmpty()) {
responseContext.getHeaders().add("Access-Control-Allow-Origin", origin);
responseContext.getHeaders().add("Access-Control-Allow-Credentials", true);
responseContext.getHeaders().add("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Graylog-No-Session-Extension, X-Requested-With");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Graylog-No-Session-Extension, X-Requested-With, X-Requested-By");
responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
// In order to avoid redoing the preflight thingy for every request, see http://stackoverflow.com/a/12021982/1088469
responseContext.getHeaders().add("Access-Control-Max-Age", "600"); // 10 minutes seems to be the maximum allowable value
Expand All @@ -57,7 +57,7 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
options.header("Access-Control-Allow-Origin", origin);
options.header("Access-Control-Allow-Credentials", true);
options.header("Access-Control-Allow-Headers",
"Authorization, Content-Type, X-Graylog-No-Session-Extension, X-Requested-With");
"Authorization, Content-Type, X-Graylog-No-Session-Extension, X-Requested-With, X-Requested-By");
options.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
// In order to avoid redoing the preflight thingy for every request, see http://stackoverflow.com/a/12021982/1088469
options.header("Access-Control-Max-Age", "600"); // 10 minutes seems to be the maximum allowable value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
};
$('#input_apiUser').change(updateApiAuth);
$('#input_apiPassword').change(updateApiAuth);

// Add CSRF header
window.authorizations.add("csrf", new ApiKeyAuthorization("X-Requested-By", "Graylog API Browser", "header"));

window.swaggerUi.load();
});

Expand Down
4 changes: 3 additions & 1 deletion graylog2-web-interface/src/logic/rest/FetchProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export class FetchError extends Error {

export class Builder {
constructor(method, url) {
this.request = request(method, url.replace(/([^:])\/\//, '$1/')).set('X-Requested-With', 'XMLHttpRequest');
this.request = request(method, url.replace(/([^:])\/\//, '$1/'))
.set('X-Requested-With', 'XMLHttpRequest')
.set('X-Requested-By', 'XMLHttpRequest');
}

authenticated() {
Expand Down