| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 | [[modules-scripting-security]]=== Scripting and securityWhile Elasticsearch contributors make every effort to prevent scripts fromrunning amok, security is something best done inhttps://en.wikipedia.org/wiki/Defense_in_depth_(computing)[layers] becauseall software has bugs and it is important to minimize the risk of failure inany security layer. Find below rules of thumb for how to keep Elasticsearchfrom being a vulnerability.[float]=== Do not run as rootFirst and foremost, never run Elasticsearch as the `root` user as this wouldallow any successful effort to circumvent the other security layers to do*anything* on your server. Elasticsearch will refuse to start if it detectsthat it is running as `root` but this is so important that it is worth doubleand triple checking.[float]=== Do not expose Elasticsearch directly to usersDo not expose Elasticsearch directly to users, instead have an applicationmake requests on behalf of users. If this is not possible, have an applicationto sanitize requests from users. If *that* is not possible then have somemechanism to track which users did what. Understand that it is quite possibleto write a <<search, `_search`>> that overwhelms Elasticsearch and brings downthe cluster. All such searches should be considered bugs and the Elasticsearchcontributors make an effort to prevent this but they are still possible.[float]=== Do not expose Elasticsearch directly to the InternetDo not expose Elasticsearch to the Internet, instead have an applicationmake requests on behalf of the Internet. Do not entertain the thought of havingan application "sanitize" requests to Elasticsearch. Understand that it ispossible for a sufficiently determined malicious user to write searches thatoverwhelm the Elasticsearch cluster and bring it down. For example:Good:* Users type text into a search box and the text is sent directly to a<<query-dsl-match-query>>, <<query-dsl-match-query-phrase>>,<<query-dsl-simple-query-string-query>>, or any of the <<search-suggesters>>.* Running a script with any of the above queries that was written as part ofthe application development process.* Running a script with `params` provided by users.* User actions makes documents with a fixed structure.Bad:* Users can write arbitrary scripts, queries, `_search` requests.* User actions make documents with structure defined by users.[float][[modules-scripting-security-do-no-weaken]]=== Do not weaken script security settingsBy default Elasticsearch will run inline, stored, and filesystem scripts forthe builtin languages, namely the scripting language Painless, the templatelanguage Mustache, and the expression language Expressions. These *ought* to besafe to expose to trusted users and to your application servers because theyhave strong security sandboxes. The Elasticsearch committers do not support anynon-sandboxed scripting languages and using any would be a poor choice because:1. This drops a layer of security, leaving only Elasticsearch's builtin<<modules-scripting-other-layers, security layers>>.2. Non-sandboxed scripts have unchecked access to Elasticsearch's internals andcan cause all kinds of trouble if misused.[float][[modules-scripting-other-layers]]=== Other security layersIn addition to user privileges and script sandboxing Elasticsearch uses thehttp://www.oracle.com/technetwork/java/seccodeguide-139067.html[Java Security Manager]and native security tools as additional layers of security.As part of its startup sequence Elasticsearch enables the Java Security Managerwhich limits the actions that can be taken by portions of the code. Painlessuses this to limit the actions that generated Painless scripts can take,preventing them from being able to do things like write files and listen tosockets.Elasticsearch useshttps://en.wikipedia.org/wiki/Seccomp[seccomp] in Linux,https://www.chromium.org/developers/design-documents/sandbox/osx-sandboxing-design[Seatbelt]in macOS, andhttps://msdn.microsoft.com/en-us/library/windows/desktop/ms684147[ActiveProcessLimit]on Windows to prevent Elasticsearch from forking or executing other processes.Below this we describe the security settings for scripts and how you canchange from the defaults described above. You should be very, very carefulwhen allowing more than the defaults. Any extra permissions weakens the totalsecurity of the Elasticsearch deployment.[[security-script-source]][float]=== Script source settingsWhich scripts Elasticsearch will execute where is controlled by settingsstarting with `scripts.`. The simplest settings allow scripts to be enabledor disabled based on where they are stored. For example:[source,yaml]-----------------------------------script.inline: false  <1>script.stored: false  <2>script.file:   true   <3>-----------------------------------<1> Refuse to run scripts provided inline in the API.<2> Refuse to run scripts stored using the API.<3> Run scripts found on the filesystem in `/etc/elasticsearch/scripts`(rpm or deb) or `config/scripts` (zip or tar).NOTE: These settings override the defaults mentioned<<modules-scripting-security-do-no-weaken, above>>. Recreating the defaultsrequires more fine grained settings described <<security-script-fine, below>>.[[security-script-context]][float]=== Script context settingsScripting may also be enabled or disabled in different contexts in theElasticsearch API. The supported contexts are:[horizontal]`aggs`::    Aggregations`search`::  Search api, Percolator API and Suggester API`update`::  Update api`plugin`::  Any plugin that makes use of scripts under the generic `plugin` categoryPlugins can also define custom operations that they use scripts for insteadof using the generic `plugin` category. Those operations can be referred toin the following form: `${pluginName}_${operation}`.The following example disables scripting for `update` and `plugin` operations,regardless of the script source or language. Scripts can still be executedas part of `aggregations`, `search` and plugins execution though, as the abovedefaults still get applied.[source,yaml]-----------------------------------script.update: falsescript.plugin: false-----------------------------------[[security-script-fine]][float]=== Fine-grained script settingsFirst, the high-level script settings described above are applied in order(context settings have precedence over source settings). Then fine-grainedsettings which include the script language take precedence over any high-levelsettings. They have two forms:[source,yaml]------------------------script.engine.{lang}.{inline|file|stored}.{context}: true|false------------------------And[source,yaml]------------------------script.engine.{lang}.{inline|file|stored}: true|false------------------------For example:[source,yaml]-----------------------------------script.inline: false <1>script.stored: false <1>script.file:   false <1>script.engine.painless.inline:          true <2>script.engine.painless.stored.search:   true <3>script.engine.painless.stored.aggs:     true <3>script.engine.mustache.stored.search:   true <4>-----------------------------------<1> Disable all scripting from any source.<2> Allow inline Painless scripts for all operations.<3> Allow stored Painless scripts to be used for search and aggregations.<4> Allow stored Mustache templates to be used for search.
 |