--- title: "脚本允许上下文配置" date: 2026-03-16 lastmod: 2026-03-16 description: "控制允许执行脚本的上下文配置说明" tags: ["脚本", "访问控制", "安全配置"] summary: "配置项作用 # script.allowed_contexts 配置项控制允许执行脚本的上下文环境。脚本上下文定义了脚本可以运行的位置(如聚合、搜索、更新等)。 是否可选 # 是 默认值 # [] (空列表,允许所有上下文) 配置项类型 # 动态配置 - 可以在运行时修改 配置格式 # # 允许所有上下文(默认) script.allowed_contexts: [] # 只允许特定上下文 script.allowed_contexts: - aggs - search # 允许更多上下文 script.allowed_contexts: - aggs - search - update - score # 禁用所有脚本 script.allowed_contexts: - none 可用上下文 # 上下文 说明 使用场景 aggs 聚合脚本 聚合计算 search 搜索脚本 查询评分 update 更新脚本 文档更新 score 评分脚本 自定义评分 filter 过滤脚本 结果过滤 ingest 摄取脚本 数据处理 推荐设置 # 环境 推荐值 说明 开发环境 [] 允许所有上下文 生产环境 只允许需要的上下文 最小权限原则 只读集群 ["aggs", "search", "score"] 禁用写入操作 安全优先 只允许必要上下文 减少攻击面 使用示例 # 默认配置:" --- ## 配置项作用 `script.allowed_contexts` 配置项控制允许执行脚本的上下文环境。脚本上下文定义了脚本可以运行的位置(如聚合、搜索、更新等)。 ## 是否可选 是 ## 默认值 ``` [] (空列表,允许所有上下文) ``` ## 配置项类型 **动态配置** - 可以在运行时修改 ## 配置格式 ```yaml # 允许所有上下文(默认) script.allowed_contexts: [] # 只允许特定上下文 script.allowed_contexts: - aggs - search # 允许更多上下文 script.allowed_contexts: - aggs - search - update - score # 禁用所有脚本 script.allowed_contexts: - none ``` ## 可用上下文 | 上下文 | 说明 | 使用场景 | |--------|------|---------| | `aggs` | 聚合脚本 | 聚合计算 | | `search` | 搜索脚本 | 查询评分 | | `update` | 更新脚本 | 文档更新 | | `score` | 评分脚本 | 自定义评分 | | `filter` | 过滤脚本 | 结果过滤 | | `ingest` | 摄取脚本 | 数据处理 | ## 推荐设置 | 环境 | 推荐值 | 说明 | |------|--------|------| | 开发环境 | `[]` | 允许所有上下文 | | 生产环境 | 只允许需要的上下文 | 最小权限原则 | | 只读集群 | `["aggs", "search", "score"]` | 禁用写入操作 | | 安全优先 | 只允许必要上下文 | 减少攻击面 | ## 使用示例 **默认配置:** ```yaml script.allowed_contexts: [] ``` **生产环境配置(推荐):** ```yaml # 只允许只读操作 script.allowed_contexts: - aggs - search - score ``` **完整功能环境:** ```yaml script.allowed_contexts: - aggs - search - update - score - filter ``` ## 最小权限原则 根据实际需求限制脚本上下文: ```yaml # 只需要聚合的脚本 script.allowed_contexts: - aggs # 只需要搜索评分 script.allowed_contexts: - search - score ``` ## 配置验证 ```bash # 查看当前配置 GET /_cluster/settings?filter_path=*.script.allowed_contexts # 测试不同上下文的脚本 # 聚合上下文 GET /_search { "aggs": { "test": { "script": { "source": "doc.field.value * 2" } } } } ``` ## 常见问题 **问题 1:上下文不被允许** **错误信息:** ``` illegal_argument_exception: can't execute scripts in context [update] ``` **解决方案:** ```yaml # 添加允许的上下文 script.allowed_contexts: - update ``` **问题 2:安全顾虑** **解决方案:** ```yaml # 只允许只读上下文 script.allowed_contexts: - aggs - search - score ``` ## 注意事项 1. **动态配置**:可以在运行时修改 2. **空列表**:表示允许所有上下文 3. **none 值**:禁用所有脚本执行 4. **最小权限**:生产环境应限制上下文 5. **功能需求**:根据实际功能需求设置 ## 相关配置项 | 配置项 | 默认值 | 说明 | |-------|-------|------| | `script.allowed_types` | [] | 允许的脚本类型 | | `script.disable_max_compilations_rate` | false | 是否禁用编译速率限制 | ## 完整配置示例 ```yaml # easysearch.yml # 生产环境配置 script.allowed_contexts: - aggs - search - score script.allowed_types: - stored ```