1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
"""Claude Code permissions: tool rules, extra directories and folder trust.
They live in three kinds of files:
~/.claude/settings.json user scope, every project
<project>/.claude/settings.json project scope, usually committed
<project>/.claude/settings.local.json local scope, personal, git-ignored
each with a `permissions` object:
{"permissions": {"allow": [...], "deny": [...], "ask": [...],
"additionalDirectories": [...]}}
Trust is elsewhere: `~/.claude.json` keeps one entry per absolute project path,
and `hasTrustDialogAccepted` in it records that the "do you trust this folder?"
dialog was accepted. That file also holds the account and every other piece of
Claude Code state, so it is rewritten keeping all of it as it was.
Every write is read-modify-replace: the file is read again right before the
change and swapped in atomically, keeping its permissions.
"""
import json
import os
import re
import tempfile
from .sessions import SessionError
SCOPES = ("user", "project", "local")
LISTS = ("allow", "deny", "ask")
DIRS_KEY = "additionalDirectories"
TRUST_KEY = "hasTrustDialogAccepted"
# Tool, Tool(specifier) or mcp__server__tool. The specifier is free text.
RULE_RE = re.compile(r"^[A-Za-z][\w.-]*(\(.*\))?$", re.S)
# ──────────────────────────────── locations ──────────────────────────────────
def config_dir():
return os.environ.get("CLAUDE_CONFIG_DIR") or os.path.join(
os.path.expanduser("~"), ".claude")
def global_state_path():
"""~/.claude.json, or .claude.json inside CLAUDE_CONFIG_DIR if it is set."""
if os.environ.get("CLAUDE_CONFIG_DIR"):
return os.path.join(os.environ["CLAUDE_CONFIG_DIR"], ".claude.json")
return os.path.join(os.path.expanduser("~"), ".claude.json")
def settings_path(scope, project=None):
if scope == "user":
return os.path.join(config_dir(), "settings.json")
if not project:
raise SessionError(f"the {scope} scope needs a project (-p PATH)")
name = "settings.json" if scope == "project" else "settings.local.json"
return os.path.join(project, ".claude", name)
# ──────────────────────────────── files ───────────────────────────────────
def read_json(path):
"""The file's object, {} if it does not exist. Broken JSON is an error:
rewriting it would lose whatever it had."""
try:
with open(path, encoding="utf-8") as fh:
text = fh.read()
except FileNotFoundError:
return {}
except OSError as e:
raise SessionError(f"cannot read {path}: {e.strerror}")
if not text.strip():
return {}
try:
data = json.loads(text)
except ValueError as e:
raise SessionError(f"{path} is not valid JSON ({e}); fix it by hand first")
if not isinstance(data, dict):
raise SessionError(f"{path} does not hold a JSON object")
return data
def write_json(path, data, mode=None):
"""Atomic replace that keeps the file's mode (0644 minus umask if new) and
whether it ended in a newline, so an untouched key reads exactly the same.
An explicit `mode` wins over both, for files that hold secrets."""
forced = mode
try:
mode = os.stat(path).st_mode & 0o777
with open(path, "rb") as fh:
fh.seek(0, os.SEEK_END)
if fh.tell():
fh.seek(-1, os.SEEK_END)
newline = fh.read(1) == b"\n"
else:
newline = True
except FileNotFoundError:
umask = os.umask(0)
os.umask(umask)
mode = 0o666 & ~umask
newline = True
folder = os.path.dirname(path) or "."
os.makedirs(folder, exist_ok=True)
fd, tmp = tempfile.mkstemp(dir=folder, prefix=".tmp-", suffix=".json")
try:
with os.fdopen(fd, "w", encoding="utf-8") as fh:
json.dump(data, fh, indent=2, ensure_ascii=False)
if newline:
fh.write("\n")
os.chmod(tmp, forced if forced is not None else mode)
os.replace(tmp, path)
except BaseException:
try:
os.remove(tmp)
except OSError:
pass
raise
# ──────────────────────────────── reading ───────────────────────────────────
def permissions_of(data):
perms = data.get("permissions")
return perms if isinstance(perms, dict) else {}
def rules(data):
"""{"allow": [...], "deny": [...], "ask": [...], "dirs": [...], "mode": str}"""
perms = permissions_of(data)
out = {k: [r for r in perms.get(k) or [] if isinstance(r, str)] for k in LISTS}
out["dirs"] = [d for d in perms.get(DIRS_KEY) or [] if isinstance(d, str)]
out["mode"] = perms.get("defaultMode") if isinstance(
perms.get("defaultMode"), str) else None
return out
def is_empty(r):
return not any(r[k] for k in (*LISTS, "dirs", "mode"))
def trusted_projects(state=None):
"""{path: bool} for every project ~/.claude.json knows about."""
state = read_json(global_state_path()) if state is None else state
projects = state.get("projects")
if not isinstance(projects, dict):
return {}
return {p: bool(v.get(TRUST_KEY)) for p, v in projects.items()
if isinstance(v, dict)}
def trusted_parent(path, trust):
"""Closest trusted ancestor of `path`, or None."""
cur = path
while True:
parent = os.path.dirname(cur)
if parent == cur:
return None
if trust.get(parent):
return parent
cur = parent
def overview(project_paths):
"""User scope plus one entry per project, for the listing."""
trust = trusted_projects()
user_path = settings_path("user")
projects = []
for p in sorted(set(project_paths) | set(trust)):
scopes = []
for scope in ("project", "local"):
path = settings_path(scope, p)
scopes.append({"scope": scope, "path": path,
"rules": rules(read_json(path))})
projects.append({"path": p, "trusted": trust.get(p, False),
"parent": trusted_parent(p, trust),
"exists": os.path.isdir(p), "scopes": scopes})
return {"user": {"scope": "user", "path": user_path,
"rules": rules(read_json(user_path))},
"projects": projects}
# ──────────────────────────────── targets ───────────────────────────────────
def resolve_project(ref, known):
"""An existing directory, or the only known project whose path contains `ref`."""
expanded = os.path.expanduser(ref)
if os.path.isdir(expanded):
return os.path.abspath(expanded).rstrip("/") or "/"
needle = expanded.rstrip("/").lower()
hits = sorted({p for p in known if needle in p.lower()})
if len(hits) == 1:
return hits[0]
if not hits:
raise SessionError(f"{ref} is not a directory nor a known project")
shown = "\n ".join(hits[:8]) + ("\n …" if len(hits) > 8 else "")
raise SessionError(f"{ref} is ambiguous, it matches {len(hits)} projects:\n {shown}")
def check_rule(rule):
rule = rule.strip()
if not RULE_RE.match(rule) or rule.count("(") != rule.count(")"):
raise SessionError(
f"{rule!r} does not look like a rule: Tool or Tool(specifier), "
f"e.g. Bash(npm test:*), Read(./secrets/**), WebFetch")
return rule
def normalize_dir(d):
return os.path.abspath(os.path.expanduser(d)).rstrip("/") or "/"
# ──────────────────────────────── changes ───────────────────────────────────
#
# Each change is a tuple and `plan` turns them into human-readable steps
# against the current contents, so --dry-run and the real run say the same.
#
# ("add", list, rule) ("forget", rule)
# ("add-dir", path) ("remove-dir", path)
def apply_rules(data, changes):
"""Applies the changes to `data` in place. Returns the list of steps done;
a change that is already in effect produces no step."""
perms = data.get("permissions")
if not isinstance(perms, dict):
perms = {}
steps = []
def lst(key):
cur = perms.get(key)
return cur if isinstance(cur, list) else []
for change in changes:
kind = change[0]
if kind == "add":
_, target, rule = change
for other in LISTS:
if other != target and rule in lst(other):
perms[other] = [r for r in lst(other) if r != rule]
steps.append(f"{rule} leaves {other}")
if rule not in lst(target):
perms[target] = lst(target) + [rule]
steps.append(f"{target} {rule}")
elif kind == "forget":
_, rule = change
for key in LISTS:
if rule in lst(key):
perms[key] = [r for r in lst(key) if r != rule]
steps.append(f"{rule} leaves {key}")
elif kind == "add-dir":
_, d = change
if d not in lst(DIRS_KEY):
perms[DIRS_KEY] = lst(DIRS_KEY) + [d]
steps.append(f"directory {d}")
elif kind == "remove-dir":
_, d = change
gone = [x for x in lst(DIRS_KEY) if x == d or normalize_dir(x) == d]
if gone:
perms[DIRS_KEY] = [x for x in lst(DIRS_KEY) if x not in gone]
steps.append(f"directory {d} removed")
# Lists left empty are dropped, and so is an empty permissions object.
for key in (*LISTS, DIRS_KEY):
if key in perms and perms[key] == []:
del perms[key]
if perms:
data["permissions"] = perms
else:
data.pop("permissions", None)
return steps
def edit_settings(path, changes, dry_run=False):
data = read_json(path)
steps = apply_rules(data, changes)
if steps and not dry_run:
write_json(path, data)
return steps
def set_trust(project, value, dry_run=False):
"""Sets hasTrustDialogAccepted for that path. Returns whether it changed.
~/.claude.json is rewritten by every running Claude Code, so it is read
right before writing and the write is retried if the file moved under us.
"""
path = global_state_path()
for _ in range(3):
try:
before = os.stat(path).st_mtime_ns
except FileNotFoundError:
before = None
state = read_json(path)
projects = state.
|