- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试过滤/修改 Post 和 Put 调用,以确保用户提供的所有参数都从 HTML 和 JS 代码中过滤出来,以防止 XSS 攻击。我想确保这是在 API 级别实现的,因此无论使用什么客户端,它都会受到保护。
在 Jersey 1.x 中,这可以通过实现 ContainerRequestFilter 并在它们与请求的 servlet 匹配之前修改 request.getQueryParameters() 来实现。示例:http://codehustler.org/blog/jersey-cross-site-scripting-xss-filter-for-java-web-apps/
然而,对于 Jersey 2,这不可能通过实现相同的接口(interface)来实现,因为我们不能再使用 getQueryParameters() 或 getPathParameters(),而是只能使用 getUriInfo(),但是由于查询参数是不可变的。我查看了 Jersey 的 Filters and Interceptors但不幸的是,它们仅限于提供对 header 和 cookie 的访问权限。
我花了很多时间研究,但找不到我要找的东西。
是否有其他方法来过滤路径和查询参数?有什么我想念的吗?
谢谢!
最佳答案
我在下面添加了一个适用于 Jersey 2.x 的过滤器。但是,它不会对 Cookie 执行 XSS 修复,因为我还没有找到修改它们的方法。
重要的是要注意,这需要与 POJO 属性上的@SafeHtml 结合使用,以便清理这些值。
@PreMatching
public class XSSFilter implements ContainerRequestFilter
{
/**
* @see ContainerRequestFilter#filter(ContainerRequest)
*/
@Override
public void filter( ContainerRequestContext request )
{
cleanQueryParams( request );
cleanHeaders( request.getHeaders() );
}
/**
* Replace the existing query parameters with ones stripped of XSS vulnerabilities
* @param request
*/
private void cleanQueryParams( ContainerRequestContext request )
{
UriBuilder builder = request.getUriInfo().getRequestUriBuilder();
MultivaluedMap<String, String> queries = request.getUriInfo().getQueryParameters();
for( Map.Entry<String, List<String>> query : queries.entrySet() )
{
String key = query.getKey();
List<String> values = query.getValue();
builder.replaceQueryParam( key );
for( String value : values ) {
builder.replaceQueryParam( key, Utils.stripXSS( value ) );
}
}
request.setRequestUri( builder.build() );
}
/**
* Replace the existing headers with ones stripped of XSS vulnerabilities
* @param headers
*/
private void cleanHeaders( MultivaluedMap<String, String> headers )
{
for( Map.Entry<String, List<String>> header : headers.entrySet() )
{
String key = header.getKey();
List<String> values = header.getValue();
List<String> cleanValues = new ArrayList<String>();
for( String value : values ) {
cleanValues.add( Utils.stripXSS( value ) );
}
headers.put( key, cleanValues );
}
}
}
stripXSS 函数如下:
/**
* Strips any potential XSS threats out of the value
*
* @param value
* @return
*/
public static String stripXSS( String value )
{
return stripXSS( value, Whitelist.none() );
}
/**
* Strips any potential XSS threats out of the value excluding
* the white listed HTML
*
* @param value
* @param whitelist
* @return
*/
public static String stripXSS( String value, Whitelist whitelist )
{
if( StringUtils.isBlank( value ) )
return value;
// Use the ESAPI library to avoid encoded attacks.
value = ESAPI.encoder().canonicalize( value );
// Avoid null characters
value = value.replaceAll("\0", "");
// Clean out HTML
Document.OutputSettings outputSettings = new Document.OutputSettings();
outputSettings.escapeMode( EscapeMode.xhtml );
outputSettings.prettyPrint( false );
value = Jsoup.clean( value, "", whitelist, outputSettings );
return value;
}
同时更新了原帖:http://codehustler.org/blog/jersey-cross-site-scripting-xss-filter-for-java-web-apps/
关于java - 如何在 Jersey 2 中修改 QueryParam 和 PathParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32939919/
我使用 vuejs-axios 从客户端获取数据并将其发布到服务器端。在服务器端,我使用了 java,而我使用 SparkJAVA 框架来处理请求响应。下面提到的代码块是我用来将表单数据从 vuejs
您好,使用多路由构建 Angular 应用。 导航到页面 > queryParams 触发订阅。 当我在页面中时,我需要更改页面内的 queryParams 但不会再次触发订阅功能。 (因为我不想再次
我正在寻找在 Spring 中使用 @QueryParam 的简单示例。我的想法是有一个像这样的链接,它将获取 id 值,运行我的 java 类并显示返回的字符串。我的问题是没有找到返回值。 http
我有以下格式从浏览器发送的查询参数 sort[0][field]:prodId sort[0][dir]:asc 如何使用@QueryParam在服务器中检索上述参数? 从 Chrome 控制台 ta
我正在创建一个端点(使用 Jersey 注释),该端点接收字符串列表作为参数。字符串需要包含一个键和一个值。所以在 url 上它看起来像 ?params=key1=value1¶ms=key2
我正在尝试从 REST Web 服务的 URL 请求中获取请求参数。 @Path 能够映射方法。但 QueryParam 无法从查询参数中获取值。 我的请求网址是 192.168.20.147:808
我必须从客户端 java 类调用一个 Restful Web 服务。 我需要传递 HashMap、String,它必须返回一个 bean 列表。我正在使用 Jersey Restful 网络服务 我的
我正在使用 Jersey for Rest 并有一个接受 @QueryParam. 的方法 但是,用户可以像这样发送@QueryParam.: contractName# 其中 # 是 0-155 之
这是函数头 @GET @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Path("/getxyz") pub
我正在尝试更新(添加、删除)组件中的 queryParams。在 angularJS 中,它曾经是可能的,这要归功于: $location.search('f', 'filters[]'); // s
我有一个 Express 服务器,并且在我的路由中配置了: app.use(express.urlencoded({ extended: false })); 在前端,我使用 Angular 发送请求
给出以下代码: newtype HelloMessage = HelloMessage { msg :: String } deriving (Generic) instance ToJSON H
我有一个 RESTful 服务。这需要两个参数:开始日期和结束日期。 如果我使用 @RequestParam 注释,我就会得到我需要的东西。但如果我使用 @QueryParam,我注意到即使传递它,它
有没有办法在不使用 @QueryParam 注释显式声明其名称的情况下获取任何查询参数? 我有一个字符串,其中可能包含诸如 {animal} 之类的标记,然后将其替换为发布的查询参数,例如?anima
当我在 JAX-RS 中添加字符串的默认值时,它不会获取该值。它保持为 null 或空。 @QueryParam("status") private String status = "confirme
例如,我有课 class MyQuery { Date start; Date end; ChronoUnit scale; // ChronoUnit is enum } 我想声明
对于以下示例(使用 Jersey 2.6),百分比编码的查询参数未解码,相反,+ 被空格替换。 @Path("test") public class TestResource { @Path(
我目前正在尝试了解如何在 Angular 组件中收集 queryParameters。因为我只想在初始化期间收集它们一次,所以我首先尝试了快照模式: ngOnInit() { console.
我想在我的应用程序中提供一个基于来自 url 的 QueryParam 的常量。 localhost:4200?flag=true 在我添加的模块提供者中 { provide: FLAG, useFa
我正在尝试使用 Angular 2.0 捕获查询字符串参数,但遇到了困难。我有一个这样的网址 http://localhost:5000/?id_token=eyJ0eXAiO .... 而且我希望能
我是一名优秀的程序员,十分优秀!