| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 | [[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-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.[[allowed-script-types-setting]][float]=== Allowed script types settingBy default all script types are allowed to be executed.  This can be modified using thesetting `script.allowed_types`.  Only the types specified as part of the setting will beallowed to be executed.  To specify no types are allowed, set `script.allowed_types` tobe `none`.[source,yaml]----script.allowed_types: inline <1>----<1> This will allow only inline scripts to be executed but not stored scripts(or any other types).[[allowed-script-contexts-setting]][float]=== Allowed script contexts settingBy default all script contexts are allowed to be executed.  This can be modified using thesetting `script.allowed_contexts`.  Only the contexts specified as part of the setting willbe allowed to be executed.  To specify no contexts are allowed, set `script.allowed_contexts`to be `none`.[source,yaml]----script.allowed_contexts: search, update <1>----<1> This will allow only search and update scripts to be executed but notaggs or plugin scripts (or any other contexts).
 |