security.asciidoc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. [[modules-scripting-security]]
  2. === Scripting and security
  3. While Elasticsearch contributors make every effort to prevent scripts from
  4. running amok, security is something best done in
  5. https://en.wikipedia.org/wiki/Defense_in_depth_(computing)[layers] because
  6. all software has bugs and it is important to minimize the risk of failure in
  7. any security layer. Find below rules of thumb for how to keep Elasticsearch
  8. from being a vulnerability.
  9. [float]
  10. === Do not run as root
  11. First and foremost, never run Elasticsearch as the `root` user as this would
  12. allow any successful effort to circumvent the other security layers to do
  13. *anything* on your server. Elasticsearch will refuse to start if it detects
  14. that it is running as `root` but this is so important that it is worth double
  15. and triple checking.
  16. [float]
  17. === Do not expose Elasticsearch directly to users
  18. Do not expose Elasticsearch directly to users, instead have an application
  19. make requests on behalf of users. If this is not possible, have an application
  20. to sanitize requests from users. If *that* is not possible then have some
  21. mechanism to track which users did what. Understand that it is quite possible
  22. to write a <<search, `_search`>> that overwhelms Elasticsearch and brings down
  23. the cluster. All such searches should be considered bugs and the Elasticsearch
  24. contributors make an effort to prevent this but they are still possible.
  25. [float]
  26. === Do not expose Elasticsearch directly to the Internet
  27. Do not expose Elasticsearch to the Internet, instead have an application
  28. make requests on behalf of the Internet. Do not entertain the thought of having
  29. an application "sanitize" requests to Elasticsearch. Understand that it is
  30. possible for a sufficiently determined malicious user to write searches that
  31. overwhelm the Elasticsearch cluster and bring it down. For example:
  32. Good:
  33. * Users type text into a search box and the text is sent directly to a
  34. <<query-dsl-match-query>>, <<query-dsl-match-query-phrase>>,
  35. <<query-dsl-simple-query-string-query>>, or any of the <<search-suggesters>>.
  36. * Running a script with any of the above queries that was written as part of
  37. the application development process.
  38. * Running a script with `params` provided by users.
  39. * User actions makes documents with a fixed structure.
  40. Bad:
  41. * Users can write arbitrary scripts, queries, `_search` requests.
  42. * User actions make documents with structure defined by users.
  43. [float]
  44. [[modules-scripting-security-do-no-weaken]]
  45. === Do not weaken script security settings
  46. By default Elasticsearch will run inline, stored, and filesystem scripts for
  47. the builtin languages, namely the scripting language Painless, the template
  48. language Mustache, and the expression language Expressions. These *ought* to be
  49. safe to expose to trusted users and to your application servers because they
  50. have strong security sandboxes. The Elasticsearch committers do not support any
  51. non-sandboxed scripting languages and using any would be a poor choice because:
  52. 1. This drops a layer of security, leaving only Elasticsearch's builtin
  53. <<modules-scripting-other-layers, security layers>>.
  54. 2. Non-sandboxed scripts have unchecked access to Elasticsearch's internals and
  55. can cause all kinds of trouble if misused.
  56. [float]
  57. [[modules-scripting-other-layers]]
  58. === Other security layers
  59. In addition to user privileges and script sandboxing Elasticsearch uses the
  60. http://www.oracle.com/technetwork/java/seccodeguide-139067.html[Java Security Manager]
  61. and native security tools as additional layers of security.
  62. As part of its startup sequence Elasticsearch enables the Java Security Manager
  63. which limits the actions that can be taken by portions of the code. Painless
  64. uses this to limit the actions that generated Painless scripts can take,
  65. preventing them from being able to do things like write files and listen to
  66. sockets.
  67. Elasticsearch uses
  68. https://en.wikipedia.org/wiki/Seccomp[seccomp] in Linux,
  69. https://www.chromium.org/developers/design-documents/sandbox/osx-sandboxing-design[Seatbelt]
  70. in macOS, and
  71. https://msdn.microsoft.com/en-us/library/windows/desktop/ms684147[ActiveProcessLimit]
  72. on Windows to prevent Elasticsearch from forking or executing other processes.
  73. Below this we describe the security settings for scripts and how you can
  74. change from the defaults described above. You should be very, very careful
  75. when allowing more than the defaults. Any extra permissions weakens the total
  76. security of the Elasticsearch deployment.
  77. [[allowed-script-types-setting]]
  78. [float]
  79. === Allowed script types setting
  80. By default all script types are allowed to be executed. This can be modified using the
  81. setting `script.allowed_types`. Only the types specified as part of the setting will be
  82. allowed to be executed.
  83. [source,yaml]
  84. ----
  85. script.allowed_types: inline <1>
  86. ----
  87. <1> This will allow only inline scripts to be executed but not stored scripts
  88. (or any other types).
  89. [[allowed-script-contexts-setting]]
  90. [float]
  91. === Allowed script contexts setting
  92. By default all script contexts are allowed to be executed. This can be modified using the
  93. setting `script.allowed_contexts`. Only the contexts specified as part of the setting will
  94. be allowed to be executed.
  95. [source,yaml]
  96. ----
  97. script.allowed_contexts: search, update <1>
  98. ----
  99. <1> This will allow only search and update scripts to be executed but not
  100. aggs or plugin scripts (or any other contexts).
  101. [[deprecated-script=settings]]
  102. [float]
  103. === Deprecated script settings
  104. The following settings have all been deprecated and will be removed in 6.0:
  105. * <<security-script-source>>
  106. * <<security-script-context>>
  107. * <<security-script-fine>>
  108. Use the following instead:
  109. * <<allowed-script-types-setting>>
  110. * <<allowed-script-contexts-setting>>
  111. [[security-script-source]]
  112. [float]
  113. === Script source settings
  114. Which scripts Elasticsearch will execute where is controlled by settings
  115. starting with `scripts.`. The simplest settings allow scripts to be enabled
  116. or disabled based on where they are stored. For example:
  117. [source,yaml]
  118. -----------------------------------
  119. script.inline: false <1>
  120. script.stored: false <2>
  121. script.file: true <3>
  122. -----------------------------------
  123. <1> Refuse to run scripts provided inline in the API.
  124. <2> Refuse to run scripts stored using the API.
  125. <3> Run scripts found on the filesystem in `/etc/elasticsearch/scripts`
  126. (rpm or deb) or `config/scripts` (zip or tar).
  127. NOTE: These settings override the defaults mentioned
  128. <<modules-scripting-security-do-no-weaken, above>>. Recreating the defaults
  129. requires more fine grained settings described <<security-script-fine, below>>.
  130. [[security-script-context]]
  131. [float]
  132. === Script context settings
  133. Scripting may also be enabled or disabled in different contexts in the
  134. Elasticsearch API. The supported contexts are:
  135. [horizontal]
  136. `aggs`:: Aggregations
  137. `search`:: Search api, Percolator API and Suggester API
  138. `update`:: Update api
  139. `plugin`:: Any plugin that makes use of scripts under the generic `plugin` category
  140. Plugins can also define custom operations that they use scripts for instead
  141. of using the generic `plugin` category. Those operations can be referred to
  142. in the following form: `${pluginName}_${operation}`.
  143. The following example disables scripting for `update` and `plugin` operations,
  144. regardless of the script source or language. Scripts can still be executed
  145. as part of `aggregations`, `search` and plugins execution though, as the above
  146. defaults still get applied.
  147. [source,yaml]
  148. -----------------------------------
  149. script.update: false
  150. script.plugin: false
  151. -----------------------------------
  152. [[security-script-fine]]
  153. [float]
  154. === Fine-grained script settings
  155. First, the high-level script settings described above are applied in order
  156. (context settings have precedence over source settings). Then fine-grained
  157. settings which include the script language take precedence over any high-level
  158. settings. They have two forms:
  159. [source,yaml]
  160. ------------------------
  161. script.engine.{lang}.{inline|file|stored}.{context}: true|false
  162. ------------------------
  163. And
  164. [source,yaml]
  165. ------------------------
  166. script.engine.{lang}.{inline|file|stored}: true|false
  167. ------------------------
  168. For example:
  169. [source,yaml]
  170. -----------------------------------
  171. script.inline: false <1>
  172. script.stored: false <1>
  173. script.file: false <1>
  174. script.engine.painless.inline: true <2>
  175. script.engine.painless.stored.search: true <3>
  176. script.engine.painless.stored.aggs: true <3>
  177. script.engine.mustache.stored.search: true <4>
  178. -----------------------------------
  179. <1> Disable all scripting from any source.
  180. <2> Allow inline Painless scripts for all operations.
  181. <3> Allow stored Painless scripts to be used for search and aggregations.
  182. <4> Allow stored Mustache templates to be used for search.