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

Patches to prevent XSS #893

Merged
merged 19 commits into from
Oct 10, 2019
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
25 changes: 25 additions & 0 deletions engine/src/main/java/io/seldon/engine/filters/XSSFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.seldon.engine.filters;

import java.io.IOException;

import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;

@Component
public class XSSFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
// Add nosniff option to avoid content sniffing by the browser
response.addHeader("X-Content-Type-Options", "nosniff");

filterChain.doFilter(request, response);
}
}
2 changes: 1 addition & 1 deletion engine/src/main/java/io/seldon/engine/pb/JsonFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ private static final class PrinterImpl {
private final CharSequence blankOrNewLine;

private static class GsonHolder {
private static final Gson DEFAULT_GSON = new GsonBuilder().disableHtmlEscaping().create();
private static final Gson DEFAULT_GSON = new GsonBuilder().create();
}

PrinterImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import io.seldon.engine.filters.XSSFilter;
import io.seldon.engine.pb.JsonFormat;
import io.seldon.engine.predictors.EnginePredictor;
import io.seldon.engine.service.InternalPredictionService;
Expand Down Expand Up @@ -64,6 +65,7 @@ public class TestRandomABTest {
public void setup() throws Exception {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.addFilters(new XSSFilter())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.seldon.engine.filters.XSSFilter;
import io.seldon.engine.pb.ProtoBufUtils;
import io.seldon.engine.tracing.TracingProvider;
import io.seldon.protos.PredictionProtos.SeldonMessage;
import io.opentracing.mock.MockTracer;
import io.opentracing.mock.MockSpan;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -43,11 +46,12 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import static org.mockito.Mockito.when;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
//@AutoConfigureMockMvc
// @AutoConfigureMockMvc
public class TestRestClientController {
private final static Logger logger = LoggerFactory.getLogger(TestRestClientController.class);

Expand All @@ -69,6 +73,7 @@ public void setup() {
when(mockTracingProvider.isActive()).thenReturn(true);
mvc = MockMvcBuilders
.webAppContextSetup(context)
.addFilters(new XSSFilter())
.build();
}

Expand All @@ -84,6 +89,17 @@ public void testPing() throws Exception
Assert.assertEquals("pong", response);
Assert.assertEquals(200, res.getResponse().getStatus());
}

@Test
public void testSecurityHeaders() throws Exception
{
MvcResult res = mvc.perform(MockMvcRequestBuilders.get("/ping")).andReturn();
HttpServletResponse response = res.getResponse();

final String noSniff = response.getHeader("X-Content-Type-Options");
Assert.assertEquals("nosniff", noSniff);
Assert.assertEquals(200, response.getStatus());
}

@Test
public void testPredict_activateSpan() throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import io.seldon.engine.filters.XSSFilter;
import io.seldon.engine.pb.JsonFormat;
import io.seldon.engine.predictors.EnginePredictor;
import io.seldon.engine.service.InternalPredictionService;
Expand Down Expand Up @@ -67,6 +68,7 @@ public void setup() throws Exception {

mvc = MockMvcBuilders
.webAppContextSetup(context)
.addFilters(new XSSFilter())
.build();
}

Expand Down
29 changes: 29 additions & 0 deletions engine/src/test/java/io/seldon/engine/filters/TestXSSFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.seldon.engine.filters;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.FilterChain;

public class TestXSSFilter {
@Test
public void testSecurityHeaders() throws ServletException, IOException {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = mock(FilterChain.class);

XSSFilter filter = new XSSFilter();
filter.doFilter(request, response, chain);

verify(response).addHeader("X-Content-Type-Options", "nosniff");
verify(chain).doFilter(request, response);
}
}
81 changes: 81 additions & 0 deletions engine/src/test/java/io/seldon/engine/pb/TestJsonFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
* Copyright 2019 Seldon Technologies Ltd (http://www.seldon.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package io.seldon.engine.pb;

import com.google.protobuf.InvalidProtocolBufferException;

import io.kubernetes.client.proto.IntStr.IntOrString;

import org.junit.Assert;
import org.junit.Test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.seldon.engine.pb.JsonFormat.Printer;

public class TestJsonFormat {
private final static Logger logger = LoggerFactory.getLogger(TestJsonFormat.class);

@Test
public void testStrValCustomFormat() throws InvalidProtocolBufferException
{
final String val = "String Value";
IntOrString is = IntOrString.newBuilder().setStrVal(val).build();
Printer jf = JsonFormat.printer().usingTypeConverter(is.getDescriptorForType().getFullName(), new IntOrStringUtils.IntOrStringConverter());
Assert.assertEquals("\""+val+"\"", jf.print(is));
}

@Test
public void testEscapeHTML() throws InvalidProtocolBufferException
{
final String val = "<div class=\"div-class\"></div>";
final String escaped = "\\u003cdiv class\\u003d\\\"div-class\\\"\\u003e\\u003c/div\\u003e";
final String expected = String.format("{\"strVal\":\"%s\"}", escaped);
IntOrString is = IntOrString.newBuilder().setStrVal(val).build();
Printer jf = JsonFormat.printer().omittingInsignificantWhitespace();
final String json = jf.print(is);
Assert.assertEquals(expected, json);
}

@Test
public void testIntValCustomFormat() throws InvalidProtocolBufferException
{
final int val = 1;
IntOrString is = IntOrString.newBuilder().setIntVal(val).build();
Printer jf = JsonFormat.printer().usingTypeConverter(is.getDescriptorForType().getFullName(), new IntOrStringUtils.IntOrStringConverter());
Assert.assertEquals(""+val, jf.print(is));
}

@Test
public void testIntValDefaultFormat() throws InvalidProtocolBufferException
{
final int val = 1;
IntOrString is = IntOrString.newBuilder().setIntVal(val).build();
Printer jf = JsonFormat.printer().omittingInsignificantWhitespace();
Assert.assertEquals("{\"intVal\":"+val+"}", jf.print(is));
}

@Test
public void testStrValDefaultFormat() throws InvalidProtocolBufferException
{
final String val = "String Value";
IntOrString is = IntOrString.newBuilder().setStrVal(val).build();
Printer jf = JsonFormat.printer().omittingInsignificantWhitespace();
Assert.assertEquals("{\"strVal\":\""+val+"\"}", jf.print(is));
}
}

4 changes: 4 additions & 0 deletions examples/models/xss/.s2i/environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODEL_NAME=XSSModel
API_TYPE=REST
SERVICE_TYPE=MODEL
PERSISTENCE=0
6 changes: 6 additions & 0 deletions examples/models/xss/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
IMAGE_NAME=xss-model
IMAGE_VERSION=0.1

build_image:
s2i build . seldonio/seldon-core-s2i-python3:0.7 ${IMAGE_NAME}:${IMAGE_VERSION}

7 changes: 7 additions & 0 deletions examples/models/xss/XSSModel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class XSSModel(object):
"""
Dummy model which just returns its input back.
"""

def predict(self, X, feature_names):
return X
Loading