talkops-opscloud 0.3.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opscloud/__init__.py +5 -0
- opscloud/__main__.py +6 -0
- opscloud/_constants.py +53 -0
- opscloud/_debug.py +145 -0
- opscloud/_debug_buffer.py +250 -0
- opscloud/_textual_patches.py +367 -0
- opscloud/_version.py +15 -0
- opscloud/agent/__init__.py +27 -0
- opscloud/agent/config.py +168 -0
- opscloud/agent/factory.py +1103 -0
- opscloud/approval_mode.py +44 -0
- opscloud/backend/__init__.py +49 -0
- opscloud/backend/composite.py +89 -0
- opscloud/backend/local.py +183 -0
- opscloud/backend/registry.py +79 -0
- opscloud/backend/sandbox_config.py +107 -0
- opscloud/backend/sandbox_factory.py +432 -0
- opscloud/built_in_skills/remember/SKILL.md +124 -0
- opscloud/built_in_skills/skill-creator/SKILL.md +348 -0
- opscloud/built_in_skills/skill-creator/scripts/init_skill.py +371 -0
- opscloud/built_in_skills/skill-creator/scripts/quick_validate.py +165 -0
- opscloud/cli/__init__.py +1 -0
- opscloud/cli/commands/__init__.py +15 -0
- opscloud/cli/commands/auth.py +45 -0
- opscloud/cli/commands/doctor.py +100 -0
- opscloud/cli/commands/mcp.py +137 -0
- opscloud/cli/commands/threads.py +51 -0
- opscloud/cli/interactive.py +154 -0
- opscloud/cli/main.py +903 -0
- opscloud/cli/non_interactive.py +425 -0
- opscloud/cli/server_manager.py +58 -0
- opscloud/commands/__init__.py +19 -0
- opscloud/commands/_base.py +110 -0
- opscloud/commands/_guards.py +52 -0
- opscloud/commands/_router.py +146 -0
- opscloud/commands/_types.py +36 -0
- opscloud/commands/core/__init__.py +47 -0
- opscloud/commands/core/auth.py +94 -0
- opscloud/commands/core/bug.py +46 -0
- opscloud/commands/core/clear.py +106 -0
- opscloud/commands/core/cloud.py +185 -0
- opscloud/commands/core/compact.py +128 -0
- opscloud/commands/core/config_cmd.py +120 -0
- opscloud/commands/core/context.py +220 -0
- opscloud/commands/core/cost.py +44 -0
- opscloud/commands/core/doctor.py +535 -0
- opscloud/commands/core/effort.py +108 -0
- opscloud/commands/core/exit_cmd.py +41 -0
- opscloud/commands/core/fast.py +148 -0
- opscloud/commands/core/help_cmd.py +94 -0
- opscloud/commands/core/mcp.py +134 -0
- opscloud/commands/core/model.py +109 -0
- opscloud/commands/core/permissions.py +196 -0
- opscloud/commands/core/plugins.py +92 -0
- opscloud/commands/core/pool.py +108 -0
- opscloud/commands/core/resume.py +97 -0
- opscloud/commands/core/skills.py +178 -0
- opscloud/commands/power/__init__.py +51 -0
- opscloud/commands/power/agents.py +98 -0
- opscloud/commands/power/btw.py +85 -0
- opscloud/commands/power/copy.py +97 -0
- opscloud/commands/power/goal.py +548 -0
- opscloud/commands/power/loop.py +295 -0
- opscloud/commands/power/memory.py +287 -0
- opscloud/commands/power/review.py +152 -0
- opscloud/commands/power/rubric.py +287 -0
- opscloud/commands/power/runtime.py +321 -0
- opscloud/commands/power/skill_creator.py +70 -0
- opscloud/commands/power/skill_invoke.py +165 -0
- opscloud/commands/power/tasks.py +209 -0
- opscloud/commands/power/trace.py +117 -0
- opscloud/commands/power/ui_toggles.py +135 -0
- opscloud/commands/power/version.py +52 -0
- opscloud/commands/registry.py +176 -0
- opscloud/config/__init__.py +114 -0
- opscloud/config/adapters/__init__.py +5 -0
- opscloud/config/adapters/postgres.py +776 -0
- opscloud/config/adapters/sqlite.py +769 -0
- opscloud/config/aws.py +248 -0
- opscloud/config/cloud_profiles.py +437 -0
- opscloud/config/env_vars.py +81 -0
- opscloud/config/langsmith.py +319 -0
- opscloud/config/manifest.py +817 -0
- opscloud/config/metadata.py +205 -0
- opscloud/config/paths.py +604 -0
- opscloud/config/plugins.py +130 -0
- opscloud/config/settings.py +834 -0
- opscloud/config/store.py +453 -0
- opscloud/config/store_factory.py +97 -0
- opscloud/config/toml_config.py +562 -0
- opscloud/editor.py +173 -0
- opscloud/exceptions.py +65 -0
- opscloud/file_ops.py +217 -0
- opscloud/hooks/__init__.py +139 -0
- opscloud/hooks/env.py +44 -0
- opscloud/hooks/interrupt.py +121 -0
- opscloud/hooks/models/__init__.py +109 -0
- opscloud/hooks/models/adapters.py +19 -0
- opscloud/hooks/models/config.py +79 -0
- opscloud/hooks/models/domain.py +434 -0
- opscloud/hooks/models/transport.py +37 -0
- opscloud/hooks/tools.py +231 -0
- opscloud/input.py +48 -0
- opscloud/integrations/__init__.py +80 -0
- opscloud/integrations/base.py +153 -0
- opscloud/integrations/content_filter.py +119 -0
- opscloud/integrations/event_bus.py +480 -0
- opscloud/integrations/hooks.py +238 -0
- opscloud/integrations/identity.py +181 -0
- opscloud/integrations/notifications.py +134 -0
- opscloud/integrations/stream_bridge.py +397 -0
- opscloud/integrations/thread_store.py +81 -0
- opscloud/json_types.py +20 -0
- opscloud/mcp/__init__.py +57 -0
- opscloud/mcp/config.py +203 -0
- opscloud/mcp/discovery.py +229 -0
- opscloud/mcp/mcp_info.py +109 -0
- opscloud/mcp/middleware.py +102 -0
- opscloud/mcp/preload.py +505 -0
- opscloud/mcp/raw_config.py +149 -0
- opscloud/mcp/semantic_profiler.py +305 -0
- opscloud/mcp/session_manager.py +727 -0
- opscloud/mcp/trust.py +105 -0
- opscloud/memory/__init__.py +27 -0
- opscloud/memory/branch.py +61 -0
- opscloud/memory/guard.py +19 -0
- opscloud/memory/onboarding.py +120 -0
- opscloud/memory/registry.py +134 -0
- opscloud/memory/store.py +223 -0
- opscloud/middleware/__init__.py +106 -0
- opscloud/middleware/_repository_bounds.py +459 -0
- opscloud/middleware/ask_user.py +152 -0
- opscloud/middleware/auto_mode.py +60 -0
- opscloud/middleware/auto_mode_hitl.py +2434 -0
- opscloud/middleware/compaction.py +566 -0
- opscloud/middleware/configurable_model.py +233 -0
- opscloud/middleware/cost_tracking.py +1021 -0
- opscloud/middleware/glm_stall_recovery.py +109 -0
- opscloud/middleware/goal_criteria.py +1234 -0
- opscloud/middleware/goal_state_notice.py +712 -0
- opscloud/middleware/goal_tools.py +407 -0
- opscloud/middleware/headless_mcp_guard.py +88 -0
- opscloud/middleware/jev_model_router.py +1018 -0
- opscloud/middleware/local_context.py +460 -0
- opscloud/middleware/mcp_context.py +151 -0
- opscloud/middleware/mcp_middleware.py +146 -0
- opscloud/middleware/memory_guard.py +393 -0
- opscloud/middleware/model_retry.py +205 -0
- opscloud/middleware/registry.py +137 -0
- opscloud/middleware/reliable_rubric.py +709 -0
- opscloud/middleware/resume_state.py +91 -0
- opscloud/middleware/server_hooks.py +1189 -0
- opscloud/middleware/shell_allow_list.py +90 -0
- opscloud/middleware/skills.py +382 -0
- opscloud/middleware/subagent_artifacts.py +84 -0
- opscloud/middleware/subagent_telemetry.py +261 -0
- opscloud/middleware/subagents.py +243 -0
- opscloud/middleware/tool_filter.py +384 -0
- opscloud/middleware/unified_system_message.py +62 -0
- opscloud/model/__init__.py +37 -0
- opscloud/model/config.py +2992 -0
- opscloud/model/factory.py +477 -0
- opscloud/model/pool.py +459 -0
- opscloud/model/reasoning.py +282 -0
- opscloud/offload.py +173 -0
- opscloud/output.py +64 -0
- opscloud/plugins/__init__.py +189 -0
- opscloud/plugins/_json.py +22 -0
- opscloud/plugins/adapters/__init__.py +13 -0
- opscloud/plugins/adapters/agents.py +112 -0
- opscloud/plugins/adapters/commands.py +190 -0
- opscloud/plugins/adapters/mcp.py +363 -0
- opscloud/plugins/adapters/skills.py +132 -0
- opscloud/plugins/commands_cli.py +541 -0
- opscloud/plugins/discovery.py +512 -0
- opscloud/plugins/manifest.py +335 -0
- opscloud/plugins/marketplace.py +893 -0
- opscloud/plugins/models.py +334 -0
- opscloud/plugins/project_plugins.py +317 -0
- opscloud/plugins/protocol.py +31 -0
- opscloud/plugins/store.py +905 -0
- opscloud/plugins/substitution.py +122 -0
- opscloud/project_utils.py +145 -0
- opscloud/prompts/__init__.py +37 -0
- opscloud/prompts/system.py +483 -0
- opscloud/prompts/templates/system_prompt.md +166 -0
- opscloud/prompts/types.py +54 -0
- opscloud/rubrics/__init__.py +42 -0
- opscloud/rubrics/evaluator.py +404 -0
- opscloud/rubrics/evidence_extractor.py +229 -0
- opscloud/rubrics/generator.py +239 -0
- opscloud/rubrics/jev_compiler.py +175 -0
- opscloud/rubrics/jev_grader.py +297 -0
- opscloud/rubrics/middleware.py +8 -0
- opscloud/schema/__init__.py +107 -0
- opscloud/schema/interrupts.py +981 -0
- opscloud/security/__init__.py +91 -0
- opscloud/security/approval_mode.py +528 -0
- opscloud/security/approval_mode_source.py +230 -0
- opscloud/security/cli_ast_evaluator.py +552 -0
- opscloud/security/jev_classifier.py +651 -0
- opscloud/security/shell_safety.py +340 -0
- opscloud/security/unicode_security.py +439 -0
- opscloud/security/url_validation.py +160 -0
- opscloud/server/__init__.py +13 -0
- opscloud/server/_server_config.py +158 -0
- opscloud/server/server.py +242 -0
- opscloud/server/server_graph.py +93 -0
- opscloud/skills/__init__.py +91 -0
- opscloud/skills/commands.py +1099 -0
- opscloud/skills/invocation.py +200 -0
- opscloud/skills/load.py +388 -0
- opscloud/skills/loader.py +23 -0
- opscloud/skills/merge.py +65 -0
- opscloud/skills/registry.py +263 -0
- opscloud/skills/sources.py +174 -0
- opscloud/skills/trust.py +417 -0
- opscloud/state/__init__.py +142 -0
- opscloud/state/base.py +154 -0
- opscloud/state/cloud_context.py +262 -0
- opscloud/state/goal_channels.py +124 -0
- opscloud/state/goal_state_limits.py +175 -0
- opscloud/state/resume_state.py +58 -0
- opscloud/state/service_context.py +281 -0
- opscloud/state/session.py +957 -0
- opscloud/state/state_migration.py +86 -0
- opscloud/subagents/__init__.py +30 -0
- opscloud/subagents/loader.py +331 -0
- opscloud/subagents/subagents_parser.py +117 -0
- opscloud/subagents/types.py +25 -0
- opscloud/tools/__init__.py +36 -0
- opscloud/tools/catalog.py +37 -0
- opscloud/tools/display.py +24 -0
- opscloud/tools/fetch_url.py +176 -0
- opscloud/tools/goal_tools.py +423 -0
- opscloud/tools/registry.py +105 -0
- opscloud/tools/thread.py +32 -0
- opscloud/tools/web_search.py +215 -0
- opscloud/ui/__init__.py +119 -0
- opscloud/ui/app.py +5400 -0
- opscloud/ui/app.tcss +479 -0
- opscloud/ui/clipboard.py +147 -0
- opscloud/ui/command_registry.py +482 -0
- opscloud/ui/message_store.py +152 -0
- opscloud/ui/permission_store.py +360 -0
- opscloud/ui/preferences.py +113 -0
- opscloud/ui/prompt_manager.py +96 -0
- opscloud/ui/remote_client.py +505 -0
- opscloud/ui/textual_adapter.py +1269 -0
- opscloud/ui/theme.py +728 -0
- opscloud/ui/ui_help.py +415 -0
- opscloud/ui/widgets/__init__.py +383 -0
- opscloud/ui/widgets/_ask_user_types.py +81 -0
- opscloud/ui/widgets/_copy_spans.py +52 -0
- opscloud/ui/widgets/_inline_prompt.py +290 -0
- opscloud/ui/widgets/_js_eval_display.py +139 -0
- opscloud/ui/widgets/_links.py +175 -0
- opscloud/ui/widgets/_tool_stream.py +308 -0
- opscloud/ui/widgets/agent_selector.py +297 -0
- opscloud/ui/widgets/approval.py +741 -0
- opscloud/ui/widgets/ask_user.py +539 -0
- opscloud/ui/widgets/auth.py +695 -0
- opscloud/ui/widgets/auth_manager.py +214 -0
- opscloud/ui/widgets/auto_mode_notice.py +146 -0
- opscloud/ui/widgets/autocomplete.py +709 -0
- opscloud/ui/widgets/chat_input.py +805 -0
- opscloud/ui/widgets/cloud_selector.py +528 -0
- opscloud/ui/widgets/config_manager.py +652 -0
- opscloud/ui/widgets/debug_console.py +1269 -0
- opscloud/ui/widgets/devops_renderers.py +184 -0
- opscloud/ui/widgets/diff.py +237 -0
- opscloud/ui/widgets/effort_selector.py +166 -0
- opscloud/ui/widgets/goal_review.py +612 -0
- opscloud/ui/widgets/goal_status.py +57 -0
- opscloud/ui/widgets/infra_panel.py +141 -0
- opscloud/ui/widgets/install_confirm.py +114 -0
- opscloud/ui/widgets/loading.py +170 -0
- opscloud/ui/widgets/mcp_viewer.py +1204 -0
- opscloud/ui/widgets/messages.py +4461 -0
- opscloud/ui/widgets/model_selector.py +863 -0
- opscloud/ui/widgets/notification_center.py +108 -0
- opscloud/ui/widgets/notification_settings.py +119 -0
- opscloud/ui/widgets/operation_card.py +106 -0
- opscloud/ui/widgets/permissions_manager.py +667 -0
- opscloud/ui/widgets/plugin_manager.py +1212 -0
- opscloud/ui/widgets/pool_selector.py +376 -0
- opscloud/ui/widgets/preamble.py +53 -0
- opscloud/ui/widgets/skills_viewer.py +432 -0
- opscloud/ui/widgets/status.py +1037 -0
- opscloud/ui/widgets/subagent_panel.py +927 -0
- opscloud/ui/widgets/theme_selector.py +232 -0
- opscloud/ui/widgets/thread_selector.py +358 -0
- opscloud/ui/widgets/toast.py +31 -0
- opscloud/ui/widgets/tool_display.py +325 -0
- opscloud/ui/widgets/tool_renderers.py +234 -0
- opscloud/ui/widgets/tool_widgets.py +278 -0
- opscloud/ui/widgets/welcome.py +421 -0
- opscloud/ui/widgets/welcome_popup.py +102 -0
- opscloud/utils/__init__.py +24 -0
- opscloud/utils/cost_estimation.py +169 -0
- opscloud/utils/git.py +390 -0
- opscloud/utils/logger.py +571 -0
- opscloud/utils/session_stats.py +115 -0
- opscloud/utils/startup_error.py +37 -0
- talkops_opscloud-0.3.0b1.dist-info/METADATA +449 -0
- talkops_opscloud-0.3.0b1.dist-info/RECORD +309 -0
- talkops_opscloud-0.3.0b1.dist-info/WHEEL +4 -0
- talkops_opscloud-0.3.0b1.dist-info/entry_points.txt +2 -0
- talkops_opscloud-0.3.0b1.dist-info/licenses/LICENSE +175 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
"""CLI AST Safety Evaluator — deterministic safety classification for shell commands.
|
|
2
|
+
|
|
3
|
+
Parses composite bash commands, subshells, pipelines, and process substitutions using
|
|
4
|
+
`bashlex` AST traversal to detect dangerous verbs and flags in sub-millisecond time.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import re
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
import bashlex
|
|
13
|
+
import bashlex.ast
|
|
14
|
+
|
|
15
|
+
from opscloud.utils.logger import get_logger
|
|
16
|
+
|
|
17
|
+
logger = get_logger(__name__)
|
|
18
|
+
|
|
19
|
+
# Wrapper utilities that execute other commands
|
|
20
|
+
WRAPPER_UTILITIES: frozenset[str] = frozenset(
|
|
21
|
+
{
|
|
22
|
+
"xargs",
|
|
23
|
+
"sudo",
|
|
24
|
+
"doas",
|
|
25
|
+
"time",
|
|
26
|
+
"nohup",
|
|
27
|
+
"env",
|
|
28
|
+
"parallel",
|
|
29
|
+
"chroot",
|
|
30
|
+
"exec",
|
|
31
|
+
"sh",
|
|
32
|
+
"bash",
|
|
33
|
+
"zsh",
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# Pure read-only utilities whose execution only inspects local data/state
|
|
38
|
+
READONLY_UTILITIES: frozenset[str] = frozenset(
|
|
39
|
+
{
|
|
40
|
+
"cat",
|
|
41
|
+
"grep",
|
|
42
|
+
"egrep",
|
|
43
|
+
"fgrep",
|
|
44
|
+
"rg",
|
|
45
|
+
"head",
|
|
46
|
+
"tail",
|
|
47
|
+
"ls",
|
|
48
|
+
"find",
|
|
49
|
+
"echo",
|
|
50
|
+
"pwd",
|
|
51
|
+
"awk",
|
|
52
|
+
"sed",
|
|
53
|
+
"jq",
|
|
54
|
+
"yq",
|
|
55
|
+
"wc",
|
|
56
|
+
"diff",
|
|
57
|
+
"stat",
|
|
58
|
+
"file",
|
|
59
|
+
"uname",
|
|
60
|
+
"which",
|
|
61
|
+
"whereis",
|
|
62
|
+
"whoami",
|
|
63
|
+
"printenv",
|
|
64
|
+
"date",
|
|
65
|
+
"uptime",
|
|
66
|
+
"df",
|
|
67
|
+
"du",
|
|
68
|
+
"free",
|
|
69
|
+
"ps",
|
|
70
|
+
"top",
|
|
71
|
+
"tree",
|
|
72
|
+
"sort",
|
|
73
|
+
"uniq",
|
|
74
|
+
"tr",
|
|
75
|
+
"cut",
|
|
76
|
+
"less",
|
|
77
|
+
"more",
|
|
78
|
+
"curl",
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
# Destructive operations that delete or disrupt live cloud state, disk, or databases
|
|
83
|
+
DANGEROUS_VERBS: frozenset[str] = frozenset(
|
|
84
|
+
{
|
|
85
|
+
"delete",
|
|
86
|
+
"terminate",
|
|
87
|
+
"terminate-instances",
|
|
88
|
+
"delete-db-instance",
|
|
89
|
+
"delete-db-cluster",
|
|
90
|
+
"delete-cluster",
|
|
91
|
+
"delete-nodegroup",
|
|
92
|
+
"rb",
|
|
93
|
+
"remove",
|
|
94
|
+
"rm",
|
|
95
|
+
"drain",
|
|
96
|
+
"evict",
|
|
97
|
+
"taint",
|
|
98
|
+
"drop",
|
|
99
|
+
"uninstall",
|
|
100
|
+
"zap",
|
|
101
|
+
"destroy",
|
|
102
|
+
"prune",
|
|
103
|
+
"truncate",
|
|
104
|
+
"kill",
|
|
105
|
+
"purge",
|
|
106
|
+
"wipe",
|
|
107
|
+
"format",
|
|
108
|
+
"cordon",
|
|
109
|
+
"uncordon",
|
|
110
|
+
"shutdown",
|
|
111
|
+
"reboot",
|
|
112
|
+
}
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
# Destructive force-override CLI flags
|
|
116
|
+
DANGEROUS_FLAGS: frozenset[str] = frozenset(
|
|
117
|
+
{
|
|
118
|
+
"--force",
|
|
119
|
+
"--purge",
|
|
120
|
+
"--grace-period=0",
|
|
121
|
+
"-rf",
|
|
122
|
+
"-fr",
|
|
123
|
+
"--all",
|
|
124
|
+
"--no-preserve-root",
|
|
125
|
+
"--cascade=foreground",
|
|
126
|
+
"--cascade=orphan",
|
|
127
|
+
"--hard",
|
|
128
|
+
"-D",
|
|
129
|
+
"--recursive",
|
|
130
|
+
}
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
# Standard AWS/DevOps/Git read-only verbs
|
|
134
|
+
READONLY_VERBS: frozenset[str] = frozenset(
|
|
135
|
+
{
|
|
136
|
+
"get",
|
|
137
|
+
"list",
|
|
138
|
+
"describe",
|
|
139
|
+
"status",
|
|
140
|
+
"logs",
|
|
141
|
+
"log",
|
|
142
|
+
"version",
|
|
143
|
+
"show",
|
|
144
|
+
"view",
|
|
145
|
+
"explain",
|
|
146
|
+
"top",
|
|
147
|
+
"diff",
|
|
148
|
+
"cluster-info",
|
|
149
|
+
"auth",
|
|
150
|
+
"can-i",
|
|
151
|
+
"api-resources",
|
|
152
|
+
"api-versions",
|
|
153
|
+
"branch",
|
|
154
|
+
"remote",
|
|
155
|
+
"config",
|
|
156
|
+
"plan",
|
|
157
|
+
"validate",
|
|
158
|
+
"fmt",
|
|
159
|
+
"get-caller-identity",
|
|
160
|
+
}
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
# Standard mutating verbs across cloud providers and CLI tools
|
|
165
|
+
MUTATING_VERBS: frozenset[str] = frozenset(
|
|
166
|
+
{
|
|
167
|
+
"create",
|
|
168
|
+
"delete",
|
|
169
|
+
"remove",
|
|
170
|
+
"destroy",
|
|
171
|
+
"terminate",
|
|
172
|
+
"kill",
|
|
173
|
+
"purge",
|
|
174
|
+
"drop",
|
|
175
|
+
"uninstall",
|
|
176
|
+
"update",
|
|
177
|
+
"set",
|
|
178
|
+
"start",
|
|
179
|
+
"stop",
|
|
180
|
+
"restart",
|
|
181
|
+
"apply",
|
|
182
|
+
"attach",
|
|
183
|
+
"detach",
|
|
184
|
+
"install",
|
|
185
|
+
"run",
|
|
186
|
+
"execute",
|
|
187
|
+
"deploy",
|
|
188
|
+
"scale",
|
|
189
|
+
"patch",
|
|
190
|
+
"edit",
|
|
191
|
+
"import",
|
|
192
|
+
"push",
|
|
193
|
+
"commit",
|
|
194
|
+
"merge",
|
|
195
|
+
"rebase",
|
|
196
|
+
}
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
class SecurityASTVisitor(bashlex.ast.nodevisitor):
|
|
201
|
+
"""Traverses bashlex AST nodes inspecting words, commands, and substitutions."""
|
|
202
|
+
|
|
203
|
+
def __init__(self) -> None:
|
|
204
|
+
self.found_verbs: list[str] = []
|
|
205
|
+
self.found_flags: list[str] = []
|
|
206
|
+
self.found_commands: list[str] = []
|
|
207
|
+
self.is_destructive: bool = False
|
|
208
|
+
self.is_readonly: bool = True
|
|
209
|
+
|
|
210
|
+
def visitcommand(self, n: Any, parts: list[Any]) -> None:
|
|
211
|
+
words: list[str] = []
|
|
212
|
+
for part in parts:
|
|
213
|
+
if getattr(part, "kind", None) == "word":
|
|
214
|
+
w = getattr(part, "word", "")
|
|
215
|
+
if w:
|
|
216
|
+
words.append(w)
|
|
217
|
+
|
|
218
|
+
if not words:
|
|
219
|
+
return
|
|
220
|
+
|
|
221
|
+
cmd_utility = words[0].lower()
|
|
222
|
+
self.found_commands.append(cmd_utility)
|
|
223
|
+
|
|
224
|
+
# 1. Pure inspection utilities (cat, grep, ls, jq, etc.)
|
|
225
|
+
if cmd_utility in READONLY_UTILITIES:
|
|
226
|
+
if cmd_utility == "rm":
|
|
227
|
+
self.is_destructive = True
|
|
228
|
+
self.is_readonly = False
|
|
229
|
+
return
|
|
230
|
+
|
|
231
|
+
# 2. Wrapper utilities (xargs, sudo, time, etc.) - inspect all words
|
|
232
|
+
if cmd_utility in WRAPPER_UTILITIES:
|
|
233
|
+
self.is_readonly = False
|
|
234
|
+
for word in words[1:]:
|
|
235
|
+
w_lower = word.lower()
|
|
236
|
+
if word.startswith("-"):
|
|
237
|
+
self.found_flags.append(word)
|
|
238
|
+
if word in DANGEROUS_FLAGS or w_lower in DANGEROUS_FLAGS:
|
|
239
|
+
self.is_destructive = True
|
|
240
|
+
else:
|
|
241
|
+
self.found_verbs.append(w_lower)
|
|
242
|
+
if w_lower in DANGEROUS_VERBS:
|
|
243
|
+
self.is_destructive = True
|
|
244
|
+
return
|
|
245
|
+
|
|
246
|
+
# 3. AWS CLI special handling (aws <service> <verb>)
|
|
247
|
+
if cmd_utility == "aws":
|
|
248
|
+
flags: list[str] = []
|
|
249
|
+
non_flag_args: list[str] = []
|
|
250
|
+
for word in words[1:]:
|
|
251
|
+
w_lower = word.lower()
|
|
252
|
+
if word.startswith("-"):
|
|
253
|
+
flags.append(word)
|
|
254
|
+
if word in DANGEROUS_FLAGS or w_lower in DANGEROUS_FLAGS:
|
|
255
|
+
self.is_destructive = True
|
|
256
|
+
else:
|
|
257
|
+
non_flag_args.append(w_lower)
|
|
258
|
+
self.found_flags.extend(flags)
|
|
259
|
+
if len(non_flag_args) >= 2:
|
|
260
|
+
# e.g. aws ec2 terminate-instances
|
|
261
|
+
service = non_flag_args[0]
|
|
262
|
+
verb = non_flag_args[1]
|
|
263
|
+
self.found_verbs.append(verb)
|
|
264
|
+
if (
|
|
265
|
+
verb in DANGEROUS_VERBS
|
|
266
|
+
or verb.startswith(("terminate-", "delete-", "stop-", "disassociate-", "deregister-"))
|
|
267
|
+
or (service == "s3" and verb in ("rb",))
|
|
268
|
+
):
|
|
269
|
+
self.is_destructive = True
|
|
270
|
+
self.is_readonly = False
|
|
271
|
+
elif (
|
|
272
|
+
verb in READONLY_VERBS
|
|
273
|
+
or verb.startswith(("describe-", "list-", "get-"))
|
|
274
|
+
or (service == "s3" and verb in ("ls",))
|
|
275
|
+
or (service == "sts" and verb in ("get-caller-identity",))
|
|
276
|
+
):
|
|
277
|
+
pass
|
|
278
|
+
else:
|
|
279
|
+
self.is_readonly = False
|
|
280
|
+
return
|
|
281
|
+
|
|
282
|
+
# 4. Azure CLI special handling (az <service> [subgroup] <action>)
|
|
283
|
+
if cmd_utility == "az":
|
|
284
|
+
flags = []
|
|
285
|
+
non_flag_args = []
|
|
286
|
+
skip_next = False
|
|
287
|
+
for word in words[1:]:
|
|
288
|
+
if skip_next:
|
|
289
|
+
skip_next = False
|
|
290
|
+
continue
|
|
291
|
+
w_lower = word.lower()
|
|
292
|
+
if word.startswith("-"):
|
|
293
|
+
flags.append(word)
|
|
294
|
+
if word in DANGEROUS_FLAGS or w_lower in DANGEROUS_FLAGS:
|
|
295
|
+
self.is_destructive = True
|
|
296
|
+
if word in ("-o", "--output", "-g", "--resource-group", "-n", "--name", "-s", "--subscription", "--query", "-f", "--file"):
|
|
297
|
+
skip_next = True
|
|
298
|
+
else:
|
|
299
|
+
non_flag_args.append(w_lower)
|
|
300
|
+
self.found_flags.extend(flags)
|
|
301
|
+
if non_flag_args:
|
|
302
|
+
for verb in non_flag_args:
|
|
303
|
+
self.found_verbs.append(verb)
|
|
304
|
+
if verb in DANGEROUS_VERBS or verb.startswith(("delete", "purge", "destroy", "terminate")):
|
|
305
|
+
self.is_destructive = True
|
|
306
|
+
self.is_readonly = False
|
|
307
|
+
return
|
|
308
|
+
if any(v in DANGEROUS_VERBS for v in non_flag_args):
|
|
309
|
+
self.is_destructive = True
|
|
310
|
+
self.is_readonly = False
|
|
311
|
+
elif any(v in MUTATING_VERBS for v in non_flag_args):
|
|
312
|
+
self.is_readonly = False
|
|
313
|
+
elif any(v in READONLY_VERBS or v.startswith(("show", "list", "get")) for v in non_flag_args):
|
|
314
|
+
pass
|
|
315
|
+
else:
|
|
316
|
+
self.is_readonly = False
|
|
317
|
+
return
|
|
318
|
+
|
|
319
|
+
# 5. Google Cloud CLI special handling (gcloud <group> [subgroup] <action>)
|
|
320
|
+
if cmd_utility == "gcloud":
|
|
321
|
+
flags = []
|
|
322
|
+
non_flag_args = []
|
|
323
|
+
skip_next = False
|
|
324
|
+
for word in words[1:]:
|
|
325
|
+
if skip_next:
|
|
326
|
+
skip_next = False
|
|
327
|
+
continue
|
|
328
|
+
w_lower = word.lower()
|
|
329
|
+
if word.startswith("-"):
|
|
330
|
+
flags.append(word)
|
|
331
|
+
if word in DANGEROUS_FLAGS or w_lower in DANGEROUS_FLAGS:
|
|
332
|
+
self.is_destructive = True
|
|
333
|
+
if word in ("--format", "--filter", "--project", "--zone", "--region"):
|
|
334
|
+
skip_next = True
|
|
335
|
+
else:
|
|
336
|
+
non_flag_args.append(w_lower)
|
|
337
|
+
self.found_flags.extend(flags)
|
|
338
|
+
if non_flag_args:
|
|
339
|
+
for verb in non_flag_args:
|
|
340
|
+
self.found_verbs.append(verb)
|
|
341
|
+
if verb in DANGEROUS_VERBS or verb.startswith(("delete", "purge", "destroy", "terminate")):
|
|
342
|
+
self.is_destructive = True
|
|
343
|
+
self.is_readonly = False
|
|
344
|
+
return
|
|
345
|
+
if any(v in DANGEROUS_VERBS for v in non_flag_args):
|
|
346
|
+
self.is_destructive = True
|
|
347
|
+
self.is_readonly = False
|
|
348
|
+
elif any(v in MUTATING_VERBS for v in non_flag_args):
|
|
349
|
+
self.is_readonly = False
|
|
350
|
+
elif any(v in READONLY_VERBS or v.startswith(("list", "describe", "get", "info", "version")) for v in non_flag_args):
|
|
351
|
+
pass
|
|
352
|
+
else:
|
|
353
|
+
self.is_readonly = False
|
|
354
|
+
return
|
|
355
|
+
|
|
356
|
+
# 6. Command utilities with sub-commands (terraform, tofu, kubectl, helm, git, docker, etc.)
|
|
357
|
+
flags = []
|
|
358
|
+
non_flag_args = []
|
|
359
|
+
for word in words[1:]:
|
|
360
|
+
w_lower = word.lower()
|
|
361
|
+
if word.startswith("-"):
|
|
362
|
+
flags.append(word)
|
|
363
|
+
if (
|
|
364
|
+
word in DANGEROUS_FLAGS
|
|
365
|
+
or w_lower in DANGEROUS_FLAGS
|
|
366
|
+
or (cmd_utility == "rm" and ("f" in word or "r" in word))
|
|
367
|
+
):
|
|
368
|
+
self.is_destructive = True
|
|
369
|
+
self.is_readonly = False
|
|
370
|
+
else:
|
|
371
|
+
non_flag_args.append(w_lower)
|
|
372
|
+
|
|
373
|
+
self.found_flags.extend(flags)
|
|
374
|
+
if not non_flag_args:
|
|
375
|
+
if cmd_utility == "rm":
|
|
376
|
+
self.is_destructive = True
|
|
377
|
+
self.is_readonly = False
|
|
378
|
+
return
|
|
379
|
+
|
|
380
|
+
for verb in non_flag_args:
|
|
381
|
+
self.found_verbs.append(verb)
|
|
382
|
+
if verb in DANGEROUS_VERBS:
|
|
383
|
+
self.is_destructive = True
|
|
384
|
+
self.is_readonly = False
|
|
385
|
+
return
|
|
386
|
+
|
|
387
|
+
primary_verb = non_flag_args[0]
|
|
388
|
+
if primary_verb in READONLY_VERBS:
|
|
389
|
+
pass
|
|
390
|
+
else:
|
|
391
|
+
self.is_readonly = False
|
|
392
|
+
|
|
393
|
+
def visitprocesssubstitution(self, n: Any, command: Any) -> None:
|
|
394
|
+
self.visit(command)
|
|
395
|
+
|
|
396
|
+
def visitcommandsubstitution(self, n: Any, command: Any) -> None:
|
|
397
|
+
self.visit(command)
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def evaluate_cli_safety(command_string: str) -> dict[str, Any]:
|
|
401
|
+
"""Parse a composite shell command and determine its operational risk tier.
|
|
402
|
+
|
|
403
|
+
Returns:
|
|
404
|
+
dict containing:
|
|
405
|
+
- tier: 1 (Read-Only), 2 (Low-Impact), 3 (Mutating), or 4 (Destructive)
|
|
406
|
+
- is_destructive: bool
|
|
407
|
+
- is_readonly: bool
|
|
408
|
+
- safe: bool
|
|
409
|
+
- reason: str
|
|
410
|
+
"""
|
|
411
|
+
cmd_str = (command_string or "").strip()
|
|
412
|
+
if not cmd_str:
|
|
413
|
+
return {
|
|
414
|
+
"tier": 1,
|
|
415
|
+
"is_destructive": False,
|
|
416
|
+
"is_readonly": True,
|
|
417
|
+
"safe": True,
|
|
418
|
+
"reason": "Empty command string.",
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
try:
|
|
422
|
+
trees = bashlex.parse(cmd_str)
|
|
423
|
+
except Exception as exc:
|
|
424
|
+
# Fallback to regex-based classification on syntax failure
|
|
425
|
+
logger.debug("AST Parsing failed for command %r, falling back to heuristics: %s", cmd_str[:120], exc)
|
|
426
|
+
from opscloud.security.shell_safety import classify_command
|
|
427
|
+
|
|
428
|
+
cls_res = classify_command(cmd_str)
|
|
429
|
+
if cls_res == "dangerous":
|
|
430
|
+
return {
|
|
431
|
+
"tier": 4,
|
|
432
|
+
"is_destructive": True,
|
|
433
|
+
"is_readonly": False,
|
|
434
|
+
"safe": False,
|
|
435
|
+
"is_safe": False,
|
|
436
|
+
"reason": "Dangerous command pattern detected via heuristics.",
|
|
437
|
+
}
|
|
438
|
+
if cls_res == "safe":
|
|
439
|
+
return {
|
|
440
|
+
"tier": 1,
|
|
441
|
+
"is_destructive": False,
|
|
442
|
+
"is_readonly": True,
|
|
443
|
+
"safe": True,
|
|
444
|
+
"is_safe": True,
|
|
445
|
+
"reason": "Read-only command identified via heuristics.",
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
"tier": 3,
|
|
449
|
+
"is_destructive": False,
|
|
450
|
+
"is_readonly": False,
|
|
451
|
+
"safe": False,
|
|
452
|
+
"is_safe": False,
|
|
453
|
+
"reason": "Unrecognized or mutating command syntax.",
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
visitor = SecurityASTVisitor()
|
|
457
|
+
for tree in trees:
|
|
458
|
+
visitor.visit(tree)
|
|
459
|
+
|
|
460
|
+
if visitor.is_destructive:
|
|
461
|
+
matched_verbs = set(visitor.found_verbs) & DANGEROUS_VERBS
|
|
462
|
+
matched_flags = set(visitor.found_flags) & DANGEROUS_FLAGS
|
|
463
|
+
reasons: list[str] = []
|
|
464
|
+
if matched_verbs:
|
|
465
|
+
reasons.append(f"destructive verbs: {sorted(matched_verbs)}")
|
|
466
|
+
if matched_flags:
|
|
467
|
+
reasons.append(f"destructive flags: {sorted(matched_flags)}")
|
|
468
|
+
if not reasons:
|
|
469
|
+
reasons.append("destructive command pattern")
|
|
470
|
+
reason_str = f"Detected {', '.join(reasons)}."
|
|
471
|
+
res = {
|
|
472
|
+
"tier": 4,
|
|
473
|
+
"is_destructive": True,
|
|
474
|
+
"is_readonly": False,
|
|
475
|
+
"safe": False,
|
|
476
|
+
"is_safe": False,
|
|
477
|
+
"reason": reason_str,
|
|
478
|
+
}
|
|
479
|
+
elif visitor.is_readonly:
|
|
480
|
+
res = {
|
|
481
|
+
"tier": 1,
|
|
482
|
+
"is_destructive": False,
|
|
483
|
+
"is_readonly": True,
|
|
484
|
+
"safe": True,
|
|
485
|
+
"is_safe": True,
|
|
486
|
+
"reason": "Command contains only read-only inspection utilities and verbs.",
|
|
487
|
+
}
|
|
488
|
+
else:
|
|
489
|
+
res = {
|
|
490
|
+
"tier": 3,
|
|
491
|
+
"is_destructive": False,
|
|
492
|
+
"is_readonly": False,
|
|
493
|
+
"safe": False,
|
|
494
|
+
"is_safe": False,
|
|
495
|
+
"reason": f"Operational mutation command: verbs {visitor.found_verbs[:3]}.",
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return res
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
from dataclasses import dataclass
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
@dataclass(frozen=True)
|
|
505
|
+
class AstEvaluationResult:
|
|
506
|
+
is_safe: bool
|
|
507
|
+
is_destructive: bool
|
|
508
|
+
is_readonly: bool
|
|
509
|
+
tier: int
|
|
510
|
+
reason: str
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
class CliAstEvaluator:
|
|
514
|
+
"""Evaluates shell commands against allow lists and AST safety criteria."""
|
|
515
|
+
|
|
516
|
+
def evaluate(self, command: str, allow_list: list[str] | None = None) -> AstEvaluationResult:
|
|
517
|
+
if allow_list:
|
|
518
|
+
from opscloud.security.shell_safety import is_shell_command_allowed
|
|
519
|
+
|
|
520
|
+
if not is_shell_command_allowed(command, allow_list):
|
|
521
|
+
return AstEvaluationResult(
|
|
522
|
+
is_safe=False,
|
|
523
|
+
is_destructive=False,
|
|
524
|
+
is_readonly=False,
|
|
525
|
+
tier=3,
|
|
526
|
+
reason="Command not in allow list",
|
|
527
|
+
)
|
|
528
|
+
res = evaluate_cli_safety(command)
|
|
529
|
+
return AstEvaluationResult(
|
|
530
|
+
is_safe=res.get("safe", False),
|
|
531
|
+
is_destructive=res.get("is_destructive", False),
|
|
532
|
+
is_readonly=res.get("is_readonly", False),
|
|
533
|
+
tier=res.get("tier", 3),
|
|
534
|
+
reason=res.get("reason", ""),
|
|
535
|
+
)
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
evaluate_command_safety = evaluate_cli_safety
|
|
539
|
+
|
|
540
|
+
__all__ = [
|
|
541
|
+
"AstEvaluationResult",
|
|
542
|
+
"CliAstEvaluator",
|
|
543
|
+
"DANGEROUS_FLAGS",
|
|
544
|
+
"DANGEROUS_VERBS",
|
|
545
|
+
"READONLY_UTILITIES",
|
|
546
|
+
"READONLY_VERBS",
|
|
547
|
+
"SecurityASTVisitor",
|
|
548
|
+
"WRAPPER_UTILITIES",
|
|
549
|
+
"evaluate_cli_safety",
|
|
550
|
+
"evaluate_command_safety",
|
|
551
|
+
]
|
|
552
|
+
|