-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Obs AI Assistant] Disallow destructive actions via the Elasticsearch tool #229497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
viduni94
merged 6 commits into
elastic:main
from
viduni94:disallow-destructive-actions-elasticsearch-tool
Jul 28, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
368fb1e
Disallow destructive actions
viduni94 b7f3d00
Pass the path and determine search endpoint
viduni94 034b22f
Fix typo
viduni94 967a640
Keep accepted methods as the original array
viduni94 9e81a81
Merge branch 'main' into disallow-destructive-actions-elasticsearch-tool
viduni94 0f861f3
Merge branch 'main' into disallow-destructive-actions-elasticsearch-tool
viduni94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,11 +13,23 @@ export function registerElasticsearchFunction({ | |
| functions, | ||
| resources, | ||
| }: FunctionRegistrationParameters) { | ||
| functions.registerInstruction(({ availableFunctionNames }) => { | ||
| if (availableFunctionNames.includes(ELASTICSEARCH_FUNCTION_NAME)) { | ||
| return `You can use the ${ELASTICSEARCH_FUNCTION_NAME} tool to call Elasticsearch APIs on behalf of the user. | ||
| You are only allowed to perform GET requests (Some examples for GET requests are: Retrieving cluster information, cluster license, cluster health, indices stats, index stats, etc.) and GET/POST requests for the \`/_search\` endpoint (for search operations). | ||
| If the user asks to perform destructive actions or actions that are not allowed (e.g. PUT, PATCH, DELETE requests or POST requests that are not to the \`/_search\` endpoint), **NEVER** attempt to call the ${ELASTICSEARCH_FUNCTION_NAME} tool. | ||
| Instead, inform the user that you do not have the capability to perform those actions. | ||
| If you attempt to call the ${ELASTICSEARCH_FUNCTION_NAME} tool with disallowed methods (PUT, DELETE, PATCH, POST requests that are not to the \`/_search\` endpoint), it will fail. | ||
| For POST \`/_search\` operations, if a request body is needed, make sure the request body is a valid object.`; | ||
| } | ||
| return ''; | ||
| }); | ||
|
|
||
| functions.registerFunction( | ||
| { | ||
| name: ELASTICSEARCH_FUNCTION_NAME, | ||
| description: | ||
| 'Call Elasticsearch APIs on behalf of the user. Make sure the request body is valid for the API that you are using. Only call this function when the user has explicitly requested it.', | ||
| 'Call Elasticsearch APIs on behalf of the user. Make sure the request body is valid for the API that you are using. Only call this function when the user has explicitly requested it. Only GET requests and requests for /_search (GET and POST) are allowed', | ||
| descriptionForUser: 'Call Elasticsearch APIs on behalf of the user', | ||
| parameters: { | ||
| type: 'object', | ||
|
|
@@ -40,6 +52,18 @@ export function registerElasticsearchFunction({ | |
| }, | ||
| }, | ||
| async ({ arguments: { method, path, body } }) => { | ||
| // Allowlist: (1) all GET requests, (2) POST requests whose *final* path segment is exactly "_search". | ||
| const [pathWithoutQuery] = path.split('?'); | ||
| const pathSegments = pathWithoutQuery.replace(/^\//, '').split('/'); | ||
| const lastPathSegment = pathSegments[pathSegments.length - 1]; | ||
| const isSearchEndpoint = lastPathSegment === '_search'; | ||
|
|
||
| if (method !== 'GET' && !(method === 'POST' && isSearchEndpoint)) { | ||
| throw new Error( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the How it works:
The reason to leave those methods in the allowed list is because:
Example:
|
||
| 'Only GET requests or POST requests to the "_search" endpoint are permitted through this assistant function.' | ||
| ); | ||
| } | ||
|
|
||
| const esClient = (await resources.context.core).elasticsearch.client; | ||
| const response = await esClient.asCurrentUser.transport.request({ | ||
| method, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you extract this to a well-named helper (eg
isOperationAllowed) to contain this logic?