Max Weber
2016-05-09 10:14:52 UTC
Hi,
I like to expose datomic.api/q via an API. I'm trying to find out the steps
to make this approach "secure".
Let's assume that the database only contains public data or is
appropriately filtered by datomic.api/filter.
The most obvious security flaw of this approach is that a Datalog query is
allowed to execute arbitrary Clojure functions and Java methods:
(datomic.api/q
'[:find ?r .
:where [(System/getenv) ?r]]
nil)
However "safe" functions like clojure.string/starts-with? are very useful
for Datomic queries and should be allowed. A first action to make the
approach more "secure" would be to define a whitelist of all allowed
functions and check fn-expr and pred-expr against this whitelist.
Besides the whitelist I would prefer to write some kind of parser for
Datomic's query grammar (http://docs.datomic.com/query.html#sec-4) and only
allow the elements that are necessary for the corresponding API clients.
This would be quite a lot of effort, therefore I'm looking for alternatives.
Another issue is to constrain the query execution time. But I assume the
:timeout functionality of datomic.api/query should do the job.
Furthermore the arguments for datomic.api/q are quite flexible. The query
can be a map, list, or string. Here it would be necessary to make sure that
a query as string is blocked. A query string is harder to check and it
could yield other security flaws, since maybe Datomic read it with
clojure.core/read-string.
I guess that I overlook some other security flaws? What would be your
recommendations to make the described approach feasible?
Best regards
Max
P.S. I'm aware of other developments in this area like Relay / GraphQL,
Falcor or Datomic DataScript synchronization
(https://github.com/metasoarous/datsync). My motivation is just to expose
the full power of Datomic's datalog to my ClojureScript app and to avoid
wrapping each query requirement in some clumsy API endpoint.
I like to expose datomic.api/q via an API. I'm trying to find out the steps
to make this approach "secure".
Let's assume that the database only contains public data or is
appropriately filtered by datomic.api/filter.
The most obvious security flaw of this approach is that a Datalog query is
allowed to execute arbitrary Clojure functions and Java methods:
(datomic.api/q
'[:find ?r .
:where [(System/getenv) ?r]]
nil)
However "safe" functions like clojure.string/starts-with? are very useful
for Datomic queries and should be allowed. A first action to make the
approach more "secure" would be to define a whitelist of all allowed
functions and check fn-expr and pred-expr against this whitelist.
Besides the whitelist I would prefer to write some kind of parser for
Datomic's query grammar (http://docs.datomic.com/query.html#sec-4) and only
allow the elements that are necessary for the corresponding API clients.
This would be quite a lot of effort, therefore I'm looking for alternatives.
Another issue is to constrain the query execution time. But I assume the
:timeout functionality of datomic.api/query should do the job.
Furthermore the arguments for datomic.api/q are quite flexible. The query
can be a map, list, or string. Here it would be necessary to make sure that
a query as string is blocked. A query string is harder to check and it
could yield other security flaws, since maybe Datomic read it with
clojure.core/read-string.
I guess that I overlook some other security flaws? What would be your
recommendations to make the described approach feasible?
Best regards
Max
P.S. I'm aware of other developments in this area like Relay / GraphQL,
Falcor or Datomic DataScript synchronization
(https://github.com/metasoarous/datsync). My motivation is just to expose
the full power of Datomic's datalog to my ClojureScript app and to avoid
wrapping each query requirement in some clumsy API endpoint.
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.