@cdot65/prisma-airs-cursor-hooks 0.1.0
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.
- package/LICENSE +201 -0
- package/README.md +295 -0
- package/airs-config.json +32 -0
- package/dist/adapters/cursor-adapter.js +51 -0
- package/dist/adapters/index.js +1 -0
- package/dist/adapters/types.js +1 -0
- package/dist/airs-client.js +92 -0
- package/dist/circuit-breaker.js +66 -0
- package/dist/cli.js +59 -0
- package/dist/code-extractor.js +85 -0
- package/dist/config.js +101 -0
- package/dist/dlp-masking.js +31 -0
- package/dist/hooks/after-agent-response.js +75 -0
- package/dist/hooks/before-submit-prompt.js +75 -0
- package/dist/log-rotation.js +27 -0
- package/dist/logger.js +47 -0
- package/dist/scanner.js +298 -0
- package/dist/types.js +1 -0
- package/package.json +64 -0
- package/scripts/airs-stats.ts +119 -0
- package/scripts/install-hooks.ts +153 -0
- package/scripts/uninstall-hooks.ts +72 -0
- package/scripts/validate-connection.ts +45 -0
- package/scripts/validate-detection.ts +52 -0
- package/scripts/verify-hooks.ts +84 -0
- package/src/adapters/cursor-adapter.ts +62 -0
- package/src/adapters/index.ts +2 -0
- package/src/adapters/types.ts +14 -0
- package/src/airs-client.ts +136 -0
- package/src/circuit-breaker.ts +88 -0
- package/src/cli.ts +65 -0
- package/src/code-extractor.ts +99 -0
- package/src/config.ts +126 -0
- package/src/dlp-masking.ts +48 -0
- package/src/hooks/after-agent-response.ts +84 -0
- package/src/hooks/before-submit-prompt.ts +86 -0
- package/src/log-rotation.ts +27 -0
- package/src/logger.ts +55 -0
- package/src/scanner.ts +388 -0
- package/src/types.ts +153 -0
- package/tsconfig.build.json +19 -0
package/LICENSE
ADDED
|
@@ -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 [yyyy] [name of copyright owner]
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
# Prisma AIRS Cursor Hooks
|
|
2
|
+
|
|
3
|
+
Cursor IDE hooks that scan prompts and AI responses in real-time using [Prisma AI Runtime Security (AIRS)](https://www.paloaltonetworks.com/prisma/ai-runtime-security). Intercepts prompts before they reach the AI agent and responses before they're displayed, scanning for prompt injections, malicious code, sensitive data leakage, and policy violations.
|
|
4
|
+
|
|
5
|
+
Built on the [`@cdot65/prisma-airs-sdk`](https://github.com/cdot65/prisma-airs-sdk).
|
|
6
|
+
|
|
7
|
+
## Architecture
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
Developer prompt → beforeSubmitPrompt hook → AIRS Sync API → allow/block
|
|
11
|
+
↓
|
|
12
|
+
Cursor AI Agent (if allowed)
|
|
13
|
+
↓
|
|
14
|
+
AI response → afterAgentResponse hook → code extractor → AIRS Sync API → allow/block
|
|
15
|
+
↓
|
|
16
|
+
(response field + code_response field)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Both hooks use Cursor's native hooks.json system. They receive structured JSON on stdin, scan via the AIRS API, and reply on stdout (`{ "continue": false }` to block prompts, `{ "permission": "deny" }` + exit code 2 to block responses).
|
|
20
|
+
|
|
21
|
+
## Prerequisites
|
|
22
|
+
|
|
23
|
+
- **Node.js 18+** (native fetch, crypto.randomUUID)
|
|
24
|
+
- **Cursor IDE** (with hooks support)
|
|
25
|
+
- **Prisma AIRS API key** and regional endpoint URL
|
|
26
|
+
- **AIRS security profiles** configured for prompt and response scanning
|
|
27
|
+
|
|
28
|
+
## Setup
|
|
29
|
+
|
|
30
|
+
### Option A: Install from npm (recommended)
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install -g @cdot65/prisma-airs-cursor-hooks
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Then register hooks in Cursor:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
prisma-airs-hooks install --global
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Option B: Install from source
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
git clone https://github.com/cdot65/prisma-airs-cursor-hooks.git
|
|
46
|
+
cd prisma-airs-cursor-hooks
|
|
47
|
+
npm install # also runs `npm run build` via prepare hook
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 2. Set environment variables
|
|
51
|
+
|
|
52
|
+
Add to your shell profile (`~/.zshrc`, `~/.bashrc`, etc.):
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
export AIRS_API_KEY=<your-x-pan-token>
|
|
56
|
+
export AIRS_API_ENDPOINT=https://service.api.aisecurity.paloaltonetworks.com # optional, defaults to US
|
|
57
|
+
export AIRS_PROMPT_PROFILE=cursor-ide-prompt-profile # optional
|
|
58
|
+
export AIRS_RESPONSE_PROFILE=cursor-ide-response-profile # optional
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
> **Note:** Cursor inherits your shell environment, so hooks automatically have access to these variables. Only `AIRS_API_KEY` is required — endpoint defaults to US and profile names default to `cursor-ide-prompt-profile` / `cursor-ide-response-profile`.
|
|
62
|
+
|
|
63
|
+
Available regional endpoints:
|
|
64
|
+
| Region | Endpoint |
|
|
65
|
+
|--------|----------|
|
|
66
|
+
| US (default) | `https://service.api.aisecurity.paloaltonetworks.com` |
|
|
67
|
+
| EU | `https://service-de.api.aisecurity.paloaltonetworks.com` |
|
|
68
|
+
| India | `https://service-in.api.aisecurity.paloaltonetworks.com` |
|
|
69
|
+
| Singapore | `https://service-sg.api.aisecurity.paloaltonetworks.com` |
|
|
70
|
+
|
|
71
|
+
### 3. Validate connectivity
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
# Test that your API key and endpoint work
|
|
75
|
+
npm run validate-connection
|
|
76
|
+
|
|
77
|
+
# Confirm prompt injection detection is active
|
|
78
|
+
npm run validate-detection
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 4. Install hooks into Cursor
|
|
82
|
+
|
|
83
|
+
If installed from npm:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
prisma-airs-hooks install --global
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
If installed from source:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
npm run install-hooks -- --global # all workspaces (~/.cursor/hooks.json)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
This writes `hooks.json` registering two hooks pointing at precompiled JS in `dist/`:
|
|
96
|
+
- **`beforeSubmitPrompt`** — scans every prompt before it reaches the AI agent
|
|
97
|
+
- **`afterAgentResponse`** — scans every AI response (with code extraction) before display
|
|
98
|
+
|
|
99
|
+
It also copies `airs-config.json` to the hooks config directory.
|
|
100
|
+
|
|
101
|
+
> **Why compiled JS?** Hooks run as a fresh process on every prompt/response. Using precompiled JS (`node dist/...`) vs TypeScript (`npx tsx src/...`) eliminates ~1.5s of startup overhead per invocation (npx resolution + tsx transpilation). See [Development mode](#development-mode) for the tsx alternative.
|
|
102
|
+
|
|
103
|
+
### 5. Restart Cursor
|
|
104
|
+
|
|
105
|
+
Cursor reads `hooks.json` at startup. **Restart Cursor** to activate the hooks.
|
|
106
|
+
|
|
107
|
+
### 6. Verify installation
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
npm run verify-hooks
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Configuration
|
|
114
|
+
|
|
115
|
+
Runtime config lives at `.cursor/hooks/airs-config.json`:
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"endpoint": "${AIRS_API_ENDPOINT}",
|
|
120
|
+
"apiKeyEnvVar": "AIRS_API_KEY",
|
|
121
|
+
"profiles": {
|
|
122
|
+
"prompt": "cursor-ide-prompt-profile",
|
|
123
|
+
"response": "cursor-ide-response-profile"
|
|
124
|
+
},
|
|
125
|
+
"mode": "observe",
|
|
126
|
+
"timeout_ms": 3000,
|
|
127
|
+
"retry": {
|
|
128
|
+
"enabled": true,
|
|
129
|
+
"max_attempts": 1,
|
|
130
|
+
"backoff_base_ms": 200
|
|
131
|
+
},
|
|
132
|
+
"logging": {
|
|
133
|
+
"path": ".cursor/hooks/airs-scan.log",
|
|
134
|
+
"include_content": false
|
|
135
|
+
},
|
|
136
|
+
"enforcement": {
|
|
137
|
+
"prompt_injection": "block",
|
|
138
|
+
"dlp": "block",
|
|
139
|
+
"malicious_code": "block",
|
|
140
|
+
"url_categorization": "block",
|
|
141
|
+
"toxicity": "block",
|
|
142
|
+
"custom_topic": "block"
|
|
143
|
+
},
|
|
144
|
+
"circuit_breaker": {
|
|
145
|
+
"enabled": true,
|
|
146
|
+
"failure_threshold": 5,
|
|
147
|
+
"cooldown_ms": 60000
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Modes
|
|
153
|
+
|
|
154
|
+
| Mode | Behavior |
|
|
155
|
+
|------|----------|
|
|
156
|
+
| `observe` | Log scan results, never block (default — start here) |
|
|
157
|
+
| `enforce` | Block prompts/responses that AIRS flags |
|
|
158
|
+
| `bypass` | Skip scanning entirely |
|
|
159
|
+
|
|
160
|
+
### Enforcement actions
|
|
161
|
+
|
|
162
|
+
When `mode` is `enforce`, each detection service can be configured independently:
|
|
163
|
+
|
|
164
|
+
| Action | Behavior |
|
|
165
|
+
|--------|----------|
|
|
166
|
+
| `block` | Prevent the prompt/response from passing through |
|
|
167
|
+
| `mask` | Log a warning and allow through (DLP masking) |
|
|
168
|
+
| `allow` | Log but allow through |
|
|
169
|
+
|
|
170
|
+
### Circuit breaker
|
|
171
|
+
|
|
172
|
+
After `failure_threshold` consecutive AIRS API failures, scanning is temporarily bypassed for `cooldown_ms` milliseconds. A probe request is sent after cooldown — if it succeeds, scanning resumes normally.
|
|
173
|
+
|
|
174
|
+
## Scanning details
|
|
175
|
+
|
|
176
|
+
### Prompt scanning (beforeSubmitPrompt)
|
|
177
|
+
|
|
178
|
+
- Prompt text sent to AIRS with `prompt` content key
|
|
179
|
+
- Scanned against the prompt security profile (prompt injection, DLP, toxicity, custom topics)
|
|
180
|
+
|
|
181
|
+
### Response scanning (afterAgentResponse)
|
|
182
|
+
|
|
183
|
+
- AI response is parsed by the code extractor:
|
|
184
|
+
- Fenced code blocks (` ```lang `) are extracted with language detection
|
|
185
|
+
- Indented code blocks (4+ spaces) are detected
|
|
186
|
+
- Heuristic fallback for unfenced code-like content
|
|
187
|
+
- Natural language goes in the `response` field
|
|
188
|
+
- Extracted code goes in the `code_response` field (triggers WildFire/ATP malicious code detection)
|
|
189
|
+
- Scanned against the response security profile (malicious code, DLP, URL categorization, toxicity)
|
|
190
|
+
|
|
191
|
+
### Fail-open design
|
|
192
|
+
|
|
193
|
+
Scanning **never blocks the developer workflow** on infrastructure failures:
|
|
194
|
+
- `failClosed: false` in hooks.json — Cursor allows through if the hook process crashes
|
|
195
|
+
- Network errors and timeouts return `{ "permission": "allow" }`
|
|
196
|
+
- Config errors return allow with a warning message
|
|
197
|
+
- Circuit breaker bypasses scanning after consecutive failures
|
|
198
|
+
|
|
199
|
+
## Commands
|
|
200
|
+
|
|
201
|
+
| Command | Description |
|
|
202
|
+
|---------|-------------|
|
|
203
|
+
| `npm run build` | Compile hooks to `dist/` (also runs on `npm install`) |
|
|
204
|
+
| `npm test` | Run all tests (66 tests across 9 suites) |
|
|
205
|
+
| `npm run typecheck` | TypeScript type checking |
|
|
206
|
+
| `npm run validate-connection` | Test AIRS API connectivity |
|
|
207
|
+
| `npm run validate-detection` | Verify prompt injection detection |
|
|
208
|
+
| `npm run install-hooks` | Write AIRS entries to `.cursor/hooks.json` |
|
|
209
|
+
| `npm run uninstall-hooks` | Remove AIRS entries from `.cursor/hooks.json` |
|
|
210
|
+
| `npm run verify-hooks` | Check hooks are installed and env vars set |
|
|
211
|
+
| `npm run stats` | Show scan statistics from log file |
|
|
212
|
+
| `npm run stats -- --since 7d --json` | Stats for last 7 days as JSON |
|
|
213
|
+
|
|
214
|
+
## Uninstall
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
# npm global install
|
|
218
|
+
prisma-airs-hooks uninstall --global
|
|
219
|
+
|
|
220
|
+
# from source
|
|
221
|
+
npm run uninstall-hooks -- --global
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Removes AIRS entries from `.cursor/hooks.json` while preserving other hooks, config, and logs. Restart Cursor after uninstalling.
|
|
225
|
+
|
|
226
|
+
## Development
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
# Install dependencies + build
|
|
230
|
+
npm install
|
|
231
|
+
|
|
232
|
+
# Run tests in watch mode
|
|
233
|
+
npm run test:watch
|
|
234
|
+
|
|
235
|
+
# Type check
|
|
236
|
+
npm run typecheck
|
|
237
|
+
|
|
238
|
+
# Rebuild after source changes
|
|
239
|
+
npm run build
|
|
240
|
+
|
|
241
|
+
# Build docs
|
|
242
|
+
npm run docs:build
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Development mode
|
|
246
|
+
|
|
247
|
+
During development you can run hooks directly from TypeScript source without a build step. This is useful when iterating on hook logic — changes take effect immediately without rebuilding.
|
|
248
|
+
|
|
249
|
+
Manually edit `~/.cursor/hooks.json` (or `.cursor/hooks.json`) to use tsx:
|
|
250
|
+
|
|
251
|
+
```json
|
|
252
|
+
{
|
|
253
|
+
"command": "npx tsx \"/path/to/prisma-airs-cursor-hooks/src/hooks/before-submit-prompt.ts\""
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
This adds ~1.5s per hook invocation compared to compiled JS, so switch back to `node dist/...` for production use:
|
|
258
|
+
|
|
259
|
+
```bash
|
|
260
|
+
npm run build
|
|
261
|
+
npm run install-hooks -- --global
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### Project structure
|
|
265
|
+
|
|
266
|
+
```
|
|
267
|
+
src/ TypeScript source
|
|
268
|
+
hooks/
|
|
269
|
+
before-submit-prompt.ts Cursor beforeSubmitPrompt entry point
|
|
270
|
+
after-agent-response.ts Cursor afterAgentResponse entry point
|
|
271
|
+
config.ts Config loader (project → global fallback)
|
|
272
|
+
airs-client.ts SDK wrapper with circuit breaker
|
|
273
|
+
scanner.ts Scan orchestration + DLP masking + UX messages
|
|
274
|
+
code-extractor.ts Separates code from natural language
|
|
275
|
+
logger.ts JSON Lines logging with rotation
|
|
276
|
+
circuit-breaker.ts Failure tracking with cooldown bypass
|
|
277
|
+
dlp-masking.ts Per-service enforcement actions
|
|
278
|
+
log-rotation.ts Rotate logs at 10MB
|
|
279
|
+
types.ts TypeScript interfaces
|
|
280
|
+
adapters/ Multi-IDE adapter layer
|
|
281
|
+
dist/ Compiled JS (production hooks point here)
|
|
282
|
+
scripts/
|
|
283
|
+
install-hooks.ts Write .cursor/hooks.json (points at dist/)
|
|
284
|
+
uninstall-hooks.ts Remove AIRS entries from hooks.json
|
|
285
|
+
verify-hooks.ts Tamper detection
|
|
286
|
+
validate-connection.ts Test AIRS connectivity
|
|
287
|
+
validate-detection.ts Verify detection works
|
|
288
|
+
airs-stats.ts Scan statistics CLI
|
|
289
|
+
test/
|
|
290
|
+
9 test suites, 66 tests (incl. compiled JS integration tests)
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## License
|
|
294
|
+
|
|
295
|
+
MIT
|
package/airs-config.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"endpoint": "${AIRS_API_ENDPOINT}",
|
|
3
|
+
"apiKeyEnvVar": "AIRS_API_KEY",
|
|
4
|
+
"profiles": {
|
|
5
|
+
"prompt": "${AIRS_PROMPT_PROFILE}",
|
|
6
|
+
"response": "${AIRS_RESPONSE_PROFILE}"
|
|
7
|
+
},
|
|
8
|
+
"mode": "enforce",
|
|
9
|
+
"timeout_ms": 3000,
|
|
10
|
+
"retry": {
|
|
11
|
+
"enabled": true,
|
|
12
|
+
"max_attempts": 1,
|
|
13
|
+
"backoff_base_ms": 200
|
|
14
|
+
},
|
|
15
|
+
"logging": {
|
|
16
|
+
"path": ".cursor/hooks/airs-scan.log",
|
|
17
|
+
"include_content": false
|
|
18
|
+
},
|
|
19
|
+
"enforcement": {
|
|
20
|
+
"prompt_injection": "block",
|
|
21
|
+
"dlp": "block",
|
|
22
|
+
"malicious_code": "block",
|
|
23
|
+
"url_categorization": "block",
|
|
24
|
+
"toxicity": "block",
|
|
25
|
+
"custom_topic": "block"
|
|
26
|
+
},
|
|
27
|
+
"circuit_breaker": {
|
|
28
|
+
"enabled": true,
|
|
29
|
+
"failure_threshold": 5,
|
|
30
|
+
"cooldown_ms": 60000
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor IDE hook adapter.
|
|
3
|
+
*
|
|
4
|
+
* Cursor hooks contract (v1):
|
|
5
|
+
* stdin → structured JSON with event-specific fields
|
|
6
|
+
* stdout → JSON { permission: "allow"|"deny"|"ask", userMessage?, agentMessage? }
|
|
7
|
+
* exit 0 = success; exit 2 = deny
|
|
8
|
+
* stderr → debug logs (shown in Cursor's "Hooks" output panel)
|
|
9
|
+
*
|
|
10
|
+
* Environment variables injected by Cursor:
|
|
11
|
+
* CURSOR_PROJECT_DIR, CURSOR_VERSION, CURSOR_USER_EMAIL,
|
|
12
|
+
* CURSOR_TRANSCRIPT_PATH, CURSOR_CODE_REMOTE
|
|
13
|
+
*/
|
|
14
|
+
export class CursorHookAdapter {
|
|
15
|
+
preSendHandler;
|
|
16
|
+
preDisplayHandler;
|
|
17
|
+
registerPreSend(handler) {
|
|
18
|
+
this.preSendHandler = handler;
|
|
19
|
+
}
|
|
20
|
+
registerPreDisplay(handler) {
|
|
21
|
+
this.preDisplayHandler = handler;
|
|
22
|
+
}
|
|
23
|
+
/** Execute a hook given parsed content and the registered handler */
|
|
24
|
+
async execute(content, handler) {
|
|
25
|
+
if (!handler) {
|
|
26
|
+
this.respond({ permission: "allow" });
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const result = await handler(content);
|
|
31
|
+
if (result.action === "block") {
|
|
32
|
+
this.respond({
|
|
33
|
+
permission: "deny",
|
|
34
|
+
userMessage: result.message ?? "Blocked by Prisma AIRS.",
|
|
35
|
+
});
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const output = { permission: "allow" };
|
|
39
|
+
if (result.message)
|
|
40
|
+
output.userMessage = result.message;
|
|
41
|
+
this.respond(output);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// Fail-open
|
|
45
|
+
this.respond({ permission: "allow" });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
respond(output) {
|
|
49
|
+
process.stdout.write(JSON.stringify(output) + "\n");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CursorHookAdapter } from "./cursor-adapter.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { init, Scanner, Content, AISecSDKException, } from "@cdot65/prisma-airs-sdk";
|
|
2
|
+
import { getApiKey } from "./config.js";
|
|
3
|
+
import { CircuitBreaker } from "./circuit-breaker.js";
|
|
4
|
+
let initialized = false;
|
|
5
|
+
/** Module-level circuit breaker — persists across scans within a process */
|
|
6
|
+
let breaker = null;
|
|
7
|
+
/** Initialize the SDK from our hook config */
|
|
8
|
+
function ensureInit(config, logger) {
|
|
9
|
+
if (!initialized) {
|
|
10
|
+
const apiKey = getApiKey(config);
|
|
11
|
+
init({
|
|
12
|
+
apiKey,
|
|
13
|
+
apiEndpoint: config.endpoint,
|
|
14
|
+
numRetries: config.retry.enabled ? config.retry.max_attempts : 0,
|
|
15
|
+
});
|
|
16
|
+
initialized = true;
|
|
17
|
+
}
|
|
18
|
+
if (!breaker && config.circuit_breaker?.enabled) {
|
|
19
|
+
breaker = new CircuitBreaker({
|
|
20
|
+
failureThreshold: config.circuit_breaker.failure_threshold,
|
|
21
|
+
cooldownMs: config.circuit_breaker.cooldown_ms,
|
|
22
|
+
}, (from, to) => {
|
|
23
|
+
logger?.logEvent("circuit_breaker_transition", { from, to });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Reset init state (for testing) */
|
|
28
|
+
export function resetInit() {
|
|
29
|
+
initialized = false;
|
|
30
|
+
breaker = null;
|
|
31
|
+
}
|
|
32
|
+
/** Get the current circuit breaker (exposed for stats/diagnostics) */
|
|
33
|
+
export function getCircuitBreaker() {
|
|
34
|
+
return breaker;
|
|
35
|
+
}
|
|
36
|
+
/** Synthetic fail-open result when circuit breaker is open */
|
|
37
|
+
function circuitOpenResult() {
|
|
38
|
+
return {
|
|
39
|
+
action: "allow",
|
|
40
|
+
scan_id: "",
|
|
41
|
+
report_id: "",
|
|
42
|
+
category: "bypassed",
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Scan a prompt via AIRS Sync API using the SDK */
|
|
46
|
+
export async function scanPromptContent(config, prompt, appUser, logger) {
|
|
47
|
+
ensureInit(config, logger);
|
|
48
|
+
// Circuit breaker check — bypass scan if open
|
|
49
|
+
if (breaker && !breaker.shouldAllow()) {
|
|
50
|
+
logger?.logEvent("scan_bypassed_circuit_open", { direction: "prompt" });
|
|
51
|
+
return { result: circuitOpenResult(), latencyMs: 0 };
|
|
52
|
+
}
|
|
53
|
+
const scanner = new Scanner();
|
|
54
|
+
const content = new Content({ prompt });
|
|
55
|
+
const start = Date.now();
|
|
56
|
+
try {
|
|
57
|
+
const result = await scanner.syncScan({ profile_name: config.profiles.prompt }, content, { metadata: { app_name: "cursor-ide", app_user: appUser } });
|
|
58
|
+
const latencyMs = Date.now() - start;
|
|
59
|
+
breaker?.recordSuccess();
|
|
60
|
+
return { result, latencyMs };
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
breaker?.recordFailure();
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** Scan a response (with optional code) via AIRS Sync API using the SDK */
|
|
68
|
+
export async function scanResponseContent(config, response, codeResponse, appUser, logger) {
|
|
69
|
+
ensureInit(config, logger);
|
|
70
|
+
if (breaker && !breaker.shouldAllow()) {
|
|
71
|
+
logger?.logEvent("scan_bypassed_circuit_open", { direction: "response" });
|
|
72
|
+
return { result: circuitOpenResult(), latencyMs: 0 };
|
|
73
|
+
}
|
|
74
|
+
const scanner = new Scanner();
|
|
75
|
+
const contentOpts = { response };
|
|
76
|
+
if (codeResponse) {
|
|
77
|
+
contentOpts.codeResponse = codeResponse;
|
|
78
|
+
}
|
|
79
|
+
const content = new Content(contentOpts);
|
|
80
|
+
const start = Date.now();
|
|
81
|
+
try {
|
|
82
|
+
const result = await scanner.syncScan({ profile_name: config.profiles.response }, content, { metadata: { app_name: "cursor-ide", app_user: appUser } });
|
|
83
|
+
const latencyMs = Date.now() - start;
|
|
84
|
+
breaker?.recordSuccess();
|
|
85
|
+
return { result, latencyMs };
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
breaker?.recordFailure();
|
|
89
|
+
throw err;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export { AISecSDKException };
|