Standard regex to prevent SPARQL injection?

房东的猫 提交于 2019-12-24 03:44:14

问题


I am developing a RESTful API in scala that sends SPARQL-queries to a triplestore. I read an article about the threat of SPARQL-injections when user defined params (URL params in a HTTP get request) are directly put in the SPARQL (e.g. in a fulltext search).

I saw some Jena specific solutions (using its Java API) that are not useful for me.

Is there some kind of standard regex search and replace pattern to escape malicious characters in a string that should be integrated in a SPARQL-query?


回答1:


If the user input goes into a string literal within the query:

  1. Use the triple-quote form: """...""" or '''...'''
  2. Prepend any backslash, single quote or double quote in the user input with a backslash

ES6 example:

const escapeForTurtle: s => s.replace(/(["'\\])/g, '\\$1')

const query = `SELECT * { ?x :name """${escapeForTurtle(name)}""" }`

This is enough to prevent injection and syntax errors from unusual user input.

Depending on the triple store, some extra work might be required if you want to make sure that absolutely every possible user input arrives in the store without any lost characters or extra backslashes. The input \\u0022 is a good test case. If that goes through without modification, you should be safe and done.



来源:https://stackoverflow.com/questions/29601839/standard-regex-to-prevent-sparql-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!