damage-control-dev 0.1.0__tar.gz
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.
- damage_control_dev-0.1.0/.gitignore +8 -0
- damage_control_dev-0.1.0/LICENSE +201 -0
- damage_control_dev-0.1.0/NOTICE +4 -0
- damage_control_dev-0.1.0/PKG-INFO +15 -0
- damage_control_dev-0.1.0/pyproject.toml +40 -0
- damage_control_dev-0.1.0/src/damage_control/__init__.py +9 -0
- damage_control_dev-0.1.0/src/damage_control/__main__.py +7 -0
- damage_control_dev-0.1.0/src/damage_control/adapter.py +179 -0
- damage_control_dev-0.1.0/src/damage_control/audit.py +432 -0
- damage_control_dev-0.1.0/src/damage_control/breakglass.py +155 -0
- damage_control_dev-0.1.0/src/damage_control/claude_code.py +148 -0
- damage_control_dev-0.1.0/src/damage_control/cli.py +173 -0
- damage_control_dev-0.1.0/src/damage_control/engine.py +10656 -0
- damage_control_dev-0.1.0/src/damage_control/groups.py +114 -0
- damage_control_dev-0.1.0/src/damage_control/harness_config.py +205 -0
- damage_control_dev-0.1.0/src/damage_control/hook.py +54 -0
- damage_control_dev-0.1.0/src/damage_control/install.py +618 -0
- damage_control_dev-0.1.0/src/damage_control/packs.py +219 -0
- damage_control_dev-0.1.0/src/damage_control/policy.py +323 -0
- damage_control_dev-0.1.0/src/damage_control/profile.py +177 -0
- damage_control_dev-0.1.0/src/damage_control/rules/aws.json +85 -0
- damage_control_dev-0.1.0/src/damage_control/rules/aws.yaml +76 -0
- damage_control_dev-0.1.0/src/damage_control/rules/cloud-hosting.json +162 -0
- damage_control_dev-0.1.0/src/damage_control/rules/cloud-hosting.yaml +163 -0
- damage_control_dev-0.1.0/src/damage_control/rules/containers.json +67 -0
- damage_control_dev-0.1.0/src/damage_control/rules/containers.yaml +69 -0
- damage_control_dev-0.1.0/src/damage_control/rules/control-plane.json +7 -0
- damage_control_dev-0.1.0/src/damage_control/rules/control-plane.yaml +21 -0
- damage_control_dev-0.1.0/src/damage_control/rules/core.json +332 -0
- damage_control_dev-0.1.0/src/damage_control/rules/core.yaml +441 -0
- damage_control_dev-0.1.0/src/damage_control/rules/databases.json +117 -0
- damage_control_dev-0.1.0/src/damage_control/rules/databases.yaml +123 -0
- damage_control_dev-0.1.0/src/damage_control/rules/firebase.json +34 -0
- damage_control_dev-0.1.0/src/damage_control/rules/firebase.yaml +30 -0
- damage_control_dev-0.1.0/src/damage_control/rules/gcp.json +60 -0
- damage_control_dev-0.1.0/src/damage_control/rules/gcp.yaml +57 -0
- damage_control_dev-0.1.0/src/damage_control/rules/git.json +119 -0
- damage_control_dev-0.1.0/src/damage_control/rules/git.yaml +120 -0
- damage_control_dev-0.1.0/src/damage_control/rules/infrastructure.json +69 -0
- damage_control_dev-0.1.0/src/damage_control/rules/infrastructure.yaml +66 -0
- damage_control_dev-0.1.0/src/damage_control/rules/mcp-tools.json +11 -0
- damage_control_dev-0.1.0/src/damage_control/rules/mcp-tools.yaml +47 -0
- damage_control_dev-0.1.0/src/damage_control/rules/outbound.json +40 -0
- damage_control_dev-0.1.0/src/damage_control/rules/outbound.yaml +58 -0
- damage_control_dev-0.1.0/src/damage_control/rules/payloads.json +120 -0
- damage_control_dev-0.1.0/src/damage_control/rules/payloads.yaml +155 -0
- damage_control_dev-0.1.0/src/damage_control/rules/publish.json +46 -0
- damage_control_dev-0.1.0/src/damage_control/rules/publish.yaml +46 -0
- damage_control_dev-0.1.0/src/damage_control/rules/registry.json +10 -0
- damage_control_dev-0.1.0/src/damage_control/rules/registry.yaml +11 -0
- damage_control_dev-0.1.0/src/damage_control/rules/remote.json +31 -0
- damage_control_dev-0.1.0/src/damage_control/rules/remote.yaml +53 -0
- damage_control_dev-0.1.0/src/damage_control/rules/tmux.json +17 -0
- damage_control_dev-0.1.0/src/damage_control/rules/tmux.yaml +16 -0
- damage_control_dev-0.1.0/src/damage_control/ruleset.py +94 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/aws.json +140 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/aws.yaml +119 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/docker.json +146 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/docker.yaml +123 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/gcp.json +186 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/gcp.yaml +158 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/gh.json +172 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/gh.yaml +145 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/git.json +166 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/git.yaml +143 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/kubectl.json +101 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/kubectl.yaml +85 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/npm.json +77 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/npm.yaml +65 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/terraform.json +68 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/terraform.yaml +57 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/uv.json +112 -0
- damage_control_dev-0.1.0/src/damage_control/tooldefs/uv.yaml +97 -0
- damage_control_dev-0.1.0/tools/build_rules_json.py +72 -0
- damage_control_dev-0.1.0/tools/coverage_matrix.py +74 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 dotdev
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: damage-control-dev
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Damage control for AI coding agents: a PreToolUse guard that blocks or asks before destructive commands and protected-path writes (Claude Code).
|
|
5
|
+
Author-email: dotdev <dev@dotdev.dev>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
License-File: NOTICE
|
|
9
|
+
Keywords: agent,ai,claude-code,hooks,safety
|
|
10
|
+
Classifier: Operating System :: MacOS
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Requires-Dist: pyyaml>=6.0
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "damage-control-dev"
|
|
3
|
+
dynamic = ["version"]
|
|
4
|
+
description = "Damage control for AI coding agents: a PreToolUse guard that blocks or asks before destructive commands and protected-path writes (Claude Code)."
|
|
5
|
+
requires-python = ">=3.9"
|
|
6
|
+
license = { text = "Apache-2.0" }
|
|
7
|
+
authors = [{ name = "dotdev", email = "dev@dotdev.dev" }]
|
|
8
|
+
keywords = ["ai", "agent", "claude-code", "safety", "hooks"]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Operating System :: MacOS",
|
|
11
|
+
"Operating System :: POSIX :: Linux",
|
|
12
|
+
"Programming Language :: Python :: 3",
|
|
13
|
+
"Programming Language :: Python :: 3.9",
|
|
14
|
+
]
|
|
15
|
+
# PyYAML is read ONLY for user-authored YAML (the policy file, rule/tooldef overlays); the
|
|
16
|
+
# bundled rules ship as JSON, so a hook with no PyYAML still enforces every bundled rule.
|
|
17
|
+
dependencies = ["pyyaml>=6.0"]
|
|
18
|
+
|
|
19
|
+
[project.scripts]
|
|
20
|
+
damage-control-dev = "damage_control.cli:main"
|
|
21
|
+
|
|
22
|
+
[build-system]
|
|
23
|
+
requires = ["hatchling"]
|
|
24
|
+
build-backend = "hatchling.build"
|
|
25
|
+
|
|
26
|
+
[tool.hatch.version]
|
|
27
|
+
path = "src/damage_control/__init__.py"
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.wheel]
|
|
30
|
+
packages = ["src/damage_control"]
|
|
31
|
+
include = [
|
|
32
|
+
"src/damage_control/**/*.py",
|
|
33
|
+
"src/damage_control/rules/*.json",
|
|
34
|
+
"src/damage_control/rules/*.yaml",
|
|
35
|
+
"src/damage_control/tooldefs/*.json",
|
|
36
|
+
"src/damage_control/tooldefs/*.yaml",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[tool.hatch.build.targets.sdist]
|
|
40
|
+
include = ["src/damage_control", "tools", "pyproject.toml"]
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""Damage control for AI coding agents.
|
|
2
|
+
|
|
3
|
+
This module stays import-free on purpose: ``python -I -m damage_control.hook`` imports
|
|
4
|
+
the package before it runs a line of ``hook.py``, and an exception raised here would
|
|
5
|
+
escape the hook's fail-closed wrapper (a Python crash exits 1, which Claude Code treats
|
|
6
|
+
as non-blocking).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""Claude Code adapter (#1209 M3/M4): hook payload -> ``ToolCall``/``Context``, ``Verdict`` -> exit code.
|
|
2
|
+
|
|
3
|
+
The five PreToolUse hooks differ only in WHICH tools they handle and how their rule
|
|
4
|
+
sources load; every one is ``parse -> decide -> render``, and this file holds the parse
|
|
5
|
+
and render halves (``policy.Guard`` is the decide half) plus ``run_hook``, the entry
|
|
6
|
+
``hook.py`` calls. A hook kind declares a tool table ``{tool_name: (kind, (payload
|
|
7
|
+
fields...))}``.
|
|
8
|
+
|
|
9
|
+
``render`` is the only place a decision becomes an exit code, a stderr line, an audit row
|
|
10
|
+
and a notification, so a verdict that reaches it can only be rendered one way.
|
|
11
|
+
|
|
12
|
+
Standard library only.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import subprocess
|
|
17
|
+
import sys
|
|
18
|
+
import types
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
21
|
+
|
|
22
|
+
from damage_control import audit, engine
|
|
23
|
+
from damage_control.policy import BYPASS, Context, Guard, ToolCall
|
|
24
|
+
from damage_control.profile import load_profile
|
|
25
|
+
|
|
26
|
+
# ``permission_mode`` values that mean the human opted out of ask-escalation friction:
|
|
27
|
+
# - "bypassPermissions" -> --dangerously-skip-permissions
|
|
28
|
+
# - "auto" -> autonomous /loop or similar auto mode
|
|
29
|
+
# Hard blocks still fire either way; an ``ask`` becomes an allow — EXCEPT an unverifiable one.
|
|
30
|
+
BYPASS_PERMISSION_MODES = frozenset({"bypassPermissions", "auto"})
|
|
31
|
+
|
|
32
|
+
# stdout of an ask: Claude Code's PreToolUse permission-decision envelope.
|
|
33
|
+
_HOOK_EVENT = "PreToolUse"
|
|
34
|
+
|
|
35
|
+
_Tools = Optional[Dict[str, Tuple[str, Tuple[str, ...]]]]
|
|
36
|
+
|
|
37
|
+
# hook kind -> (tool table, whether it judges shell commands and so loads the tooldefs).
|
|
38
|
+
# ``None`` for a table means every tool is an ``mcp`` call; the decision itself lives in
|
|
39
|
+
# ``policy.Guard``.
|
|
40
|
+
HOOK_KINDS: Dict[str, Tuple[_Tools, bool]] = {
|
|
41
|
+
# Bash and PowerShell calls: judge the command line.
|
|
42
|
+
"bash": ({"Bash": ("shell", ("command",)), "PowerShell": ("shell", ("command",))}, True),
|
|
43
|
+
# Edit and NotebookEdit calls: judge the target path. NotebookEdit writes files exactly
|
|
44
|
+
# like Edit but names its target ``notebook_path``: an operation refused via Edit must
|
|
45
|
+
# not be permitted via NotebookEdit (#923).
|
|
46
|
+
"edit": ({"Edit": ("edit", ("file_path", "notebook_path")),
|
|
47
|
+
"NotebookEdit": ("edit", ("file_path", "notebook_path"))}, False),
|
|
48
|
+
"write": ({"Write": ("write", ("file_path",))}, False),
|
|
49
|
+
# Read, Grep and Glob: judge the path they read or list. ``Glob`` only reveals filenames,
|
|
50
|
+
# but a zero-access directory is still off-limits.
|
|
51
|
+
"read": ({"Read": ("read", ("file_path",)), "Grep": ("read", ("path",)),
|
|
52
|
+
"Glob": ("read", ("path",))}, False),
|
|
53
|
+
"mcp": (None, True),
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class HookUsageError(ValueError):
|
|
58
|
+
"""The hook was invoked with arguments it does not understand. Blocks, like any hook failure."""
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def parse_args(argv: List[str]) -> Tuple[str, Optional[str]]:
|
|
62
|
+
"""``(hook kind, profile manifest path | None)`` from the hook's command-line arguments."""
|
|
63
|
+
args = list(argv)
|
|
64
|
+
profile: Optional[str] = None
|
|
65
|
+
if "--profile" in args:
|
|
66
|
+
i = args.index("--profile")
|
|
67
|
+
if i + 1 >= len(args):
|
|
68
|
+
raise HookUsageError("--profile needs a path")
|
|
69
|
+
profile = args[i + 1]
|
|
70
|
+
del args[i:i + 2]
|
|
71
|
+
if len(args) != 1 or args[0] not in HOOK_KINDS:
|
|
72
|
+
raise HookUsageError(f"usage: hook {{{'|'.join(HOOK_KINDS)}}} [--profile PATH]; got {argv!r}")
|
|
73
|
+
return args[0], profile
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def hook_namespace() -> Any:
|
|
77
|
+
"""What ``Guard`` and ``render`` reach engine functions and audit writers through."""
|
|
78
|
+
names = {n: getattr(audit, n) for n in ("log_allowed", "log_asked", "log_blocked", "log_disabled", "log_escape")}
|
|
79
|
+
return types.SimpleNamespace(**{**vars(engine), **names})
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def parse_payload(payload: Dict[str, Any], tools: Optional[Dict[str, Tuple[str, Tuple[str, ...]]]],
|
|
83
|
+
engine: Any) -> Tuple[Any, Any]:
|
|
84
|
+
"""``(ToolCall | None, Context)``; no call means this hook has nothing to judge.
|
|
85
|
+
|
|
86
|
+
``tools`` maps a tool name to ``(kind, fields)``: for ``shell`` the field names the
|
|
87
|
+
command, for read/write/edit the target path (the first non-empty field wins). ``None``
|
|
88
|
+
means every tool is an ``mcp`` call.
|
|
89
|
+
"""
|
|
90
|
+
tool_call, context, bypass = ToolCall, Context, BYPASS
|
|
91
|
+
# Session attribution for audit rows (#940): hand the parsed payload to the audit writers
|
|
92
|
+
# so every log_* call carries the conversation id.
|
|
93
|
+
audit.HOOK_INPUT = payload
|
|
94
|
+
|
|
95
|
+
tool_name = payload.get("tool_name", "")
|
|
96
|
+
tool_input = payload.get("tool_input", {})
|
|
97
|
+
ctx = context(
|
|
98
|
+
mode=bypass if payload.get("permission_mode", "") in BYPASS_PERMISSION_MODES else "default",
|
|
99
|
+
unattended=engine.is_unattended(),
|
|
100
|
+
)
|
|
101
|
+
if tools is None:
|
|
102
|
+
return tool_call("mcp", tool_name, tool_input=tool_input or {}), ctx
|
|
103
|
+
if tool_name not in tools:
|
|
104
|
+
return None, ctx
|
|
105
|
+
kind, fields = tools[tool_name]
|
|
106
|
+
value = next((tool_input.get(f, "") for f in fields if tool_input.get(f, "")), "")
|
|
107
|
+
if not value:
|
|
108
|
+
return None, ctx
|
|
109
|
+
if kind == "shell":
|
|
110
|
+
return tool_call(kind, tool_name, command=value, cwd=payload.get("cwd")), ctx
|
|
111
|
+
return tool_call(kind, tool_name, paths=(value,)), ctx
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def notify_unattended_block(engine: Any, subject: str, reason: str, rule_id: Optional[str]) -> None:
|
|
115
|
+
"""Fire-and-forget: tell the owner an unattended action was blocked.
|
|
116
|
+
|
|
117
|
+
Runs the profile's notifier argv, detached and non-blocking so it never sits on the
|
|
118
|
+
hook's hot path, with the event as one JSON object on its stdin:
|
|
119
|
+
``{"event": "unattended_block", "reason": ..., "rule_id": ..., "command": ...}``
|
|
120
|
+
(``command`` is truncated to 500 characters). Failures are swallowed (the block has
|
|
121
|
+
already fired and is audit-logged regardless). A hook never imports the host, hence the
|
|
122
|
+
subprocess.
|
|
123
|
+
"""
|
|
124
|
+
argv = engine.PROFILE.unattended_block_notifier
|
|
125
|
+
if not argv:
|
|
126
|
+
return
|
|
127
|
+
event = {"event": "unattended_block", "reason": reason or "", "rule_id": rule_id or "", "command": subject[:500]}
|
|
128
|
+
try:
|
|
129
|
+
child = subprocess.Popen(
|
|
130
|
+
list(argv), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.PIPE,
|
|
131
|
+
start_new_session=True,
|
|
132
|
+
)
|
|
133
|
+
child.stdin.write(json.dumps(event).encode())
|
|
134
|
+
child.stdin.close()
|
|
135
|
+
except Exception:
|
|
136
|
+
pass
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def render(verdict: Any, engine: Any) -> None:
|
|
140
|
+
"""Carry out ``verdict`` and exit: audit row, notification, stderr, then the exit code / ask envelope."""
|
|
141
|
+
if verdict.audit:
|
|
142
|
+
event, args, kwargs = verdict.audit
|
|
143
|
+
# An audit-log failure never changes the decision (#1108).
|
|
144
|
+
engine.audit_best_effort(getattr(engine, f"log_{event}"))(*args, **kwargs)
|
|
145
|
+
if verdict.notify:
|
|
146
|
+
notify_unattended_block(engine, *verdict.notify)
|
|
147
|
+
for line in verdict.stderr:
|
|
148
|
+
print(line, file=sys.stderr)
|
|
149
|
+
if verdict.decision == "block":
|
|
150
|
+
sys.exit(2)
|
|
151
|
+
if verdict.decision == "ask":
|
|
152
|
+
print(json.dumps({"hookSpecificOutput": {
|
|
153
|
+
"hookEventName": _HOOK_EVENT,
|
|
154
|
+
"permissionDecision": "ask",
|
|
155
|
+
"permissionDecisionReason": verdict.reason,
|
|
156
|
+
}}))
|
|
157
|
+
sys.exit(0)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def run_hook(argv: List[str]) -> None:
|
|
161
|
+
"""The hook entry: parse the arguments, install the profile, judge one payload, exit.
|
|
162
|
+
|
|
163
|
+
Everything runs under ``run_hook_guarded``, so any failure here (a bad argument, an
|
|
164
|
+
unreadable profile, a crashed rule load) is a blocking exit 2, never Python's own exit 1.
|
|
165
|
+
"""
|
|
166
|
+
|
|
167
|
+
def main() -> None:
|
|
168
|
+
kind, profile_path = parse_args(argv)
|
|
169
|
+
if profile_path:
|
|
170
|
+
engine.install_profile(load_profile(Path(profile_path)))
|
|
171
|
+
namespace = hook_namespace()
|
|
172
|
+
tools, tooldefs = HOOK_KINDS[kind]
|
|
173
|
+
guard = Guard(namespace.load_effective_config(tooldefs), namespace)
|
|
174
|
+
call, ctx = parse_payload(namespace.read_hook_input(), tools, namespace)
|
|
175
|
+
if call is None:
|
|
176
|
+
sys.exit(0)
|
|
177
|
+
render(guard.decide(call, ctx), namespace)
|
|
178
|
+
|
|
179
|
+
engine.run_hook_guarded(main)
|