From 7273220cff26e6718fca56d4290eea2d2d12767a Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Fri, 20 Nov 2026 22:41:25 -0600 Subject: [PATCH 01/20] Add remove-selected-ai-extension template --- .../remove-selected-ai-extension.yaml | 210 ++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml new file mode 100644 index 00000000..50196859 --- /dev/null +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -0,0 +1,210 @@ +template-metadata: + slug: remove-selected-ai-extension + version: "1.0.0" + availability: ">=9.5.0" + name: "Remove Selected AI Extension" + description: >- + Audit and remove one supported user-installed AI editor extension by + exact ID and profile via Defend execute. Refuses built-in, system, and + policy-managed extensions. Audit by default. + solutions: [security] + categories: [response] + install: + form: + - name: enable-enforce + label: "Enforce extension uninstall" + description: >- + When true, run the uninstall adapter for the verified extension id. + inputType: boolean + required: true + default: false + +name: Remove Selected AI Extension +description: >- + Audit and remove one supported user-installed VS Code-family extension by + exact publisher.extension ID and profile. Refuses built-in/system/policy + managed extensions. Audit by default; enforce uses Defend execute. +enabled: true +tags: + - security + - elastic-defend + - hunting + - response + - genai +settings: + timeout: 15m + concurrency: + key: "{{ workflow.id }}-ext-{{ inputs.extension_id | default: '' }}-{{ inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' }}" + strategy: drop + max: 1 +consts: + enable_enforce: __install__.enable-enforce + supported_editors: vscode,cursor +triggers: + - type: alert + - type: manual + inputs: + - name: agent_id + type: string + required: true + - name: os_type + type: string + required: true + - name: editor + type: string + required: true + description: vscode | cursor + - name: extension_id + type: string + required: true + description: publisher.extension exact id + - name: profile + type: string + required: false + default: default + - name: install_source + type: string + required: false + default: user + description: Must be user - built-in/system/policy refused +steps: + # --- multi-alert guard --- + - name: reject_multi_alert + type: if + condition: 'event.alerts.size > 1' + steps: + - name: log_reject_batch + type: console + with: + message: "rejected - received {{ event.alerts.size }} alerts; this workflow handles one extension removal at a time. outcome=rejected" + else: + - name: set_context + type: data.set + with: + agent_id: "{% if event.alerts[0].agent.id != '' and event.alerts[0].agent.id != null %}{{ event.alerts[0].agent.id }}{% elsif event.alerts[0].elastic.agent.id != '' and event.alerts[0].elastic.agent.id != null %}{{ event.alerts[0].elastic.agent.id }}{% else %}{{ inputs.agent_id }}{% endif %}" + os_type: "{% if event.alerts[0].host.os.type != '' and event.alerts[0].host.os.type != null %}{{ event.alerts[0].host.os.type | downcase }}{% elsif inputs.os_type != '' and inputs.os_type != null %}{{ inputs.os_type | downcase }}{% else %}{% endif %}" + editor: "{% if inputs.editor != '' and inputs.editor != null %}{{ inputs.editor | downcase }}{% else %}{% endif %}" + extension_id: "{% if inputs.extension_id != '' and inputs.extension_id != null %}{{ inputs.extension_id }}{% else %}{% endif %}" + profile: "{% if inputs.profile != '' and inputs.profile != null %}{{ inputs.profile }}{% else %}default{% endif %}" + install_source: "{% if inputs.install_source != '' and inputs.install_source != null %}{{ inputs.install_source | downcase }}{% else %}user{% endif %}" + run_mode: "{% if event.alerts[0]._id != '' and event.alerts[0]._id != null %}alert{% else %}manual{% endif %}" + # --- extension_id charset allowlist (Liquid; KQL has no regex) --- + - name: check_extension_id + type: data.set + with: + extension_id_ok: >- + {%- assign s = steps.set_context.output.extension_id | default: '' -%} + {%- assign allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-' -%} + {%- assign alnum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' -%} + {%- assign ok = false -%} + {%- assign len = s.size -%} + {%- if len > 2 and len <= 256 and s contains '.' -%} + {%- assign c0 = s | slice: 0, 1 -%} + {%- if alnum contains c0 -%} + {%- assign bad = false -%} + {%- for i in (0..255) -%} + {%- if i >= len -%}{%- break -%}{%- endif -%} + {%- assign c = s | slice: i, 1 -%} + {%- unless allowed contains c -%}{%- assign bad = true -%}{%- break -%}{%- endunless -%} + {%- endfor -%} + {%- unless bad -%} + {%- assign parts = s | split: '.' -%} + {%- if parts.size >= 2 and parts[0].size > 0 and parts[1].size > 0 -%} + {%- assign ok = true -%} + {%- endif -%} + {%- endunless -%} + {%- endif -%} + {%- endif -%} + {{ ok }} + - name: validate_extension_id + type: if + condition: 'not steps.check_extension_id.output.extension_id_ok: "true"' + steps: + - name: log_bad_ext + type: console + with: + message: "rejected - extension_id failed charset allowlist (expect publisher.extension). outcome=rejected" + else: + - name: validate + type: if + condition: 'steps.set_context.output.agent_id: "" OR steps.set_context.output.extension_id: "" OR steps.set_context.output.os_type: "" OR not steps.set_context.output.install_source: "user" OR (not steps.set_context.output.editor: "vscode" AND not steps.set_context.output.editor: "cursor")' + steps: + - name: log_skip + type: console + with: + message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user. Built-in/system/policy refused. outcome=skipped" + else: + - name: decide_enforce + type: data.set + with: + enforce: "{% if consts.enable_enforce %}true{% else %}false{% endif %}" + - name: audit_path + type: if + condition: 'steps.decide_enforce.output.enforce: "false"' + steps: + - name: log_audit + type: console + with: + message: "audited - would remove editor={{ steps.set_context.output.editor }} extension={{ steps.set_context.output.extension_id }} profile={{ steps.set_context.output.profile }} agent={{ steps.set_context.output.agent_id }} outcome=audited" + else: + - name: set_cmd + type: data.set + with: + command: |- + {%- assign ed = steps.set_context.output.editor -%} + {%- assign eid = steps.set_context.output.extension_id -%} + {%- if steps.set_context.output.os_type == 'windows' -%} + {%- if ed == 'cursor' -%} + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; & $bin --uninstall-extension $eid --force; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + {%- else -%} + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; & $bin --uninstall-extension $eid --force; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + {%- endif -%} + {%- else -%} + eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; "$bin" --uninstall-extension "$eid" --force; printf 'removed:%s\n' "$eid" + {%- endif -%} + - name: execute_remove + type: kibana.request + with: + method: POST + path: /api/endpoint/action/execute + headers: + kbn-xsrf: "true" + body: + agent_type: endpoint + endpoint_ids: + - "{{ steps.set_context.output.agent_id }}" + comment: "T05 remove-selected-ai-extension {{ steps.set_context.output.extension_id }}" + parameters: + command: "{{ steps.set_cmd.output.command }}" + timeout: 120 + - name: remember + type: data.set + with: + action_id: "{{ steps.execute_remove.output.data.id | default: steps.execute_remove.output.id | default: '' }}" + completed: false + - name: poll + type: while + condition: 'variables.action_id: * and not variables.action_id: "" and not variables.completed: true' + max-iterations: + limit: 36 + on-limit: continue + steps: + - name: wait_poll + type: wait + with: + duration: 5s + - name: fetch + type: kibana.request + on-failure: + continue: true + with: + method: GET + path: /api/endpoint/action/{{ variables.action_id | url_encode }} + - name: mark + type: data.set + with: + completed: "{%- assign d = steps.fetch.output.data -%}{%- if variables.action_id != '' and d.id == variables.action_id and d.isCompleted -%}true{%- else -%}false{%- endif -%}" + - name: summarize + type: console + with: + message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} outcome={% if variables.completed == 'true' and steps.fetch.output.data.wasSuccessful and steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code == 0 %}verified{% elsif variables.completed == 'true' %}failed{% else %}unknown{% endif %}" From aa8b8f159f6bd992f071b95ce7bcaf63df494b4e Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Fri, 20 Nov 2026 23:20:30 -0600 Subject: [PATCH 02/20] fix: alert-sourced fields, profile flag, multi-alert, exit status --- .../remove-selected-ai-extension.yaml | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 50196859..97f13a17 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -23,7 +23,7 @@ name: Remove Selected AI Extension description: >- Audit and remove one supported user-installed VS Code-family extension by exact publisher.extension ID and profile. Refuses built-in/system/policy - managed extensions. Audit by default; enforce uses Defend execute. + managed extensions (caller must attest install_source=user). Audit by default; enforce uses Defend execute as SYSTEM/root — pass profile/user home so the editor CLI targets the right extensions directory. enabled: true tags: - security @@ -66,28 +66,28 @@ triggers: type: string required: false default: user - description: Must be user - built-in/system/policy refused + description: Caller attestation — must be user; built-in/system/policy must be refused by the triggering rule or analyst before invoke steps: - # --- multi-alert guard --- + - name: set_context + type: data.set + with: + agent_id: "{% if event.alerts[0].agent.id != '' and event.alerts[0].agent.id != null %}{{ event.alerts[0].agent.id }}{% elsif event.alerts[0].elastic.agent.id != '' and event.alerts[0].elastic.agent.id != null %}{{ event.alerts[0].elastic.agent.id }}{% else %}{{ inputs.agent_id }}{% endif %}" + os_type: "{% if event.alerts[0].host.os.type != '' and event.alerts[0].host.os.type != null %}{{ event.alerts[0].host.os.type | downcase }}{% elsif inputs.os_type != '' and inputs.os_type != null %}{{ inputs.os_type | downcase }}{% else %}{% endif %}" + editor: "{% if inputs.editor != '' and inputs.editor != null %}{{ inputs.editor | downcase }}{% elsif event.alerts[0].gen_ai.editor != '' and event.alerts[0].gen_ai.editor != null %}{{ event.alerts[0].gen_ai.editor | downcase }}{% elsif event.alerts[0].gen_ai.tool.name != '' and event.alerts[0].gen_ai.tool.name != null %}{{ event.alerts[0].gen_ai.tool.name | downcase }}{% else %}{% endif %}" + extension_id: "{% if inputs.extension_id != '' and inputs.extension_id != null %}{{ inputs.extension_id }}{% elsif event.alerts[0].gen_ai.extension.id != '' and event.alerts[0].gen_ai.extension.id != null %}{{ event.alerts[0].gen_ai.extension.id }}{% elsif event.alerts[0].package.name != '' and event.alerts[0].package.name != null %}{{ event.alerts[0].package.name }}{% else %}{% endif %}" + profile: "{% if inputs.profile != '' and inputs.profile != null %}{{ inputs.profile }}{% elsif event.alerts[0].gen_ai.editor.profile != '' and event.alerts[0].gen_ai.editor.profile != null %}{{ event.alerts[0].gen_ai.editor.profile }}{% else %}default{% endif %}" + install_source: "{% if inputs.install_source != '' and inputs.install_source != null %}{{ inputs.install_source | downcase }}{% elsif event.alerts[0].gen_ai.extension.install_source != '' and event.alerts[0].gen_ai.extension.install_source != null %}{{ event.alerts[0].gen_ai.extension.install_source | downcase }}{% else %}user{% endif %}" + run_mode: "{% if event.alerts[0]._id != '' and event.alerts[0]._id != null %}alert{% else %}manual{% endif %}" + alert_count: "{{ event.alerts | size | default: 0 }}" - name: reject_multi_alert type: if - condition: 'event.alerts.size > 1' + condition: 'steps.set_context.output.run_mode: "alert" AND not steps.set_context.output.alert_count: "0" AND not steps.set_context.output.alert_count: "1"' steps: - name: log_reject_batch type: console with: - message: "rejected - received {{ event.alerts.size }} alerts; this workflow handles one extension removal at a time. outcome=rejected" + message: "rejected - received {{ steps.set_context.output.alert_count }} alerts; this workflow handles one extension removal at a time. outcome=rejected" else: - - name: set_context - type: data.set - with: - agent_id: "{% if event.alerts[0].agent.id != '' and event.alerts[0].agent.id != null %}{{ event.alerts[0].agent.id }}{% elsif event.alerts[0].elastic.agent.id != '' and event.alerts[0].elastic.agent.id != null %}{{ event.alerts[0].elastic.agent.id }}{% else %}{{ inputs.agent_id }}{% endif %}" - os_type: "{% if event.alerts[0].host.os.type != '' and event.alerts[0].host.os.type != null %}{{ event.alerts[0].host.os.type | downcase }}{% elsif inputs.os_type != '' and inputs.os_type != null %}{{ inputs.os_type | downcase }}{% else %}{% endif %}" - editor: "{% if inputs.editor != '' and inputs.editor != null %}{{ inputs.editor | downcase }}{% else %}{% endif %}" - extension_id: "{% if inputs.extension_id != '' and inputs.extension_id != null %}{{ inputs.extension_id }}{% else %}{% endif %}" - profile: "{% if inputs.profile != '' and inputs.profile != null %}{{ inputs.profile }}{% else %}default{% endif %}" - install_source: "{% if inputs.install_source != '' and inputs.install_source != null %}{{ inputs.install_source | downcase }}{% else %}user{% endif %}" - run_mode: "{% if event.alerts[0]._id != '' and event.alerts[0]._id != null %}alert{% else %}manual{% endif %}" # --- extension_id charset allowlist (Liquid; KQL has no regex) --- - name: check_extension_id type: data.set @@ -132,7 +132,7 @@ steps: - name: log_skip type: console with: - message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user. Built-in/system/policy refused. outcome=skipped" + message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user (caller attestation; Defend runs as SYSTEM/root and cannot verify built-in/policy). outcome=skipped" else: - name: decide_enforce type: data.set @@ -155,12 +155,12 @@ steps: {%- assign eid = steps.set_context.output.extension_id -%} {%- if steps.set_context.output.os_type == 'windows' -%} {%- if ed == 'cursor' -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; & $bin --uninstall-extension $eid --force; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- else -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; & $bin --uninstall-extension $eid --force; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- endif -%} {%- else -%} - eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; "$bin" --uninstall-extension "$eid" --force; printf 'removed:%s\n' "$eid" + eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; prof='{{ steps.set_context.output.profile | replace: "'", "'\\''" }}'; if [ -n "$prof" ] && [ "$prof" != "default" ]; then "$bin" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" {%- endif -%} - name: execute_remove type: kibana.request From d8998eef8bb775a8381428015a2c6100a8990aeb Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Fri, 20 Nov 2026 23:29:39 -0600 Subject: [PATCH 03/20] fix: profile allowlist, alert concurrency key, safer editor fields --- .../remove-selected-ai-extension.yaml | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 97f13a17..8bfad02e 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -34,7 +34,7 @@ tags: settings: timeout: 15m concurrency: - key: "{{ workflow.id }}-ext-{{ inputs.extension_id | default: '' }}-{{ inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' }}" + key: "{{ workflow.id }}-ext-{{ inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' }}-{{ inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' }}" strategy: drop max: 1 consts: @@ -73,8 +73,8 @@ steps: with: agent_id: "{% if event.alerts[0].agent.id != '' and event.alerts[0].agent.id != null %}{{ event.alerts[0].agent.id }}{% elsif event.alerts[0].elastic.agent.id != '' and event.alerts[0].elastic.agent.id != null %}{{ event.alerts[0].elastic.agent.id }}{% else %}{{ inputs.agent_id }}{% endif %}" os_type: "{% if event.alerts[0].host.os.type != '' and event.alerts[0].host.os.type != null %}{{ event.alerts[0].host.os.type | downcase }}{% elsif inputs.os_type != '' and inputs.os_type != null %}{{ inputs.os_type | downcase }}{% else %}{% endif %}" - editor: "{% if inputs.editor != '' and inputs.editor != null %}{{ inputs.editor | downcase }}{% elsif event.alerts[0].gen_ai.editor != '' and event.alerts[0].gen_ai.editor != null %}{{ event.alerts[0].gen_ai.editor | downcase }}{% elsif event.alerts[0].gen_ai.tool.name != '' and event.alerts[0].gen_ai.tool.name != null %}{{ event.alerts[0].gen_ai.tool.name | downcase }}{% else %}{% endif %}" - extension_id: "{% if inputs.extension_id != '' and inputs.extension_id != null %}{{ inputs.extension_id }}{% elsif event.alerts[0].gen_ai.extension.id != '' and event.alerts[0].gen_ai.extension.id != null %}{{ event.alerts[0].gen_ai.extension.id }}{% elsif event.alerts[0].package.name != '' and event.alerts[0].package.name != null %}{{ event.alerts[0].package.name }}{% else %}{% endif %}" + editor: "{% if inputs.editor != '' and inputs.editor != null %}{{ inputs.editor | downcase }}{% elsif event.alerts[0].gen_ai.editor.name != '' and event.alerts[0].gen_ai.editor.name != null %}{{ event.alerts[0].gen_ai.editor.name | downcase }}{% elsif event.alerts[0].gen_ai.tool.name != '' and event.alerts[0].gen_ai.tool.name != null %}{{ event.alerts[0].gen_ai.tool.name | downcase }}{% else %}{% endif %}" + extension_id: "{% if inputs.extension_id != '' and inputs.extension_id != null %}{{ inputs.extension_id }}{% elsif event.alerts[0].gen_ai.extension.id != '' and event.alerts[0].gen_ai.extension.id != null %}{{ event.alerts[0].gen_ai.extension.id }}{% else %}{% endif %}" profile: "{% if inputs.profile != '' and inputs.profile != null %}{{ inputs.profile }}{% elsif event.alerts[0].gen_ai.editor.profile != '' and event.alerts[0].gen_ai.editor.profile != null %}{{ event.alerts[0].gen_ai.editor.profile }}{% else %}default{% endif %}" install_source: "{% if inputs.install_source != '' and inputs.install_source != null %}{{ inputs.install_source | downcase }}{% elsif event.alerts[0].gen_ai.extension.install_source != '' and event.alerts[0].gen_ai.extension.install_source != null %}{{ event.alerts[0].gen_ai.extension.install_source | downcase }}{% else %}user{% endif %}" run_mode: "{% if event.alerts[0]._id != '' and event.alerts[0]._id != null %}alert{% else %}manual{% endif %}" @@ -116,6 +116,23 @@ steps: {%- endif -%} {%- endif -%} {{ ok }} + - name: check_profile + type: data.set + with: + profile_ok: >- + {%- assign s = steps.set_context.output.profile | default: 'default' -%} + {%- assign allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-' -%} + {%- assign ok = true -%} + {%- assign len = s.size -%} + {%- if len < 1 or len > 128 -%}{%- assign ok = false -%} + {%- else -%} + {%- for i in (0..127) -%} + {%- if i >= len -%}{%- break -%}{%- endif -%} + {%- assign c = s | slice: i, 1 -%} + {%- unless allowed contains c -%}{%- assign ok = false -%}{%- break -%}{%- endunless -%} + {%- endfor -%} + {%- endif -%} + {{ ok }} - name: validate_extension_id type: if condition: 'not steps.check_extension_id.output.extension_id_ok: "true"' @@ -127,12 +144,12 @@ steps: else: - name: validate type: if - condition: 'steps.set_context.output.agent_id: "" OR steps.set_context.output.extension_id: "" OR steps.set_context.output.os_type: "" OR not steps.set_context.output.install_source: "user" OR (not steps.set_context.output.editor: "vscode" AND not steps.set_context.output.editor: "cursor")' + condition: 'not steps.check_profile.output.profile_ok: "true" OR steps.set_context.output.agent_id: "" OR steps.set_context.output.extension_id: "" OR steps.set_context.output.os_type: "" OR not steps.set_context.output.install_source: "user" OR (not steps.set_context.output.editor: "vscode" AND not steps.set_context.output.editor: "cursor")' steps: - name: log_skip type: console with: - message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user (caller attestation; Defend runs as SYSTEM/root and cannot verify built-in/policy). outcome=skipped" + message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user, and a safe profile name (caller attestation; Defend runs as SYSTEM/root and cannot verify built-in/policy). outcome=skipped" else: - name: decide_enforce type: data.set @@ -155,12 +172,12 @@ steps: {%- assign eid = steps.set_context.output.extension_id -%} {%- if steps.set_context.output.os_type == 'windows' -%} {%- if ed == 'cursor' -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- else -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- endif -%} {%- else -%} - eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; prof='{{ steps.set_context.output.profile | replace: "'", "'\\''" }}'; if [ -n "$prof" ] && [ "$prof" != "default" ]; then "$bin" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; prof='{{ steps.set_context.output.profile | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; if [ -n "$prof" ] && [ "$prof" != "default" ]; then "$bin" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" {%- endif -%} - name: execute_remove type: kibana.request From 85bf09ff1d42dd9125ce86e4250acf91807c0702 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Fri, 20 Nov 2026 23:41:14 -0600 Subject: [PATCH 04/20] fix: concurrency key editor/profile and spaced profiles --- .../remove-selected-ai-extension.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 8bfad02e..0f924339 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -34,7 +34,7 @@ tags: settings: timeout: 15m concurrency: - key: "{{ workflow.id }}-ext-{{ inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' }}-{{ inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' }}" + key: "{{ workflow.id }}-ext-{{ inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' | downcase }}-{{ inputs.editor | default: event.alerts[0].gen_ai.editor.name | default: event.alerts[0].gen_ai.tool.name | default: '' | downcase }}-{{ inputs.profile | default: event.alerts[0].gen_ai.editor.profile | default: 'default' | downcase }}-{{ inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' }}" strategy: drop max: 1 consts: @@ -121,7 +121,7 @@ steps: with: profile_ok: >- {%- assign s = steps.set_context.output.profile | default: 'default' -%} - {%- assign allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-' -%} + {%- assign allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.- ' -%} {%- assign ok = true -%} {%- assign len = s.size -%} {%- if len < 1 or len > 128 -%}{%- assign ok = false -%} @@ -172,12 +172,12 @@ steps: {%- assign eid = steps.set_context.output.extension_id -%} {%- if steps.set_context.output.os_type == 'windows' -%} {%- if ed == 'cursor' -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- else -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- endif -%} {%- else -%} - eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; prof='{{ steps.set_context.output.profile | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; if [ -n "$prof" ] && [ "$prof" != "default" ]; then "$bin" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; prof='{{ steps.set_context.output.profile | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; if [ -n "$prof" ] && [ "$prof" != "default" ]; then "$bin" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" {%- endif -%} - name: execute_remove type: kibana.request From 1f3f40c93b633f0c15a05a6407c3978f167fa3f0 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Fri, 20 Nov 2026 23:56:11 -0600 Subject: [PATCH 05/20] fix: encode concurrency key fields; keep profile case --- .../remove-selected-ai-extension.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 0f924339..fce71ae3 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -34,7 +34,7 @@ tags: settings: timeout: 15m concurrency: - key: "{{ workflow.id }}-ext-{{ inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' | downcase }}-{{ inputs.editor | default: event.alerts[0].gen_ai.editor.name | default: event.alerts[0].gen_ai.tool.name | default: '' | downcase }}-{{ inputs.profile | default: event.alerts[0].gen_ai.editor.profile | default: 'default' | downcase }}-{{ inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' }}" + key: "{{ workflow.id }}-ext-{{ inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' | downcase | base64_encode }}|{{ inputs.editor | default: event.alerts[0].gen_ai.editor.name | default: event.alerts[0].gen_ai.tool.name | default: '' | downcase | base64_encode }}|{{ inputs.profile | default: event.alerts[0].gen_ai.editor.profile | default: 'default' | base64_encode }}|{{ inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' | base64_encode }}" strategy: drop max: 1 consts: From e00f23bd4b6ec6293d3d9385f764f63acd4f0779 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Sat, 21 Nov 2026 00:03:40 -0600 Subject: [PATCH 06/20] fix: normalize default profile and bound concurrency key --- .../remove-selected-ai-extension.yaml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index fce71ae3..0b32db3b 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -34,7 +34,14 @@ tags: settings: timeout: 15m concurrency: - key: "{{ workflow.id }}-ext-{{ inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' | downcase | base64_encode }}|{{ inputs.editor | default: event.alerts[0].gen_ai.editor.name | default: event.alerts[0].gen_ai.tool.name | default: '' | downcase | base64_encode }}|{{ inputs.profile | default: event.alerts[0].gen_ai.editor.profile | default: 'default' | base64_encode }}|{{ inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' | base64_encode }}" + key: |- + {%- assign eid = inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' | strip | downcase | truncate: 128, '' -%} + {%- assign ed = inputs.editor | default: event.alerts[0].gen_ai.editor.name | default: event.alerts[0].gen_ai.tool.name | default: '' | strip | downcase | truncate: 32, '' -%} + {%- assign prof = inputs.profile | default: event.alerts[0].gen_ai.editor.profile | default: 'default' | strip | truncate: 128, '' -%} + {%- assign prof_lc = prof | downcase -%} + {%- if prof_lc == 'default' -%}{%- assign prof = 'default' -%}{%- endif -%} + {%- assign aid = inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' | strip | truncate: 128, '' -%} + {{ workflow.id }}-ext-{{ eid | base64_encode }}|{{ ed | base64_encode }}|{{ prof | base64_encode }}|{{ aid | base64_encode }} strategy: drop max: 1 consts: From c9e319953ee99062bc04f2c0c148fb96c61e642d Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Sat, 21 Nov 2026 00:07:03 -0600 Subject: [PATCH 07/20] fix: align concurrency key with validation and default profile --- .../remove-selected-ai-extension.yaml | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 0b32db3b..697db782 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -35,8 +35,8 @@ settings: timeout: 15m concurrency: key: |- - {%- assign eid = inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' | strip | downcase | truncate: 128, '' -%} - {%- assign ed = inputs.editor | default: event.alerts[0].gen_ai.editor.name | default: event.alerts[0].gen_ai.tool.name | default: '' | strip | downcase | truncate: 32, '' -%} + {%- assign eid = inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' | strip | downcase | truncate: 256, '' -%} + {%- assign ed = inputs.editor | default: event.alerts[0].gen_ai.editor.name | default: event.alerts[0].gen_ai.tool.name | default: '' | downcase | truncate: 32, '' -%} {%- assign prof = inputs.profile | default: event.alerts[0].gen_ai.editor.profile | default: 'default' | strip | truncate: 128, '' -%} {%- assign prof_lc = prof | downcase -%} {%- if prof_lc == 'default' -%}{%- assign prof = 'default' -%}{%- endif -%} @@ -82,7 +82,16 @@ steps: os_type: "{% if event.alerts[0].host.os.type != '' and event.alerts[0].host.os.type != null %}{{ event.alerts[0].host.os.type | downcase }}{% elsif inputs.os_type != '' and inputs.os_type != null %}{{ inputs.os_type | downcase }}{% else %}{% endif %}" editor: "{% if inputs.editor != '' and inputs.editor != null %}{{ inputs.editor | downcase }}{% elsif event.alerts[0].gen_ai.editor.name != '' and event.alerts[0].gen_ai.editor.name != null %}{{ event.alerts[0].gen_ai.editor.name | downcase }}{% elsif event.alerts[0].gen_ai.tool.name != '' and event.alerts[0].gen_ai.tool.name != null %}{{ event.alerts[0].gen_ai.tool.name | downcase }}{% else %}{% endif %}" extension_id: "{% if inputs.extension_id != '' and inputs.extension_id != null %}{{ inputs.extension_id }}{% elsif event.alerts[0].gen_ai.extension.id != '' and event.alerts[0].gen_ai.extension.id != null %}{{ event.alerts[0].gen_ai.extension.id }}{% else %}{% endif %}" - profile: "{% if inputs.profile != '' and inputs.profile != null %}{{ inputs.profile }}{% elsif event.alerts[0].gen_ai.editor.profile != '' and event.alerts[0].gen_ai.editor.profile != null %}{{ event.alerts[0].gen_ai.editor.profile }}{% else %}default{% endif %}" + profile: |- + {%- if inputs.profile != '' and inputs.profile != null -%} + {%- assign p = inputs.profile | strip -%} + {%- elsif event.alerts[0].gen_ai.editor.profile != '' and event.alerts[0].gen_ai.editor.profile != null -%} + {%- assign p = event.alerts[0].gen_ai.editor.profile | strip -%} + {%- else -%} + {%- assign p = 'default' -%} + {%- endif -%} + {%- assign pl = p | downcase -%} + {%- if pl == 'default' or pl == '' -%}default{%- else -%}{{ p }}{%- endif -%} install_source: "{% if inputs.install_source != '' and inputs.install_source != null %}{{ inputs.install_source | downcase }}{% elsif event.alerts[0].gen_ai.extension.install_source != '' and event.alerts[0].gen_ai.extension.install_source != null %}{{ event.alerts[0].gen_ai.extension.install_source | downcase }}{% else %}user{% endif %}" run_mode: "{% if event.alerts[0]._id != '' and event.alerts[0]._id != null %}alert{% else %}manual{% endif %}" alert_count: "{{ event.alerts | size | default: 0 }}" @@ -179,12 +188,12 @@ steps: {%- assign eid = steps.set_context.output.extension_id -%} {%- if steps.set_context.output.os_type == 'windows' -%} {%- if ed == 'cursor' -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- else -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- endif -%} {%- else -%} - eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; prof='{{ steps.set_context.output.profile | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; if [ -n "$prof" ] && [ "$prof" != "default" ]; then "$bin" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; prof='{{ steps.set_context.output.profile | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" {%- endif -%} - name: execute_remove type: kibana.request From c8120e16477f2854bff0f2d82e1bc35f8c55cc79 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Sat, 21 Nov 2026 00:15:16 -0600 Subject: [PATCH 08/20] fix: reject whitespace-only profiles in key and validation --- .../remove-selected-ai-extension.yaml | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 697db782..8f6a7f08 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -37,9 +37,24 @@ settings: key: |- {%- assign eid = inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' | strip | downcase | truncate: 256, '' -%} {%- assign ed = inputs.editor | default: event.alerts[0].gen_ai.editor.name | default: event.alerts[0].gen_ai.tool.name | default: '' | downcase | truncate: 32, '' -%} - {%- assign prof = inputs.profile | default: event.alerts[0].gen_ai.editor.profile | default: 'default' | strip | truncate: 128, '' -%} + {%- assign prof_raw = '' -%} + {%- assign prof_supplied = false -%} + {%- if inputs.profile != '' and inputs.profile != null -%} + {%- assign prof_raw = inputs.profile -%} + {%- assign prof_supplied = true -%} + {%- elsif event.alerts[0].gen_ai.editor.profile != '' and event.alerts[0].gen_ai.editor.profile != null -%} + {%- assign prof_raw = event.alerts[0].gen_ai.editor.profile -%} + {%- assign prof_supplied = true -%} + {%- endif -%} + {%- if prof_supplied -%} + {%- assign prof = prof_raw | strip | truncate: 128, '' -%} {%- assign prof_lc = prof | downcase -%} - {%- if prof_lc == 'default' -%}{%- assign prof = 'default' -%}{%- endif -%} + {%- if prof == '' -%}{%- assign prof = '__blank__' -%} + {%- elsif prof_lc == 'default' -%}{%- assign prof = 'default' -%} + {%- endif -%} + {%- else -%} + {%- assign prof = 'default' -%} + {%- endif -%} {%- assign aid = inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' | strip | truncate: 128, '' -%} {{ workflow.id }}-ext-{{ eid | base64_encode }}|{{ ed | base64_encode }}|{{ prof | base64_encode }}|{{ aid | base64_encode }} strategy: drop @@ -83,15 +98,25 @@ steps: editor: "{% if inputs.editor != '' and inputs.editor != null %}{{ inputs.editor | downcase }}{% elsif event.alerts[0].gen_ai.editor.name != '' and event.alerts[0].gen_ai.editor.name != null %}{{ event.alerts[0].gen_ai.editor.name | downcase }}{% elsif event.alerts[0].gen_ai.tool.name != '' and event.alerts[0].gen_ai.tool.name != null %}{{ event.alerts[0].gen_ai.tool.name | downcase }}{% else %}{% endif %}" extension_id: "{% if inputs.extension_id != '' and inputs.extension_id != null %}{{ inputs.extension_id }}{% elsif event.alerts[0].gen_ai.extension.id != '' and event.alerts[0].gen_ai.extension.id != null %}{{ event.alerts[0].gen_ai.extension.id }}{% else %}{% endif %}" profile: |- + {%- assign supplied = false -%} + {%- assign raw = '' -%} {%- if inputs.profile != '' and inputs.profile != null -%} - {%- assign p = inputs.profile | strip -%} + {%- assign raw = inputs.profile -%} + {%- assign supplied = true -%} {%- elsif event.alerts[0].gen_ai.editor.profile != '' and event.alerts[0].gen_ai.editor.profile != null -%} - {%- assign p = event.alerts[0].gen_ai.editor.profile | strip -%} - {%- else -%} - {%- assign p = 'default' -%} + {%- assign raw = event.alerts[0].gen_ai.editor.profile -%} + {%- assign supplied = true -%} {%- endif -%} + {%- if supplied -%} + {%- assign p = raw | strip -%} + {%- if p == '' -%} + {%- else -%} {%- assign pl = p | downcase -%} - {%- if pl == 'default' or pl == '' -%}default{%- else -%}{{ p }}{%- endif -%} + {%- if pl == 'default' -%}default{%- else -%}{{ p }}{%- endif -%} + {%- endif -%} + {%- else -%} + default + {%- endif -%} install_source: "{% if inputs.install_source != '' and inputs.install_source != null %}{{ inputs.install_source | downcase }}{% elsif event.alerts[0].gen_ai.extension.install_source != '' and event.alerts[0].gen_ai.extension.install_source != null %}{{ event.alerts[0].gen_ai.extension.install_source | downcase }}{% else %}user{% endif %}" run_mode: "{% if event.alerts[0]._id != '' and event.alerts[0]._id != null %}alert{% else %}manual{% endif %}" alert_count: "{{ event.alerts | size | default: 0 }}" @@ -136,11 +161,11 @@ steps: type: data.set with: profile_ok: >- - {%- assign s = steps.set_context.output.profile | default: 'default' -%} + {%- assign s = steps.set_context.output.profile | default: '' -%} {%- assign allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.- ' -%} {%- assign ok = true -%} {%- assign len = s.size -%} - {%- if len < 1 or len > 128 -%}{%- assign ok = false -%} + {%- if s == '' or s == '__blank__' or len < 1 or len > 128 -%}{%- assign ok = false -%} {%- else -%} {%- for i in (0..127) -%} {%- if i >= len -%}{%- break -%}{%- endif -%} @@ -165,7 +190,7 @@ steps: - name: log_skip type: console with: - message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user, and a safe profile name (caller attestation; Defend runs as SYSTEM/root and cannot verify built-in/policy). outcome=skipped" + message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user, and a safe non-blank profile name (omit or set default; whitespace-only rejected; Defend runs as SYSTEM/root and cannot verify built-in/policy). outcome=skipped" else: - name: decide_enforce type: data.set From b0dd7a7c907e23f05b50d71b6dd6fbae83ad0b01 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Sat, 21 Nov 2026 00:24:11 -0600 Subject: [PATCH 09/20] Reject empty profiles instead of mapping them to __blank__. --- .../remove-selected-ai-extension.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 8f6a7f08..70e4e1ac 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -49,7 +49,7 @@ settings: {%- if prof_supplied -%} {%- assign prof = prof_raw | strip | truncate: 128, '' -%} {%- assign prof_lc = prof | downcase -%} - {%- if prof == '' -%}{%- assign prof = '__blank__' -%} + {%- if prof == '' -%} {%- elsif prof_lc == 'default' -%}{%- assign prof = 'default' -%} {%- endif -%} {%- else -%} @@ -165,7 +165,7 @@ steps: {%- assign allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.- ' -%} {%- assign ok = true -%} {%- assign len = s.size -%} - {%- if s == '' or s == '__blank__' or len < 1 or len > 128 -%}{%- assign ok = false -%} + {%- if s == '' or len < 1 or len > 128 -%}{%- assign ok = false -%} {%- else -%} {%- for i in (0..127) -%} {%- if i >= len -%}{%- break -%}{%- endif -%} From b53a8d8f91c6199272adbd6d67a686a7354b245c Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Mon, 30 Nov 2026 17:28:31 -0600 Subject: [PATCH 10/20] fix: target user extensions dir and fail closed on unverified removal Require user home for --extensions-dir/--user-data-dir, absolute editor binaries, install_source on alerts, analyst approval before enforce, and workflow.fail when removal is not verified. --- .../remove-selected-ai-extension.yaml | 261 +++++++++++++----- node_modules | 1 + 2 files changed, 197 insertions(+), 65 deletions(-) create mode 120000 node_modules diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 70e4e1ac..73355772 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -5,8 +5,12 @@ template-metadata: name: "Remove Selected AI Extension" description: >- Audit and remove one supported user-installed AI editor extension by - exact ID and profile via Defend execute. Refuses built-in, system, and - policy-managed extensions. Audit by default. + exact ID and profile via Defend execute. Caller must confirm + install_source=user (alert runs require that field). Requires Elastic + Defend with the execute response action (Enterprise) and Execute + Operations privilege. Targets the developer's extensions directory via + user home (--extensions-dir / --user-data-dir). Audit by default; + enforce pauses for analyst approval. solutions: [security] categories: [response] install: @@ -14,7 +18,8 @@ template-metadata: - name: enable-enforce label: "Enforce extension uninstall" description: >- - When true, run the uninstall adapter for the verified extension id. + When true, run the uninstall adapter for the verified extension id + after analyst approval. inputType: boolean required: true default: false @@ -22,8 +27,14 @@ template-metadata: name: Remove Selected AI Extension description: >- Audit and remove one supported user-installed VS Code-family extension by - exact publisher.extension ID and profile. Refuses built-in/system/policy - managed extensions (caller must attest install_source=user). Audit by default; enforce uses Defend execute as SYSTEM/root — pass profile/user home so the editor CLI targets the right extensions directory. + exact publisher.extension ID and profile. Caller must confirm + install_source=user (alert runs require gen_ai.extension.install_source; + missing field skips). Requires Elastic Defend on the host, the execute + response action (Enterprise license), and Execute Operations privilege for + the workflow identity. Enforce uses Defend execute as SYSTEM/root and must + receive the target user home (input or alert user.home / user.name) so the + editor CLI runs with --extensions-dir and --user-data-dir against that + profile. Audit by default; enforce pauses for analyst approval. enabled: true tags: - security @@ -61,7 +72,6 @@ settings: max: 1 consts: enable_enforce: __install__.enable-enforce - supported_editors: vscode,cursor triggers: - type: alert - type: manual @@ -72,6 +82,7 @@ triggers: - name: os_type type: string required: true + description: linux | macos | windows - name: editor type: string required: true @@ -84,11 +95,27 @@ triggers: type: string required: false default: default + - name: user_home + type: string + required: false + default: "" + description: >- + Absolute home/profile root for the target user (required for enforce). + Prefer this over deriving from user_name. + - name: user_name + type: string + required: false + default: "" + description: >- + Account name used to derive home when user_home is empty + (/home/NAME, /Users/NAME, or C:\Users\NAME). - name: install_source type: string required: false default: user - description: Caller attestation — must be user; built-in/system/policy must be refused by the triggering rule or analyst before invoke + description: >- + Caller attestation — must be user. Alert runs require + gen_ai.extension.install_source on the alert; missing field skips. steps: - name: set_context type: data.set @@ -117,7 +144,40 @@ steps: {%- else -%} default {%- endif -%} - install_source: "{% if inputs.install_source != '' and inputs.install_source != null %}{{ inputs.install_source | downcase }}{% elsif event.alerts[0].gen_ai.extension.install_source != '' and event.alerts[0].gen_ai.extension.install_source != null %}{{ event.alerts[0].gen_ai.extension.install_source | downcase }}{% else %}user{% endif %}" + user_name: "{% if inputs.user_name != '' and inputs.user_name != null %}{{ inputs.user_name }}{% elsif event.alerts[0].user.name != '' and event.alerts[0].user.name != null %}{{ event.alerts[0].user.name }}{% else %}{% endif %}" + user_home: |- + {%- assign home = '' -%} + {%- if inputs.user_home != '' and inputs.user_home != null -%} + {%- assign home = inputs.user_home | strip -%} + {%- elsif event.alerts[0].user.home != '' and event.alerts[0].user.home != null -%} + {%- assign home = event.alerts[0].user.home | strip -%} + {%- else -%} + {%- assign uname = '' -%} + {%- if inputs.user_name != '' and inputs.user_name != null -%} + {%- assign uname = inputs.user_name | strip -%} + {%- elsif event.alerts[0].user.name != '' and event.alerts[0].user.name != null -%} + {%- assign uname = event.alerts[0].user.name | strip -%} + {%- endif -%} + {%- if uname != '' -%} + {%- if uname contains '/' -%}{%- assign uname = uname | split: '/' | last -%}{%- endif -%} + {%- assign uname_parts = uname | split: '\' -%} + {%- if uname_parts.size > 1 -%}{%- assign uname = uname_parts | last -%}{%- endif -%} + {%- assign os = '' -%} + {%- if event.alerts[0].host.os.type != '' and event.alerts[0].host.os.type != null -%} + {%- assign os = event.alerts[0].host.os.type | downcase -%} + {%- elsif inputs.os_type != '' and inputs.os_type != null -%} + {%- assign os = inputs.os_type | downcase -%} + {%- endif -%} + {%- if os == 'windows' -%}{%- assign home = 'C:/Users/' | append: uname -%} + {%- elsif os == 'macos' -%}{%- assign home = '/Users/' | append: uname -%} + {%- elsif os == 'linux' -%}{%- assign home = '/home/' | append: uname -%} + {%- endif -%} + {%- endif -%} + {%- endif -%} + {{ home }} + # Alert runs require install_source on the alert; do not default missing to user. + install_source: "{% if event.alerts[0]._id != '' and event.alerts[0]._id != null %}{% if event.alerts[0].gen_ai.extension.install_source != '' and event.alerts[0].gen_ai.extension.install_source != null %}{{ event.alerts[0].gen_ai.extension.install_source | downcase }}{% endif %}{% elsif inputs.install_source != '' and inputs.install_source != null %}{{ inputs.install_source | downcase }}{% else %}user{% endif %}" + host_name: "{% if event.alerts[0].host.name != '' and event.alerts[0].host.name != null %}{{ event.alerts[0].host.name }}{% else %}{% endif %}" run_mode: "{% if event.alerts[0]._id != '' and event.alerts[0]._id != null %}alert{% else %}manual{% endif %}" alert_count: "{{ event.alerts | size | default: 0 }}" - name: reject_multi_alert @@ -128,6 +188,10 @@ steps: type: console with: message: "rejected - received {{ steps.set_context.output.alert_count }} alerts; this workflow handles one extension removal at a time. outcome=rejected" + - name: fail_reject_batch + type: workflow.fail + with: + message: "Received {{ steps.set_context.output.alert_count }} alerts; this workflow handles one extension removal per run." else: # --- extension_id charset allowlist (Liquid; KQL has no regex) --- - name: check_extension_id @@ -174,6 +238,29 @@ steps: {%- endfor -%} {%- endif -%} {{ ok }} + - name: check_user_home + type: data.set + with: + user_home_ok: >- + {%- assign p = steps.set_context.output.user_home | default: '' | strip -%} + {%- assign pipe = '|' -%} + {%- assign ok = false -%} + {%- if p != '' and p.size <= 512 -%} + {%- unless p contains '..' or p contains '*' or p contains '?' or p contains ';' or p contains pipe or p contains '&' -%} + {%- assign os = steps.set_context.output.os_type -%} + {%- if os == 'windows' -%} + {%- assign c1 = p | slice: 1, 1 -%} + {%- assign c2 = p | slice: 2, 1 -%} + {%- if c1 == ':' and c2 == '/' -%}{%- assign ok = true -%} + {%- elsif c1 == ':' -%}{%- assign ok = true -%} + {%- endif -%} + {%- else -%} + {%- assign c0 = p | slice: 0, 1 -%} + {%- if c0 == '/' -%}{%- assign ok = true -%}{%- endif -%} + {%- endif -%} + {%- endunless -%} + {%- endif -%} + {{ ok }} - name: validate_extension_id type: if condition: 'not steps.check_extension_id.output.extension_id_ok: "true"' @@ -182,15 +269,23 @@ steps: type: console with: message: "rejected - extension_id failed charset allowlist (expect publisher.extension). outcome=rejected" + - name: fail_bad_ext + type: workflow.fail + with: + message: "extension_id failed charset allowlist (expect publisher.extension)." else: - name: validate type: if - condition: 'not steps.check_profile.output.profile_ok: "true" OR steps.set_context.output.agent_id: "" OR steps.set_context.output.extension_id: "" OR steps.set_context.output.os_type: "" OR not steps.set_context.output.install_source: "user" OR (not steps.set_context.output.editor: "vscode" AND not steps.set_context.output.editor: "cursor")' + condition: 'not steps.check_profile.output.profile_ok: "true" OR not steps.check_user_home.output.user_home_ok: "true" OR steps.set_context.output.agent_id: "" OR steps.set_context.output.extension_id: "" OR steps.set_context.output.os_type: "" OR not steps.set_context.output.install_source: "user" OR (not steps.set_context.output.editor: "vscode" AND not steps.set_context.output.editor: "cursor")' steps: - name: log_skip type: console with: - message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user, and a safe non-blank profile name (omit or set default; whitespace-only rejected; Defend runs as SYSTEM/root and cannot verify built-in/policy). outcome=skipped" + message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user (alert field required on alert runs), absolute user_home (or derivable user.name), and a safe non-blank profile. outcome=skipped" + - name: fail_skip + type: workflow.fail + with: + message: "Missing required fields for extension removal (agent/os/editor/extension/install_source=user/user_home/profile)." else: - name: decide_enforce type: data.set @@ -203,66 +298,102 @@ steps: - name: log_audit type: console with: - message: "audited - would remove editor={{ steps.set_context.output.editor }} extension={{ steps.set_context.output.extension_id }} profile={{ steps.set_context.output.profile }} agent={{ steps.set_context.output.agent_id }} outcome=audited" + message: "audited - would remove editor={{ steps.set_context.output.editor }} extension={{ steps.set_context.output.extension_id }} profile={{ steps.set_context.output.profile }} user_home={{ steps.set_context.output.user_home }} agent={{ steps.set_context.output.agent_id }} host={{ steps.set_context.output.host_name }} outcome=audited" else: - - name: set_cmd - type: data.set - with: - command: |- - {%- assign ed = steps.set_context.output.editor -%} - {%- assign eid = steps.set_context.output.extension_id -%} - {%- if steps.set_context.output.os_type == 'windows' -%} - {%- if ed == 'cursor' -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='cursor'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" - {%- else -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $bin='code'; if($bin -notin @('cursor','code')){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ steps.set_context.output.profile | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42}; & $bin --uninstall-extension $eid --force --profile $prof } else { & $bin --uninstall-extension $eid --force }; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" - {%- endif -%} - {%- else -%} - eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; bin={% if ed == 'cursor' %}cursor{% else %}code{% endif %}; command -v "$bin" >/dev/null || exit 47; prof='{{ steps.set_context.output.profile | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" - {%- endif -%} - - name: execute_remove - type: kibana.request - with: - method: POST - path: /api/endpoint/action/execute - headers: - kbn-xsrf: "true" - body: - agent_type: endpoint - endpoint_ids: - - "{{ steps.set_context.output.agent_id }}" - comment: "T05 remove-selected-ai-extension {{ steps.set_context.output.extension_id }}" - parameters: - command: "{{ steps.set_cmd.output.command }}" - timeout: 120 - - name: remember - type: data.set + - name: approve_remove + type: waitForApproval + timeout: 1h with: - action_id: "{{ steps.execute_remove.output.data.id | default: steps.execute_remove.output.id | default: '' }}" - completed: false - - name: poll - type: while - condition: 'variables.action_id: * and not variables.action_id: "" and not variables.completed: true' - max-iterations: - limit: 36 - on-limit: continue + message: >- + Remove {{ steps.set_context.output.editor }} extension + {{ steps.set_context.output.extension_id }} + (profile={{ steps.set_context.output.profile }}) + from agent={{ steps.set_context.output.agent_id }} + host={{ steps.set_context.output.host_name }} + user_home={{ steps.set_context.output.user_home }}? + approveLabel: Remove extension + rejectLabel: Hold + - name: after_approval + type: if + condition: 'steps.approve_remove.output.response.approved: true' steps: - - name: wait_poll - type: wait + - name: set_cmd + type: data.set with: - duration: 5s - - name: fetch + command: |- + {%- assign ed = steps.set_context.output.editor -%} + {%- assign eid = steps.set_context.output.extension_id -%} + {%- assign home = steps.set_context.output.user_home -%} + {%- assign prof = steps.set_context.output.profile -%} + {%- if steps.set_context.output.os_type == 'windows' -%} + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $homePath=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ home | base64_encode }}')); if([string]::IsNullOrWhiteSpace($homePath)){exit 42}; if($homePath -match '^[A-Za-z]:/'){ $homePath=$homePath.Substring(0,1)+':\'+$homePath.Substring(3).Replace('/','\') }; if($homePath -notmatch '^[A-Za-z]:\\' -or $homePath.Contains('..') -or $homePath.Contains('*') -or $homePath.Contains('?') -or $homePath.Contains(';') -or $homePath.Contains('|') -or $homePath.Contains('&')){exit 42}; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ prof | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42} }; $localApp=Join-Path $homePath 'AppData\Local'; $roaming=Join-Path $homePath 'AppData\Roaming'; {% if ed == 'cursor' %}$extDir=Join-Path $homePath '.cursor\extensions'; $userData=Join-Path $roaming 'Cursor'; $bins=@((Join-Path $localApp 'Programs\cursor\resources\app\bin\cursor.cmd'),(Join-Path $localApp 'Programs\Cursor\resources\app\bin\cursor.cmd')){% else %}$extDir=Join-Path $homePath '.vscode\extensions'; $userData=Join-Path $roaming 'Code'; $bins=@((Join-Path $localApp 'Programs\Microsoft VS Code\bin\code.cmd'),(Join-Path ${env:ProgramFiles} 'Microsoft VS Code\bin\code.cmd')){% endif %}; $bin=$bins | Where-Object { Test-Path $_ } | Select-Object -First 1; if(-not $bin){exit 47}; $args=@('--extensions-dir',$extDir,'--user-data-dir',$userData,'--uninstall-extension',$eid,'--force'); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ $args+='--profile'; $args+=$prof }; & $bin @args; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + {%- else -%} + home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; bin=""; for c in "$home/.local/bin/cursor" /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do if [ -x "$c" ]; then bin=$c; break; fi; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; bin=""; for c in "$home/.local/bin/code" /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do if [ -x "$c" ]; then bin=$c; break; fi; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + {%- endif -%} + - name: execute_remove type: kibana.request - on-failure: - continue: true with: - method: GET - path: /api/endpoint/action/{{ variables.action_id | url_encode }} - - name: mark + method: POST + path: /api/endpoint/action/execute + headers: + kbn-xsrf: "true" + body: + agent_type: endpoint + endpoint_ids: + - "{{ steps.set_context.output.agent_id }}" + comment: "remove-selected-ai-extension {{ steps.set_context.output.extension_id }}" + parameters: + command: "{{ steps.set_cmd.output.command }}" + timeout: 120 + - name: remember type: data.set with: - completed: "{%- assign d = steps.fetch.output.data -%}{%- if variables.action_id != '' and d.id == variables.action_id and d.isCompleted -%}true{%- else -%}false{%- endif -%}" - - name: summarize - type: console - with: - message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} outcome={% if variables.completed == 'true' and steps.fetch.output.data.wasSuccessful and steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code == 0 %}verified{% elsif variables.completed == 'true' %}failed{% else %}unknown{% endif %}" + action_id: "{{ steps.execute_remove.output.data.id | default: steps.execute_remove.output.id | default: '' }}" + completed: false + - name: poll + type: while + condition: 'variables.action_id: * and not variables.action_id: "" and not variables.completed: true' + max-iterations: + limit: 36 + on-limit: continue + steps: + - name: wait_poll + type: wait + with: + duration: 5s + - name: fetch + type: kibana.request + on-failure: + continue: true + with: + method: GET + path: /api/endpoint/action/{{ variables.action_id | url_encode }} + - name: mark + type: data.set + with: + completed: "{%- assign d = steps.fetch.output.data -%}{%- if variables.action_id != '' and d.id == variables.action_id and d.isCompleted -%}true{%- else -%}false{%- endif -%}" + - name: set_outcome + type: data.set + with: + outcome: "{% if variables.completed == 'true' and steps.fetch.output.data.wasSuccessful and steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code == 0 %}verified{% elsif variables.completed == 'true' %}failed{% else %}unknown{% endif %}" + - name: summarize + type: console + with: + message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} user_home={{ steps.set_context.output.user_home }} outcome={{ steps.set_outcome.output.outcome }}" + - name: fail_unverified + type: if + condition: 'not steps.set_outcome.output.outcome: "verified"' + steps: + - name: fail_remove + type: workflow.fail + with: + message: "Extension removal did not verify for {{ steps.set_context.output.extension_id }} (outcome={{ steps.set_outcome.output.outcome }})." + else: + - name: log_approval_rejected + type: console + with: + message: "rejected - analyst declined extension removal for {{ steps.set_context.output.extension_id }} outcome=rejected" + - name: fail_approval_rejected + type: workflow.fail + with: + message: "Analyst declined removal of {{ steps.set_context.output.extension_id }}." diff --git a/node_modules b/node_modules new file mode 120000 index 00000000..0ba5700e --- /dev/null +++ b/node_modules @@ -0,0 +1 @@ +/Users/stryker/workspace/Elastic/workflows/node_modules \ No newline at end of file From 8d2fff4761c22b3f8e82c394dcad4f4e714a7b3d Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Mon, 30 Nov 2026 17:28:45 -0600 Subject: [PATCH 11/20] chore: stop tracking local node_modules symlink --- .gitignore | 2 +- node_modules | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 120000 node_modules diff --git a/.gitignore b/.gitignore index 4b0506e5..5ac92512 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,4 @@ workflows-library-*.tar.gz.sha256 .env .env.* *.key -*.pem \ No newline at end of file +*.pemnode_modules diff --git a/node_modules b/node_modules deleted file mode 120000 index 0ba5700e..00000000 --- a/node_modules +++ /dev/null @@ -1 +0,0 @@ -/Users/stryker/workspace/Elastic/workflows/node_modules \ No newline at end of file From a884b7378977a8ec9b7cc98f2d6e5e4f81144726 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Mon, 30 Nov 2026 19:35:09 -0600 Subject: [PATCH 12/20] fix: restore standalone *.pem gitignore rule --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5ac92512..f19ede28 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,4 @@ workflows-library-*.tar.gz.sha256 .env .env.* *.key -*.pemnode_modules +*.pem From b9767e0df7dd00e7e47ddf51c65241a7ea69fcb4 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Mon, 30 Nov 2026 19:50:43 -0600 Subject: [PATCH 13/20] fix: harden user_home and trusted editor binaries for enforce Require absolute user_home only on enforce (input or alert user.home, never username-derived), include home in concurrency, raise settings timeout for approval, and launch uninstall from Program Files / system paths only. --- .../remove-selected-ai-extension.yaml | 241 +++++++++--------- 1 file changed, 117 insertions(+), 124 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 73355772..87ff1ef4 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -32,9 +32,11 @@ description: >- missing field skips). Requires Elastic Defend on the host, the execute response action (Enterprise license), and Execute Operations privilege for the workflow identity. Enforce uses Defend execute as SYSTEM/root and must - receive the target user home (input or alert user.home / user.name) so the - editor CLI runs with --extensions-dir and --user-data-dir against that - profile. Audit by default; enforce pauses for analyst approval. + receive the target user home (manual user_home input or alert user.home) so + the editor CLI runs with --extensions-dir and --user-data-dir against that + profile; home is never guessed from username. Uninstall uses trusted + system/editor install binaries only (not user-writable paths). Audit by + default; enforce pauses for analyst approval. enabled: true tags: - security @@ -43,7 +45,8 @@ tags: - response - genai settings: - timeout: 15m + # Cover waitForApproval (1h) plus Defend execute. + timeout: 75m concurrency: key: |- {%- assign eid = inputs.extension_id | default: event.alerts[0].gen_ai.extension.id | default: '' | strip | downcase | truncate: 256, '' -%} @@ -67,7 +70,13 @@ settings: {%- assign prof = 'default' -%} {%- endif -%} {%- assign aid = inputs.agent_id | default: event.alerts[0].agent.id | default: event.alerts[0].elastic.agent.id | default: 'none' | strip | truncate: 128, '' -%} - {{ workflow.id }}-ext-{{ eid | base64_encode }}|{{ ed | base64_encode }}|{{ prof | base64_encode }}|{{ aid | base64_encode }} + {%- assign home = '' -%} + {%- if inputs.user_home != '' and inputs.user_home != null -%} + {%- assign home = inputs.user_home | strip | truncate: 512, '' -%} + {%- elsif event.alerts[0].user.home != '' and event.alerts[0].user.home != null -%} + {%- assign home = event.alerts[0].user.home | strip | truncate: 512, '' -%} + {%- endif -%} + {{ workflow.id }}-ext-{{ eid | base64_encode }}|{{ ed | base64_encode }}|{{ prof | base64_encode }}|{{ aid | base64_encode }}|{{ home | base64_encode }} strategy: drop max: 1 consts: @@ -101,14 +110,7 @@ triggers: default: "" description: >- Absolute home/profile root for the target user (required for enforce). - Prefer this over deriving from user_name. - - name: user_name - type: string - required: false - default: "" - description: >- - Account name used to derive home when user_home is empty - (/home/NAME, /Users/NAME, or C:\Users\NAME). + Alert runs may use event.alerts[0].user.home; never guessed from username. - name: install_source type: string required: false @@ -144,35 +146,12 @@ steps: {%- else -%} default {%- endif -%} - user_name: "{% if inputs.user_name != '' and inputs.user_name != null %}{{ inputs.user_name }}{% elsif event.alerts[0].user.name != '' and event.alerts[0].user.name != null %}{{ event.alerts[0].user.name }}{% else %}{% endif %}" user_home: |- {%- assign home = '' -%} {%- if inputs.user_home != '' and inputs.user_home != null -%} {%- assign home = inputs.user_home | strip -%} {%- elsif event.alerts[0].user.home != '' and event.alerts[0].user.home != null -%} {%- assign home = event.alerts[0].user.home | strip -%} - {%- else -%} - {%- assign uname = '' -%} - {%- if inputs.user_name != '' and inputs.user_name != null -%} - {%- assign uname = inputs.user_name | strip -%} - {%- elsif event.alerts[0].user.name != '' and event.alerts[0].user.name != null -%} - {%- assign uname = event.alerts[0].user.name | strip -%} - {%- endif -%} - {%- if uname != '' -%} - {%- if uname contains '/' -%}{%- assign uname = uname | split: '/' | last -%}{%- endif -%} - {%- assign uname_parts = uname | split: '\' -%} - {%- if uname_parts.size > 1 -%}{%- assign uname = uname_parts | last -%}{%- endif -%} - {%- assign os = '' -%} - {%- if event.alerts[0].host.os.type != '' and event.alerts[0].host.os.type != null -%} - {%- assign os = event.alerts[0].host.os.type | downcase -%} - {%- elsif inputs.os_type != '' and inputs.os_type != null -%} - {%- assign os = inputs.os_type | downcase -%} - {%- endif -%} - {%- if os == 'windows' -%}{%- assign home = 'C:/Users/' | append: uname -%} - {%- elsif os == 'macos' -%}{%- assign home = '/Users/' | append: uname -%} - {%- elsif os == 'linux' -%}{%- assign home = '/home/' | append: uname -%} - {%- endif -%} - {%- endif -%} {%- endif -%} {{ home }} # Alert runs require install_source on the alert; do not default missing to user. @@ -249,10 +228,11 @@ steps: {%- unless p contains '..' or p contains '*' or p contains '?' or p contains ';' or p contains pipe or p contains '&' -%} {%- assign os = steps.set_context.output.os_type -%} {%- if os == 'windows' -%} + {%- assign bs = '\' -%} {%- assign c1 = p | slice: 1, 1 -%} {%- assign c2 = p | slice: 2, 1 -%} {%- if c1 == ':' and c2 == '/' -%}{%- assign ok = true -%} - {%- elsif c1 == ':' -%}{%- assign ok = true -%} + {%- elsif c1 == ':' and c2 == bs -%}{%- assign ok = true -%} {%- endif -%} {%- else -%} {%- assign c0 = p | slice: 0, 1 -%} @@ -276,16 +256,16 @@ steps: else: - name: validate type: if - condition: 'not steps.check_profile.output.profile_ok: "true" OR not steps.check_user_home.output.user_home_ok: "true" OR steps.set_context.output.agent_id: "" OR steps.set_context.output.extension_id: "" OR steps.set_context.output.os_type: "" OR not steps.set_context.output.install_source: "user" OR (not steps.set_context.output.editor: "vscode" AND not steps.set_context.output.editor: "cursor")' + condition: 'not steps.check_profile.output.profile_ok: "true" OR steps.set_context.output.agent_id: "" OR steps.set_context.output.extension_id: "" OR steps.set_context.output.os_type: "" OR not steps.set_context.output.install_source: "user" OR (not steps.set_context.output.editor: "vscode" AND not steps.set_context.output.editor: "cursor")' steps: - name: log_skip type: console with: - message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user (alert field required on alert runs), absolute user_home (or derivable user.name), and a safe non-blank profile. outcome=skipped" + message: "skipped - need agent_id, os_type, editor vscode|cursor, extension_id, install_source=user (alert field required on alert runs), and a safe non-blank profile. outcome=skipped" - name: fail_skip type: workflow.fail with: - message: "Missing required fields for extension removal (agent/os/editor/extension/install_source=user/user_home/profile)." + message: "Missing required fields for extension removal (agent/os/editor/extension/install_source=user/profile)." else: - name: decide_enforce type: data.set @@ -300,100 +280,113 @@ steps: with: message: "audited - would remove editor={{ steps.set_context.output.editor }} extension={{ steps.set_context.output.extension_id }} profile={{ steps.set_context.output.profile }} user_home={{ steps.set_context.output.user_home }} agent={{ steps.set_context.output.agent_id }} host={{ steps.set_context.output.host_name }} outcome=audited" else: - - name: approve_remove - type: waitForApproval - timeout: 1h - with: - message: >- - Remove {{ steps.set_context.output.editor }} extension - {{ steps.set_context.output.extension_id }} - (profile={{ steps.set_context.output.profile }}) - from agent={{ steps.set_context.output.agent_id }} - host={{ steps.set_context.output.host_name }} - user_home={{ steps.set_context.output.user_home }}? - approveLabel: Remove extension - rejectLabel: Hold - - name: after_approval + - name: require_user_home type: if - condition: 'steps.approve_remove.output.response.approved: true' + condition: 'not steps.check_user_home.output.user_home_ok: "true"' steps: - - name: set_cmd - type: data.set + - name: log_missing_home + type: console with: - command: |- - {%- assign ed = steps.set_context.output.editor -%} - {%- assign eid = steps.set_context.output.extension_id -%} - {%- assign home = steps.set_context.output.user_home -%} - {%- assign prof = steps.set_context.output.profile -%} - {%- if steps.set_context.output.os_type == 'windows' -%} - powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $homePath=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ home | base64_encode }}')); if([string]::IsNullOrWhiteSpace($homePath)){exit 42}; if($homePath -match '^[A-Za-z]:/'){ $homePath=$homePath.Substring(0,1)+':\'+$homePath.Substring(3).Replace('/','\') }; if($homePath -notmatch '^[A-Za-z]:\\' -or $homePath.Contains('..') -or $homePath.Contains('*') -or $homePath.Contains('?') -or $homePath.Contains(';') -or $homePath.Contains('|') -or $homePath.Contains('&')){exit 42}; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ prof | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42} }; $localApp=Join-Path $homePath 'AppData\Local'; $roaming=Join-Path $homePath 'AppData\Roaming'; {% if ed == 'cursor' %}$extDir=Join-Path $homePath '.cursor\extensions'; $userData=Join-Path $roaming 'Cursor'; $bins=@((Join-Path $localApp 'Programs\cursor\resources\app\bin\cursor.cmd'),(Join-Path $localApp 'Programs\Cursor\resources\app\bin\cursor.cmd')){% else %}$extDir=Join-Path $homePath '.vscode\extensions'; $userData=Join-Path $roaming 'Code'; $bins=@((Join-Path $localApp 'Programs\Microsoft VS Code\bin\code.cmd'),(Join-Path ${env:ProgramFiles} 'Microsoft VS Code\bin\code.cmd')){% endif %}; $bin=$bins | Where-Object { Test-Path $_ } | Select-Object -First 1; if(-not $bin){exit 47}; $args=@('--extensions-dir',$extDir,'--user-data-dir',$userData,'--uninstall-extension',$eid,'--force'); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ $args+='--profile'; $args+=$prof }; & $bin @args; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" - {%- else -%} - home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; bin=""; for c in "$home/.local/bin/cursor" /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do if [ -x "$c" ]; then bin=$c; break; fi; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; bin=""; for c in "$home/.local/bin/code" /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do if [ -x "$c" ]; then bin=$c; break; fi; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" - {%- endif -%} - - name: execute_remove - type: kibana.request + message: "skipped - enforce requires absolute user_home (manual input or alert user.home; never guessed from username). outcome=skipped" + - name: fail_missing_home + type: workflow.fail with: - method: POST - path: /api/endpoint/action/execute - headers: - kbn-xsrf: "true" - body: - agent_type: endpoint - endpoint_ids: - - "{{ steps.set_context.output.agent_id }}" - comment: "remove-selected-ai-extension {{ steps.set_context.output.extension_id }}" - parameters: - command: "{{ steps.set_cmd.output.command }}" - timeout: 120 - - name: remember - type: data.set + message: "Enforce requires absolute user_home (input or alert user.home)." + else: + - name: approve_remove + type: waitForApproval + timeout: 1h with: - action_id: "{{ steps.execute_remove.output.data.id | default: steps.execute_remove.output.id | default: '' }}" - completed: false - - name: poll - type: while - condition: 'variables.action_id: * and not variables.action_id: "" and not variables.completed: true' - max-iterations: - limit: 36 - on-limit: continue + message: >- + Remove {{ steps.set_context.output.editor }} extension + {{ steps.set_context.output.extension_id }} + (profile={{ steps.set_context.output.profile }}) + from agent={{ steps.set_context.output.agent_id }} + host={{ steps.set_context.output.host_name }} + user_home={{ steps.set_context.output.user_home }}? + approveLabel: Remove extension + rejectLabel: Hold + - name: after_approval + type: if + condition: 'steps.approve_remove.output.response.approved: true' steps: - - name: wait_poll - type: wait + - name: set_cmd + type: data.set with: - duration: 5s - - name: fetch + command: |- + {%- assign ed = steps.set_context.output.editor -%} + {%- assign eid = steps.set_context.output.extension_id -%} + {%- assign home = steps.set_context.output.user_home -%} + {%- assign prof = steps.set_context.output.profile -%} + {%- if steps.set_context.output.os_type == 'windows' -%} + powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $homePath=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ home | base64_encode }}')); if([string]::IsNullOrWhiteSpace($homePath)){exit 42}; if($homePath -match '^[A-Za-z]:/'){ $homePath=$homePath.Substring(0,1)+':\'+$homePath.Substring(3).Replace('/','\') }; if($homePath -notmatch '^[A-Za-z]:\\' -or $homePath.Contains('..') -or $homePath.Contains('*') -or $homePath.Contains('?') -or $homePath.Contains(';') -or $homePath.Contains('|') -or $homePath.Contains('&')){exit 42}; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ prof | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42} }; $roaming=Join-Path $homePath 'AppData\Roaming'; {% if ed == 'cursor' %}$extDir=Join-Path $homePath '.cursor\extensions'; $userData=Join-Path $roaming 'Cursor'; $bins=@((Join-Path ${env:ProgramFiles} 'Cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles} 'cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Cursor\resources\app\bin\cursor.cmd')){% else %}$extDir=Join-Path $homePath '.vscode\extensions'; $userData=Join-Path $roaming 'Code'; $bins=@((Join-Path ${env:ProgramFiles} 'Microsoft VS Code\bin\code.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Microsoft VS Code\bin\code.cmd')){% endif %}; $bin=$bins | Where-Object { Test-Path $_ } | Select-Object -First 1; if(-not $bin){exit 47}; $args=@('--extensions-dir',$extDir,'--user-data-dir',$userData,'--uninstall-extension',$eid,'--force'); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ $args+='--profile'; $args+=$prof }; & $bin @args; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" + {%- else -%} + home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do if [ -x "$c" ]; then bin=$c; break; fi; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do if [ -x "$c" ]; then bin=$c; break; fi; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + {%- endif -%} + - name: execute_remove type: kibana.request - on-failure: - continue: true with: - method: GET - path: /api/endpoint/action/{{ variables.action_id | url_encode }} - - name: mark + method: POST + path: /api/endpoint/action/execute + headers: + kbn-xsrf: "true" + body: + agent_type: endpoint + endpoint_ids: + - "{{ steps.set_context.output.agent_id }}" + comment: "remove-selected-ai-extension {{ steps.set_context.output.extension_id }}" + parameters: + command: "{{ steps.set_cmd.output.command }}" + timeout: 120 + - name: remember type: data.set with: - completed: "{%- assign d = steps.fetch.output.data -%}{%- if variables.action_id != '' and d.id == variables.action_id and d.isCompleted -%}true{%- else -%}false{%- endif -%}" - - name: set_outcome - type: data.set - with: - outcome: "{% if variables.completed == 'true' and steps.fetch.output.data.wasSuccessful and steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code == 0 %}verified{% elsif variables.completed == 'true' %}failed{% else %}unknown{% endif %}" - - name: summarize - type: console - with: - message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} user_home={{ steps.set_context.output.user_home }} outcome={{ steps.set_outcome.output.outcome }}" - - name: fail_unverified - type: if - condition: 'not steps.set_outcome.output.outcome: "verified"' - steps: - - name: fail_remove + action_id: "{{ steps.execute_remove.output.data.id | default: steps.execute_remove.output.id | default: '' }}" + completed: false + - name: poll + type: while + condition: 'variables.action_id: * and not variables.action_id: "" and not variables.completed: true' + max-iterations: + limit: 36 + on-limit: continue + steps: + - name: wait_poll + type: wait + with: + duration: 5s + - name: fetch + type: kibana.request + on-failure: + continue: true + with: + method: GET + path: /api/endpoint/action/{{ variables.action_id | url_encode }} + - name: mark + type: data.set + with: + completed: "{%- assign d = steps.fetch.output.data -%}{%- if variables.action_id != '' and d.id == variables.action_id and d.isCompleted -%}true{%- else -%}false{%- endif -%}" + - name: set_outcome + type: data.set + with: + outcome: "{% if variables.completed == 'true' and steps.fetch.output.data.wasSuccessful and steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code == 0 %}verified{% elsif variables.completed == 'true' %}failed{% else %}unknown{% endif %}" + - name: summarize + type: console + with: + message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} user_home={{ steps.set_context.output.user_home }} outcome={{ steps.set_outcome.output.outcome }}" + - name: fail_unverified + type: if + condition: 'not steps.set_outcome.output.outcome: "verified"' + steps: + - name: fail_remove + type: workflow.fail + with: + message: "Extension removal did not verify for {{ steps.set_context.output.extension_id }} (outcome={{ steps.set_outcome.output.outcome }})." + else: + - name: log_approval_rejected + type: console + with: + message: "rejected - analyst declined extension removal for {{ steps.set_context.output.extension_id }} outcome=rejected" + - name: fail_approval_rejected type: workflow.fail with: - message: "Extension removal did not verify for {{ steps.set_context.output.extension_id }} (outcome={{ steps.set_outcome.output.outcome }})." - else: - - name: log_approval_rejected - type: console - with: - message: "rejected - analyst declined extension removal for {{ steps.set_context.output.extension_id }} outcome=rejected" - - name: fail_approval_rejected - type: workflow.fail - with: - message: "Analyst declined removal of {{ steps.set_context.output.extension_id }}." + message: "Analyst declined removal of {{ steps.set_context.output.extension_id }}." From ea6939baa18025a46ec95d05a9f943436987fa4f Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Tue, 1 Dec 2026 18:48:17 -0600 Subject: [PATCH 14/20] fix: normalize home concurrency and clarify trusted binary failures Strip trailing slashes from concurrency home keys, reject Unix launchers whose symlink target resolves under user-writable paths, document system-wide-only installs, and map shell_code=47 to no_trusted_binary. --- .../remove-selected-ai-extension.yaml | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 87ff1ef4..0d7e17c3 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -9,8 +9,9 @@ template-metadata: install_source=user (alert runs require that field). Requires Elastic Defend with the execute response action (Enterprise) and Execute Operations privilege. Targets the developer's extensions directory via - user home (--extensions-dir / --user-data-dir). Audit by default; - enforce pauses for analyst approval. + user home (--extensions-dir / --user-data-dir). Enforce uses + system-wide editor binaries only (not per-user LOCALAPPDATA). Audit + by default; enforce pauses for analyst approval. solutions: [security] categories: [response] install: @@ -35,7 +36,9 @@ description: >- receive the target user home (manual user_home input or alert user.home) so the editor CLI runs with --extensions-dir and --user-data-dir against that profile; home is never guessed from username. Uninstall uses trusted - system/editor install binaries only (not user-writable paths). Audit by + system-wide editor binaries only (Program Files / Applications / usr — + not %LOCALAPPDATA% or other user-writable paths). Per-user installs are + unsupported and fail with shell_code=47 (no trusted binary). Audit by default; enforce pauses for analyst approval. enabled: true tags: @@ -76,6 +79,15 @@ settings: {%- elsif event.alerts[0].user.home != '' and event.alerts[0].user.home != null -%} {%- assign home = event.alerts[0].user.home | strip | truncate: 512, '' -%} {%- endif -%} + {%- assign bs = '\' -%} + {%- assign last = home | slice: -1, 1 -%} + {%- if home.size > 1 and last == '/' -%} + {%- assign hlen = home.size | minus: 1 -%} + {%- assign home = home | slice: 0, hlen -%} + {%- elsif home.size > 1 and last == bs -%} + {%- assign hlen = home.size | minus: 1 -%} + {%- assign home = home | slice: 0, hlen -%} + {%- endif -%} {{ workflow.id }}-ext-{{ eid | base64_encode }}|{{ ed | base64_encode }}|{{ prof | base64_encode }}|{{ aid | base64_encode }}|{{ home | base64_encode }} strategy: drop max: 1 @@ -304,6 +316,9 @@ steps: from agent={{ steps.set_context.output.agent_id }} host={{ steps.set_context.output.host_name }} user_home={{ steps.set_context.output.user_home }}? + Requires a system-wide editor install (Program Files / + Applications / usr). Per-user LOCALAPPDATA installs + are unsupported and will fail with no trusted binary. approveLabel: Remove extension rejectLabel: Hold - name: after_approval @@ -321,7 +336,7 @@ steps: {%- if steps.set_context.output.os_type == 'windows' -%} powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $homePath=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ home | base64_encode }}')); if([string]::IsNullOrWhiteSpace($homePath)){exit 42}; if($homePath -match '^[A-Za-z]:/'){ $homePath=$homePath.Substring(0,1)+':\'+$homePath.Substring(3).Replace('/','\') }; if($homePath -notmatch '^[A-Za-z]:\\' -or $homePath.Contains('..') -or $homePath.Contains('*') -or $homePath.Contains('?') -or $homePath.Contains(';') -or $homePath.Contains('|') -or $homePath.Contains('&')){exit 42}; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ prof | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42} }; $roaming=Join-Path $homePath 'AppData\Roaming'; {% if ed == 'cursor' %}$extDir=Join-Path $homePath '.cursor\extensions'; $userData=Join-Path $roaming 'Cursor'; $bins=@((Join-Path ${env:ProgramFiles} 'Cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles} 'cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Cursor\resources\app\bin\cursor.cmd')){% else %}$extDir=Join-Path $homePath '.vscode\extensions'; $userData=Join-Path $roaming 'Code'; $bins=@((Join-Path ${env:ProgramFiles} 'Microsoft VS Code\bin\code.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Microsoft VS Code\bin\code.cmd')){% endif %}; $bin=$bins | Where-Object { Test-Path $_ } | Select-Object -First 1; if(-not $bin){exit 47}; $args=@('--extensions-dir',$extDir,'--user-data-dir',$userData,'--uninstall-extension',$eid,'--force'); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ $args+='--profile'; $args+=$prof }; & $bin @args; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- else -%} - home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do if [ -x "$c" ]; then bin=$c; break; fi; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do if [ -x "$c" ]; then bin=$c; break; fi; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do [ -x "$c" ] || continue; t="$c"; if [ -L "$c" ]; then t=$(readlink "$c" 2>/dev/null || true); case "$t" in /*) ;; "") t="$c";; *) t="$(dirname "$c")/$t";; esac; fi; case "$t" in */home/*|*/Users/*|*/tmp/*) continue;; esac; bin="$c"; break; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do [ -x "$c" ] || continue; t="$c"; if [ -L "$c" ]; then t=$(readlink "$c" 2>/dev/null || true); case "$t" in /*) ;; "") t="$c";; *) t="$(dirname "$c")/$t";; esac; fi; case "$t" in */home/*|*/Users/*|*/tmp/*) continue;; esac; bin="$c"; break; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" {%- endif -%} - name: execute_remove type: kibana.request @@ -368,11 +383,11 @@ steps: - name: set_outcome type: data.set with: - outcome: "{% if variables.completed == 'true' and steps.fetch.output.data.wasSuccessful and steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code == 0 %}verified{% elsif variables.completed == 'true' %}failed{% else %}unknown{% endif %}" + outcome: "{% assign sc = steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code | default: '' %}{% if variables.completed == 'true' and steps.fetch.output.data.wasSuccessful and sc == 0 %}verified{% elsif variables.completed == 'true' and sc == 47 %}no_trusted_binary{% elsif variables.completed == 'true' %}failed{% else %}unknown{% endif %}" - name: summarize type: console with: - message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} user_home={{ steps.set_context.output.user_home }} outcome={{ steps.set_outcome.output.outcome }}" + message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} user_home={{ steps.set_context.output.user_home }} outcome={{ steps.set_outcome.output.outcome }}{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %} (no trusted system-wide editor binary under Program Files / Applications / usr){% endif %}" - name: fail_unverified type: if condition: 'not steps.set_outcome.output.outcome: "verified"' @@ -380,7 +395,7 @@ steps: - name: fail_remove type: workflow.fail with: - message: "Extension removal did not verify for {{ steps.set_context.output.extension_id }} (outcome={{ steps.set_outcome.output.outcome }})." + message: "{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %}No trusted system-wide {{ steps.set_context.output.editor }} binary found (shell_code=47). Per-user installs under LOCALAPPDATA / home are unsupported.{% else %}Extension removal did not verify for {{ steps.set_context.output.extension_id }} (outcome={{ steps.set_outcome.output.outcome }}).{% endif %}" else: - name: log_approval_rejected type: console From 9f38d25f3eebcede26b154bebfeb4095ee9c76fd Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Tue, 1 Dec 2026 18:54:51 -0600 Subject: [PATCH 15/20] fix: strip trailing backslash only for Windows homes Keep trailing backslash significant on Linux/macOS so distinct homes do not collapse into one concurrency key. --- .../remove-selected-ai-extension.yaml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 0d7e17c3..e361abf4 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -79,15 +79,20 @@ settings: {%- elsif event.alerts[0].user.home != '' and event.alerts[0].user.home != null -%} {%- assign home = event.alerts[0].user.home | strip | truncate: 512, '' -%} {%- endif -%} - {%- assign bs = '\' -%} {%- assign last = home | slice: -1, 1 -%} {%- if home.size > 1 and last == '/' -%} {%- assign hlen = home.size | minus: 1 -%} {%- assign home = home | slice: 0, hlen -%} - {%- elsif home.size > 1 and last == bs -%} + {%- endif -%} + {%- assign os_raw = inputs.os_type | default: event.alerts[0].host.os.type | default: '' | downcase -%} + {%- if os_raw == 'windows' -%} + {%- assign bs = '\' -%} + {%- assign last = home | slice: -1, 1 -%} + {%- if home.size > 1 and last == bs -%} {%- assign hlen = home.size | minus: 1 -%} {%- assign home = home | slice: 0, hlen -%} {%- endif -%} + {%- endif -%} {{ workflow.id }}-ext-{{ eid | base64_encode }}|{{ ed | base64_encode }}|{{ prof | base64_encode }}|{{ aid | base64_encode }}|{{ home | base64_encode }} strategy: drop max: 1 From 1ae465eeb3b2b690e0770f63e234ab780110661e Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Tue, 1 Dec 2026 19:25:39 -0600 Subject: [PATCH 16/20] fix: match concurrency OS resolution to set_context Prefer alert host.os.type over inputs.os_type so trailing-backslash normalization stays consistent with the uninstall path. --- .../remove-selected-ai-extension.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index e361abf4..bb372c43 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -84,7 +84,7 @@ settings: {%- assign hlen = home.size | minus: 1 -%} {%- assign home = home | slice: 0, hlen -%} {%- endif -%} - {%- assign os_raw = inputs.os_type | default: event.alerts[0].host.os.type | default: '' | downcase -%} + {%- assign os_raw = event.alerts[0].host.os.type | default: inputs.os_type | default: '' | downcase -%} {%- if os_raw == 'windows' -%} {%- assign bs = '\' -%} {%- assign last = home | slice: -1, 1 -%} From 0f255e10275bed33f939b75e2e648700327ddfd5 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Thu, 3 Dec 2026 09:01:16 -0600 Subject: [PATCH 17/20] fix: alert runs audit-only; require manual user_home and root-owned binaries Drop non-ECS alert user.home reads so enforce never pretends home comes from the alert. Document audit-only alert runs, bump to 1.0.1, and require macOS/Linux editor binaries (and parents) to be root-owned and not group/other-writable before uninstall. --- .../remove-selected-ai-extension.yaml | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index bb372c43..79755ece 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -1,17 +1,16 @@ template-metadata: slug: remove-selected-ai-extension - version: "1.0.0" + version: "1.0.1" availability: ">=9.5.0" name: "Remove Selected AI Extension" description: >- - Audit and remove one supported user-installed AI editor extension by - exact ID and profile via Defend execute. Caller must confirm - install_source=user (alert runs require that field). Requires Elastic - Defend with the execute response action (Enterprise) and Execute - Operations privilege. Targets the developer's extensions directory via - user home (--extensions-dir / --user-data-dir). Enforce uses - system-wide editor binaries only (not per-user LOCALAPPDATA). Audit - by default; enforce pauses for analyst approval. + Audit one supported user-installed AI editor extension by exact ID and + profile via Defend execute. Alert runs audit only. Enforce is a manual + run that requires absolute user_home (never guessed from username) and + a root-owned system-wide editor binary. Caller must confirm + install_source=user. Requires Elastic Defend with the execute response + action (Enterprise) and Execute Operations privilege. Audit by default; + enforce pauses for analyst approval. solutions: [security] categories: [response] install: @@ -20,7 +19,8 @@ template-metadata: label: "Enforce extension uninstall" description: >- When true, run the uninstall adapter for the verified extension id - after analyst approval. + after analyst approval. Alert runs still audit only; enforce needs a + manual run with absolute user_home. inputType: boolean required: true default: false @@ -32,14 +32,15 @@ description: >- install_source=user (alert runs require gen_ai.extension.install_source; missing field skips). Requires Elastic Defend on the host, the execute response action (Enterprise license), and Execute Operations privilege for - the workflow identity. Enforce uses Defend execute as SYSTEM/root and must - receive the target user home (manual user_home input or alert user.home) so - the editor CLI runs with --extensions-dir and --user-data-dir against that - profile; home is never guessed from username. Uninstall uses trusted - system-wide editor binaries only (Program Files / Applications / usr — - not %LOCALAPPDATA% or other user-writable paths). Per-user installs are - unsupported and fail with shell_code=47 (no trusted binary). Audit by - default; enforce pauses for analyst approval. + the workflow identity. Alert runs audit only. Enforce is a manual run that + requires absolute inputs.user_home (never guessed from username; alert + documents do not carry a home path) so Defend execute as SYSTEM/root can + target that user's extensions via --extensions-dir / --user-data-dir. + Uninstall uses trusted system-wide editor binaries only (Program Files / + Applications / usr — not %LOCALAPPDATA% or other user-writable paths). On + macOS/Linux the binary and each parent directory to / must be root-owned + and not group- or other-writable. Per-user installs fail with shell_code=47. + Audit by default; enforce pauses for analyst approval. enabled: true tags: - security @@ -76,8 +77,6 @@ settings: {%- assign home = '' -%} {%- if inputs.user_home != '' and inputs.user_home != null -%} {%- assign home = inputs.user_home | strip | truncate: 512, '' -%} - {%- elsif event.alerts[0].user.home != '' and event.alerts[0].user.home != null -%} - {%- assign home = event.alerts[0].user.home | strip | truncate: 512, '' -%} {%- endif -%} {%- assign last = home | slice: -1, 1 -%} {%- if home.size > 1 and last == '/' -%} @@ -127,7 +126,8 @@ triggers: default: "" description: >- Absolute home/profile root for the target user (required for enforce). - Alert runs may use event.alerts[0].user.home; never guessed from username. + Manual runs only — alert documents do not carry a home path; never + guessed from username. - name: install_source type: string required: false @@ -167,8 +167,6 @@ steps: {%- assign home = '' -%} {%- if inputs.user_home != '' and inputs.user_home != null -%} {%- assign home = inputs.user_home | strip -%} - {%- elsif event.alerts[0].user.home != '' and event.alerts[0].user.home != null -%} - {%- assign home = event.alerts[0].user.home | strip -%} {%- endif -%} {{ home }} # Alert runs require install_source on the alert; do not default missing to user. @@ -304,11 +302,11 @@ steps: - name: log_missing_home type: console with: - message: "skipped - enforce requires absolute user_home (manual input or alert user.home; never guessed from username). outcome=skipped" + message: "skipped - enforce requires absolute manual user_home (alert runs audit only; never guessed from username). outcome=skipped" - name: fail_missing_home type: workflow.fail with: - message: "Enforce requires absolute user_home (input or alert user.home)." + message: "Enforce requires absolute manual user_home (alert runs audit only)." else: - name: approve_remove type: waitForApproval @@ -321,9 +319,10 @@ steps: from agent={{ steps.set_context.output.agent_id }} host={{ steps.set_context.output.host_name }} user_home={{ steps.set_context.output.user_home }}? - Requires a system-wide editor install (Program Files / - Applications / usr). Per-user LOCALAPPDATA installs - are unsupported and will fail with no trusted binary. + Requires a root-owned system-wide editor install (Program Files / + Applications / usr). Per-user or user-writable + installs are unsupported and will fail with no + trusted binary. approveLabel: Remove extension rejectLabel: Hold - name: after_approval @@ -341,7 +340,7 @@ steps: {%- if steps.set_context.output.os_type == 'windows' -%} powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $homePath=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ home | base64_encode }}')); if([string]::IsNullOrWhiteSpace($homePath)){exit 42}; if($homePath -match '^[A-Za-z]:/'){ $homePath=$homePath.Substring(0,1)+':\'+$homePath.Substring(3).Replace('/','\') }; if($homePath -notmatch '^[A-Za-z]:\\' -or $homePath.Contains('..') -or $homePath.Contains('*') -or $homePath.Contains('?') -or $homePath.Contains(';') -or $homePath.Contains('|') -or $homePath.Contains('&')){exit 42}; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ prof | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42} }; $roaming=Join-Path $homePath 'AppData\Roaming'; {% if ed == 'cursor' %}$extDir=Join-Path $homePath '.cursor\extensions'; $userData=Join-Path $roaming 'Cursor'; $bins=@((Join-Path ${env:ProgramFiles} 'Cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles} 'cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Cursor\resources\app\bin\cursor.cmd')){% else %}$extDir=Join-Path $homePath '.vscode\extensions'; $userData=Join-Path $roaming 'Code'; $bins=@((Join-Path ${env:ProgramFiles} 'Microsoft VS Code\bin\code.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Microsoft VS Code\bin\code.cmd')){% endif %}; $bin=$bins | Where-Object { Test-Path $_ } | Select-Object -First 1; if(-not $bin){exit 47}; $args=@('--extensions-dir',$extDir,'--user-data-dir',$userData,'--uninstall-extension',$eid,'--force'); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ $args+='--profile'; $args+=$prof }; & $bin @args; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- else -%} - home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do [ -x "$c" ] || continue; t="$c"; if [ -L "$c" ]; then t=$(readlink "$c" 2>/dev/null || true); case "$t" in /*) ;; "") t="$c";; *) t="$(dirname "$c")/$t";; esac; fi; case "$t" in */home/*|*/Users/*|*/tmp/*) continue;; esac; bin="$c"; break; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do [ -x "$c" ] || continue; t="$c"; if [ -L "$c" ]; then t=$(readlink "$c" 2>/dev/null || true); case "$t" in /*) ;; "") t="$c";; *) t="$(dirname "$c")/$t";; esac; fi; case "$t" in */home/*|*/Users/*|*/tmp/*) continue;; esac; bin="$c"; break; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; [ $((g&2)) -eq 0 ]||return 1; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; [ $((g&2)) -eq 0 ]||return 1; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" {%- endif -%} - name: execute_remove type: kibana.request @@ -392,7 +391,7 @@ steps: - name: summarize type: console with: - message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} user_home={{ steps.set_context.output.user_home }} outcome={{ steps.set_outcome.output.outcome }}{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %} (no trusted system-wide editor binary under Program Files / Applications / usr){% endif %}" + message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} user_home={{ steps.set_context.output.user_home }} outcome={{ steps.set_outcome.output.outcome }}{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %} (no trusted root-owned system-wide editor binary under Program Files / Applications / usr){% endif %}" - name: fail_unverified type: if condition: 'not steps.set_outcome.output.outcome: "verified"' @@ -400,7 +399,7 @@ steps: - name: fail_remove type: workflow.fail with: - message: "{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %}No trusted system-wide {{ steps.set_context.output.editor }} binary found (shell_code=47). Per-user installs under LOCALAPPDATA / home are unsupported.{% else %}Extension removal did not verify for {{ steps.set_context.output.extension_id }} (outcome={{ steps.set_outcome.output.outcome }}).{% endif %}" + message: "{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %}No trusted root-owned system-wide {{ steps.set_context.output.editor }} binary found (shell_code=47). Per-user or user-writable installs are unsupported.{% else %}Extension removal did not verify for {{ steps.set_context.output.extension_id }} (outcome={{ steps.set_outcome.output.outcome }}).{% endif %}" else: - name: log_approval_rejected type: console From de0e0755844a7f7f8866275733b3f2dbe9e8eb6a Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Thu, 3 Dec 2026 13:35:37 -0600 Subject: [PATCH 18/20] fix: force alert runs to audit; allow macOS /Applications admin g+w decide_enforce now disables enforce when run_mode is alert so installed enforce no longer fails every alert on missing user_home. trust_bin still requires root-owned paths, but permits stock /Applications 775 on Darwin; document that drag-and-drop .app bundles remain unsupported (MDM/sudo only). --- .../remove-selected-ai-extension.yaml | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 79755ece..e244bfe2 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -1,13 +1,14 @@ template-metadata: slug: remove-selected-ai-extension - version: "1.0.1" + version: "1.0.2" availability: ">=9.5.0" name: "Remove Selected AI Extension" description: >- Audit one supported user-installed AI editor extension by exact ID and profile via Defend execute. Alert runs audit only. Enforce is a manual run that requires absolute user_home (never guessed from username) and - a root-owned system-wide editor binary. Caller must confirm + a root-owned system-wide editor binary (macOS: MDM/sudo installs; + drag-and-drop .app bundles unsupported). Caller must confirm install_source=user. Requires Elastic Defend with the execute response action (Enterprise) and Execute Operations privilege. Audit by default; enforce pauses for analyst approval. @@ -38,9 +39,11 @@ description: >- target that user's extensions via --extensions-dir / --user-data-dir. Uninstall uses trusted system-wide editor binaries only (Program Files / Applications / usr — not %LOCALAPPDATA% or other user-writable paths). On - macOS/Linux the binary and each parent directory to / must be root-owned - and not group- or other-writable. Per-user installs fail with shell_code=47. - Audit by default; enforce pauses for analyst approval. + Linux the binary and each parent to / must be root-owned and not group- or + other-writable. On macOS the same applies, except /Applications may be + admin group-writable (stock 775); drag-and-drop user-owned .app bundles + are unsupported (MDM/sudo root-owned installs only). Per-user installs fail + with shell_code=47. Audit by default; enforce pauses for analyst approval. enabled: true tags: - security @@ -285,7 +288,7 @@ steps: - name: decide_enforce type: data.set with: - enforce: "{% if consts.enable_enforce %}true{% else %}false{% endif %}" + enforce: "{% if steps.set_context.output.run_mode == 'alert' %}false{% elsif consts.enable_enforce %}true{% else %}false{% endif %}" - name: audit_path type: if condition: 'steps.decide_enforce.output.enforce: "false"' @@ -320,9 +323,9 @@ steps: host={{ steps.set_context.output.host_name }} user_home={{ steps.set_context.output.user_home }}? Requires a root-owned system-wide editor install (Program Files / - Applications / usr). Per-user or user-writable - installs are unsupported and will fail with no - trusted binary. + Applications / usr; macOS drag-and-drop .app + bundles unsupported). Per-user or user-writable + installs fail with no trusted binary. approveLabel: Remove extension rejectLabel: Hold - name: after_approval @@ -340,7 +343,7 @@ steps: {%- if steps.set_context.output.os_type == 'windows' -%} powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $homePath=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ home | base64_encode }}')); if([string]::IsNullOrWhiteSpace($homePath)){exit 42}; if($homePath -match '^[A-Za-z]:/'){ $homePath=$homePath.Substring(0,1)+':\'+$homePath.Substring(3).Replace('/','\') }; if($homePath -notmatch '^[A-Za-z]:\\' -or $homePath.Contains('..') -or $homePath.Contains('*') -or $homePath.Contains('?') -or $homePath.Contains(';') -or $homePath.Contains('|') -or $homePath.Contains('&')){exit 42}; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ prof | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42} }; $roaming=Join-Path $homePath 'AppData\Roaming'; {% if ed == 'cursor' %}$extDir=Join-Path $homePath '.cursor\extensions'; $userData=Join-Path $roaming 'Cursor'; $bins=@((Join-Path ${env:ProgramFiles} 'Cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles} 'cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Cursor\resources\app\bin\cursor.cmd')){% else %}$extDir=Join-Path $homePath '.vscode\extensions'; $userData=Join-Path $roaming 'Code'; $bins=@((Join-Path ${env:ProgramFiles} 'Microsoft VS Code\bin\code.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Microsoft VS Code\bin\code.cmd')){% endif %}; $bin=$bins | Where-Object { Test-Path $_ } | Select-Object -First 1; if(-not $bin){exit 47}; $args=@('--extensions-dir',$extDir,'--user-data-dir',$userData,'--uninstall-extension',$eid,'--force'); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ $args+='--profile'; $args+=$prof }; & $bin @args; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- else -%} - home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; [ $((g&2)) -eq 0 ]||return 1; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; [ $((g&2)) -eq 0 ]||return 1; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ]; then :; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ]; then :; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" {%- endif -%} - name: execute_remove type: kibana.request @@ -391,7 +394,7 @@ steps: - name: summarize type: console with: - message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} user_home={{ steps.set_context.output.user_home }} outcome={{ steps.set_outcome.output.outcome }}{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %} (no trusted root-owned system-wide editor binary under Program Files / Applications / usr){% endif %}" + message: "extension-remove action_id={{ variables.action_id }} completed={{ variables.completed }} wasSuccessful={{ steps.fetch.output.data.wasSuccessful }} shell_code={{ steps.fetch.output.data.outputs[steps.set_context.output.agent_id].content.shell_code }} user_home={{ steps.set_context.output.user_home }} outcome={{ steps.set_outcome.output.outcome }}{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %} (no trusted root-owned system-wide editor binary under Program Files / Applications / usr; macOS drag-and-drop .app unsupported){% endif %}" - name: fail_unverified type: if condition: 'not steps.set_outcome.output.outcome: "verified"' @@ -399,7 +402,7 @@ steps: - name: fail_remove type: workflow.fail with: - message: "{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %}No trusted root-owned system-wide {{ steps.set_context.output.editor }} binary found (shell_code=47). Per-user or user-writable installs are unsupported.{% else %}Extension removal did not verify for {{ steps.set_context.output.extension_id }} (outcome={{ steps.set_outcome.output.outcome }}).{% endif %}" + message: "{% if steps.set_outcome.output.outcome == 'no_trusted_binary' %}No trusted root-owned system-wide {{ steps.set_context.output.editor }} binary found (shell_code=47). Per-user, user-writable, or macOS drag-and-drop .app installs are unsupported.{% else %}Extension removal did not verify for {{ steps.set_context.output.extension_id }} (outcome={{ steps.set_outcome.output.outcome }}).{% endif %}" else: - name: log_approval_rejected type: console From 195bc32e8c469983f8c08f6f8d78e2aa6477fb30 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Thu, 3 Dec 2026 13:37:45 -0600 Subject: [PATCH 19/20] fix: simplify catalog blurb; require admin gid for /Applications g+w Keep catalog text outcome-focused per CONTRIBUTING. On Darwin, allow /Applications group-write only when the owning group is admin (gid 80). --- .../remove-selected-ai-extension.yaml | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index e244bfe2..9d12122b 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -1,17 +1,14 @@ template-metadata: slug: remove-selected-ai-extension - version: "1.0.2" + version: "1.0.3" availability: ">=9.5.0" name: "Remove Selected AI Extension" description: >- - Audit one supported user-installed AI editor extension by exact ID and - profile via Defend execute. Alert runs audit only. Enforce is a manual - run that requires absolute user_home (never guessed from username) and - a root-owned system-wide editor binary (macOS: MDM/sudo installs; - drag-and-drop .app bundles unsupported). Caller must confirm - install_source=user. Requires Elastic Defend with the execute response - action (Enterprise) and Execute Operations privilege. Audit by default; - enforce pauses for analyst approval. + Uninstall one user-installed VS Code or Cursor extension from an + endpoint with Elastic Defend execute. Audits by default; enforce runs + manually with the user's home directory after analyst approval. + Requires Defend with the execute response action (Enterprise) and a + system-wide editor install. solutions: [security] categories: [response] install: @@ -343,7 +340,7 @@ steps: {%- if steps.set_context.output.os_type == 'windows' -%} powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $homePath=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ home | base64_encode }}')); if([string]::IsNullOrWhiteSpace($homePath)){exit 42}; if($homePath -match '^[A-Za-z]:/'){ $homePath=$homePath.Substring(0,1)+':\'+$homePath.Substring(3).Replace('/','\') }; if($homePath -notmatch '^[A-Za-z]:\\' -or $homePath.Contains('..') -or $homePath.Contains('*') -or $homePath.Contains('?') -or $homePath.Contains(';') -or $homePath.Contains('|') -or $homePath.Contains('&')){exit 42}; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ prof | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42} }; $roaming=Join-Path $homePath 'AppData\Roaming'; {% if ed == 'cursor' %}$extDir=Join-Path $homePath '.cursor\extensions'; $userData=Join-Path $roaming 'Cursor'; $bins=@((Join-Path ${env:ProgramFiles} 'Cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles} 'cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Cursor\resources\app\bin\cursor.cmd')){% else %}$extDir=Join-Path $homePath '.vscode\extensions'; $userData=Join-Path $roaming 'Code'; $bins=@((Join-Path ${env:ProgramFiles} 'Microsoft VS Code\bin\code.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Microsoft VS Code\bin\code.cmd')){% endif %}; $bin=$bins | Where-Object { Test-Path $_ } | Select-Object -First 1; if(-not $bin){exit 47}; $args=@('--extensions-dir',$extDir,'--user-data-dir',$userData,'--uninstall-extension',$eid,'--force'); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ $args+='--profile'; $args+=$prof }; & $bin @args; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- else -%} - home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ]; then :; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ]; then :; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ]; then gid=$(stat -f %g "$d" 2>/dev/null)||return 1; [ "$gid" = 80 ]||return 1; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ]; then gid=$(stat -f %g "$d" 2>/dev/null)||return 1; [ "$gid" = 80 ]||return 1; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" {%- endif -%} - name: execute_remove type: kibana.request From 0e9699273cc50b0a9987e1ad350098ad84a5d4a0 Mon Sep 17 00:00:00 2001 From: Mika Ayenson Date: Thu, 3 Dec 2026 14:09:09 -0600 Subject: [PATCH 20/20] fix: require admin gid only when /Applications is group-writable Stock root:wheel 0755 /Applications already meets the non-g+w policy; only gate the Darwin exception on gid 80 when mode actually has group-write. --- .../remove-selected-ai-extension.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml index 9d12122b..90c2ad5e 100644 --- a/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml +++ b/library/workflows/remove-selected-ai-extension/remove-selected-ai-extension.yaml @@ -1,6 +1,6 @@ template-metadata: slug: remove-selected-ai-extension - version: "1.0.3" + version: "1.0.4" availability: ">=9.5.0" name: "Remove Selected AI Extension" description: >- @@ -340,7 +340,7 @@ steps: {%- if steps.set_context.output.os_type == 'windows' -%} powershell.exe -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; $homePath=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ home | base64_encode }}')); if([string]::IsNullOrWhiteSpace($homePath)){exit 42}; if($homePath -match '^[A-Za-z]:/'){ $homePath=$homePath.Substring(0,1)+':\'+$homePath.Substring(3).Replace('/','\') }; if($homePath -notmatch '^[A-Za-z]:\\' -or $homePath.Contains('..') -or $homePath.Contains('*') -or $homePath.Contains('?') -or $homePath.Contains(';') -or $homePath.Contains('|') -or $homePath.Contains('&')){exit 42}; $eid=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ eid | base64_encode }}')); if($eid -notmatch '^[A-Za-z0-9][A-Za-z0-9_.-]*\.[A-Za-z0-9][A-Za-z0-9_.-]*$'){exit 42}; $prof=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('{{ prof | base64_encode }}')); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ if($prof -notmatch '^[A-Za-z0-9_. -]{1,128}$'){exit 42} }; $roaming=Join-Path $homePath 'AppData\Roaming'; {% if ed == 'cursor' %}$extDir=Join-Path $homePath '.cursor\extensions'; $userData=Join-Path $roaming 'Cursor'; $bins=@((Join-Path ${env:ProgramFiles} 'Cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles} 'cursor\resources\app\bin\cursor.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Cursor\resources\app\bin\cursor.cmd')){% else %}$extDir=Join-Path $homePath '.vscode\extensions'; $userData=Join-Path $roaming 'Code'; $bins=@((Join-Path ${env:ProgramFiles} 'Microsoft VS Code\bin\code.cmd'),(Join-Path ${env:ProgramFiles(x86)} 'Microsoft VS Code\bin\code.cmd')){% endif %}; $bin=$bins | Where-Object { Test-Path $_ } | Select-Object -First 1; if(-not $bin){exit 47}; $args=@('--extensions-dir',$extDir,'--user-data-dir',$userData,'--uninstall-extension',$eid,'--force'); if($prof -and $prof.ToLowerInvariant() -ne 'default'){ $args+='--profile'; $args+=$prof }; & $bin @args; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; Write-Output ('removed:'+$eid)" {%- else -%} - home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ]; then gid=$(stat -f %g "$d" 2>/dev/null)||return 1; [ "$gid" = 80 ]||return 1; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ]; then gid=$(stat -f %g "$d" 2>/dev/null)||return 1; [ "$gid" = 80 ]||return 1; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" + home='{{ home | replace: "'", "'\\''" }}'; case "$home" in /*) ;; *) exit 42 ;; esac; case "$home" in *..*|*\;*|*\|*|*\&*|*\`*|*\$*|*\**) exit 42 ;; esac; eid='{{ eid | replace: "'", "'\\''" }}'; case "$eid" in *[!A-Za-z0-9_.-]*) exit 42 ;; esac; case "$eid" in *.*) ;; *) exit 42 ;; esac; prof='{{ prof | replace: "'", "'\\''" }}'; case "$prof" in *[!A-Za-z0-9_.\ -]*) exit 42 ;; esac; prof_lc=$(printf '%s' "$prof" | tr '[:upper:]' '[:lower:]'); {% if ed == 'cursor' %}ext_dir="$home/.cursor/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Cursor"; else user_data="$home/.config/Cursor"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ] && [ $((g&2)) -ne 0 ]; then gid=$(stat -f %g "$d" 2>/dev/null)||return 1; [ "$gid" = 80 ]||return 1; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/cursor /usr/bin/cursor "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% else %}ext_dir="$home/.vscode/extensions"; if [ "$(uname -s)" = Darwin ]; then user_data="$home/Library/Application Support/Code"; else user_data="$home/.config/Code"; fi; trust_bin(){ c="$1"; [ -x "$c" ]||return 1; t="$c"; n=0; while [ -L "$t" ]&&[ "$n" -lt 32 ]; do l=$(readlink "$t" 2>/dev/null)||return 1; case "$l" in /*) t="$l";; *) t="$(dirname "$t")/$l";; esac; n=$((n+1)); done; [ -e "$t" ]||return 1; if command -v realpath >/dev/null 2>&1; then t=$(realpath "$t" 2>/dev/null)||return 1; fi; d="$t"; while :; do if [ "$(uname -s)" = Darwin ]; then uid=$(stat -f %u "$d" 2>/dev/null)||return 1; mode=$(stat -f %Lp "$d" 2>/dev/null)||return 1; else uid=$(stat -c %u "$d" 2>/dev/null)||return 1; mode=$(stat -c %a "$d" 2>/dev/null)||return 1; fi; [ "$uid" = 0 ]||return 1; o=$((mode%10)); g=$(((mode/10)%10)); [ $((o&2)) -eq 0 ]||return 1; if [ "$(uname -s)" = Darwin ] && [ "$d" = /Applications ] && [ $((g&2)) -ne 0 ]; then gid=$(stat -f %g "$d" 2>/dev/null)||return 1; [ "$gid" = 80 ]||return 1; else [ $((g&2)) -eq 0 ]||return 1; fi; [ "$d" = / ]&&break; nd=$(dirname "$d"); [ "$nd" = "$d" ]&&break; d="$nd"; done; printf %s "$t"; return 0; }; bin=""; for c in /usr/local/bin/code /usr/bin/code "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"; do r=$(trust_bin "$c")||continue; bin="$r"; break; done{% endif %}; [ -n "$bin" ] || exit 47; if [ -n "$prof" ] && [ "$prof_lc" != "default" ]; then "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force --profile "$prof"; else "$bin" --extensions-dir "$ext_dir" --user-data-dir "$user_data" --uninstall-extension "$eid" --force; fi; ec=$?; [ $ec -eq 0 ] || exit $ec; printf 'removed:%s\n' "$eid" {%- endif -%} - name: execute_remove type: kibana.request