pywrit 0.2.13__tar.gz → 0.2.14__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.
- {pywrit-0.2.13/src/pywrit.egg-info → pywrit-0.2.14}/PKG-INFO +11 -2
- {pywrit-0.2.13 → pywrit-0.2.14}/README.md +9 -1
- {pywrit-0.2.13 → pywrit-0.2.14}/pyproject.toml +4 -3
- {pywrit-0.2.13 → pywrit-0.2.14}/src/pywrit/__init__.py +1 -1
- {pywrit-0.2.13 → pywrit-0.2.14}/src/pywrit/cli.py +526 -42
- {pywrit-0.2.13 → pywrit-0.2.14/src/pywrit.egg-info}/PKG-INFO +11 -2
- {pywrit-0.2.13 → pywrit-0.2.14}/src/pywrit.egg-info/SOURCES.txt +1 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/src/pywrit.egg-info/requires.txt +1 -0
- pywrit-0.2.14/tests/test_scan_go.py +344 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/tests/test_scan_tsjs.py +15 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/LICENSE +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/setup.cfg +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/src/pywrit/client.py +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/src/pywrit/errors.py +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/src/pywrit.egg-info/dependency_links.txt +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/src/pywrit.egg-info/entry_points.txt +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/src/pywrit.egg-info/top_level.txt +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/tests/test_client.py +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/tests/test_scan_json.py +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/tests/test_scan_python.py +0 -0
- {pywrit-0.2.13 → pywrit-0.2.14}/tests/test_wrap.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pywrit
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.14
|
|
4
4
|
Summary: Python client + CLI for Writ — the gate before the write
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Project-URL: Homepage, https://github.com/withwrit/pywrit
|
|
@@ -13,6 +13,7 @@ Provides-Extra: polyglot
|
|
|
13
13
|
Requires-Dist: tree-sitter>=0.23; extra == "polyglot"
|
|
14
14
|
Requires-Dist: tree-sitter-typescript>=0.23; extra == "polyglot"
|
|
15
15
|
Requires-Dist: tree-sitter-javascript>=0.23; extra == "polyglot"
|
|
16
|
+
Requires-Dist: tree-sitter-go>=0.23; extra == "polyglot"
|
|
16
17
|
Provides-Extra: keywords
|
|
17
18
|
Requires-Dist: ai-agents; extra == "keywords"
|
|
18
19
|
Requires-Dist: policy; extra == "keywords"
|
|
@@ -60,7 +61,7 @@ install test.
|
|
|
60
61
|
| --- | --- | --- | --- |
|
|
61
62
|
| Python | Yes | Yes, inserts `_writ_check` gates | Parsed with the stdlib `ast`, no extra needed |
|
|
62
63
|
| TypeScript / JavaScript | Yes, with `pywrit[polyglot]` | No, scan only | Needs the extra: `pip install 'pywrit[polyglot]'`. Without it, the report says so loudly: a warning block names the unscanned files instead of reporting zero findings |
|
|
63
|
-
| Go |
|
|
64
|
+
| Go | Yes, with `pywrit[polyglot]` | No, scan only | First detection family only: database/sql Exec/ExecContext, GORM Create/Save/Update/Updates/Delete, HTTP client writes and http.NewRequest with a write method, generated-client <client>.<Resource>.<New\|Update\|Delete\|Create>; package-level generated clients not yet covered. Without the extra, the report names the unscanned Go files in the same warning block as TS/JS |
|
|
64
65
|
|
|
65
66
|
## 60-second quickstart: scan → wrap → gate
|
|
66
67
|
|
|
@@ -184,6 +185,14 @@ pipeline (twilio-style generated clients, e.g. `twilio-node`).
|
|
|
184
185
|
|
|
185
186
|

|
|
186
187
|
|
|
188
|
+
### Go (scan-only)
|
|
189
|
+
|
|
190
|
+
`writ scan` also finds write sites in Go. It needs the optional extra (tree-sitter based):
|
|
191
|
+
|
|
192
|
+
pip install 'pywrit[polyglot]'
|
|
193
|
+
|
|
194
|
+
Covered in this first family: `database/sql` `Exec` / `ExecContext`, GORM `Create` / `Save` / `Update` / `Updates` / `Delete`, HTTP client `Post` / `Put` / `Patch` / `Delete` and `http.NewRequest` / `NewRequestWithContext` with a write method, and generated-client calls shaped `<client>.<Resource>.<New|Update|Delete|Create>` with an uppercase resource segment (the stripe-go shape). GET/HEAD requests, `db.Query` / `QueryRow`, and bare constructors are not flagged. `_test.go` files and `vendor/` trees are skipped. Go is **scan-only**: `writ scan --apply` never rewrites `.go` files, gate those by hand. Not yet covered: package-level generated clients (`charge.New(...)`, the pre-v87 stripe-go shape), request-pipeline calls with a variable method, and raw-SQL helpers.
|
|
195
|
+
|
|
187
196
|
## GitHub Action
|
|
188
197
|
|
|
189
198
|
Scan every pull request for ungated write sites. Findings land as check
|
|
@@ -38,7 +38,7 @@ install test.
|
|
|
38
38
|
| --- | --- | --- | --- |
|
|
39
39
|
| Python | Yes | Yes, inserts `_writ_check` gates | Parsed with the stdlib `ast`, no extra needed |
|
|
40
40
|
| TypeScript / JavaScript | Yes, with `pywrit[polyglot]` | No, scan only | Needs the extra: `pip install 'pywrit[polyglot]'`. Without it, the report says so loudly: a warning block names the unscanned files instead of reporting zero findings |
|
|
41
|
-
| Go |
|
|
41
|
+
| Go | Yes, with `pywrit[polyglot]` | No, scan only | First detection family only: database/sql Exec/ExecContext, GORM Create/Save/Update/Updates/Delete, HTTP client writes and http.NewRequest with a write method, generated-client <client>.<Resource>.<New\|Update\|Delete\|Create>; package-level generated clients not yet covered. Without the extra, the report names the unscanned Go files in the same warning block as TS/JS |
|
|
42
42
|
|
|
43
43
|
## 60-second quickstart: scan → wrap → gate
|
|
44
44
|
|
|
@@ -162,6 +162,14 @@ pipeline (twilio-style generated clients, e.g. `twilio-node`).
|
|
|
162
162
|
|
|
163
163
|

|
|
164
164
|
|
|
165
|
+
### Go (scan-only)
|
|
166
|
+
|
|
167
|
+
`writ scan` also finds write sites in Go. It needs the optional extra (tree-sitter based):
|
|
168
|
+
|
|
169
|
+
pip install 'pywrit[polyglot]'
|
|
170
|
+
|
|
171
|
+
Covered in this first family: `database/sql` `Exec` / `ExecContext`, GORM `Create` / `Save` / `Update` / `Updates` / `Delete`, HTTP client `Post` / `Put` / `Patch` / `Delete` and `http.NewRequest` / `NewRequestWithContext` with a write method, and generated-client calls shaped `<client>.<Resource>.<New|Update|Delete|Create>` with an uppercase resource segment (the stripe-go shape). GET/HEAD requests, `db.Query` / `QueryRow`, and bare constructors are not flagged. `_test.go` files and `vendor/` trees are skipped. Go is **scan-only**: `writ scan --apply` never rewrites `.go` files, gate those by hand. Not yet covered: package-level generated clients (`charge.New(...)`, the pre-v87 stripe-go shape), request-pipeline calls with a variable method, and raw-SQL helpers.
|
|
172
|
+
|
|
165
173
|
## GitHub Action
|
|
166
174
|
|
|
167
175
|
Scan every pull request for ungated write sites. Findings land as check
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pywrit"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.14"
|
|
8
8
|
description = "Python client + CLI for Writ — the gate before the write"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
@@ -12,12 +12,13 @@ requires-python = ">=3.9"
|
|
|
12
12
|
dependencies = ["httpx>=0.27"]
|
|
13
13
|
|
|
14
14
|
[project.optional-dependencies]
|
|
15
|
-
# `pip install 'pywrit[polyglot]'`: lets `writ scan` parse TypeScript
|
|
16
|
-
# JavaScript (scan only). Python scanning never needs it.
|
|
15
|
+
# `pip install 'pywrit[polyglot]'`: lets `writ scan` parse TypeScript,
|
|
16
|
+
# JavaScript, and Go (scan only). Python scanning never needs it.
|
|
17
17
|
polyglot = [
|
|
18
18
|
"tree-sitter>=0.23",
|
|
19
19
|
"tree-sitter-typescript>=0.23",
|
|
20
20
|
"tree-sitter-javascript>=0.23",
|
|
21
|
+
"tree-sitter-go>=0.23",
|
|
21
22
|
]
|
|
22
23
|
keywords = ["ai-agents", "policy", "audit-log", "guardrails"]
|
|
23
24
|
|
|
@@ -660,14 +660,21 @@ def _writ_score(rows):
|
|
|
660
660
|
return max(0, 100 - penalty)
|
|
661
661
|
|
|
662
662
|
|
|
663
|
-
def print_score_report(root, sites, gated_funcs, unscanned_tsjs=0
|
|
663
|
+
def print_score_report(root, sites, gated_funcs, unscanned_tsjs=0,
|
|
664
|
+
polyglot_sites=0):
|
|
664
665
|
"""Print the risk report and Writ Score. Pure read-only: no files written.
|
|
665
666
|
|
|
666
|
-
unscanned_tsjs counts TS/JS
|
|
667
|
-
scanned (polyglot extra missing). When it is
|
|
668
|
-
write was found, the verdict says so: "No write
|
|
669
|
-
discovered" on its own would read as a clean result for a
|
|
670
|
-
scanner only half saw.
|
|
667
|
+
unscanned_tsjs counts polyglot files (TS/JS and Go) that were
|
|
668
|
+
collected but never scanned (polyglot extra missing). When it is
|
|
669
|
+
nonzero and no Python write was found, the verdict says so: "No write
|
|
670
|
+
operations discovered" on its own would read as a clean result for a
|
|
671
|
+
repo the scanner only half saw.
|
|
672
|
+
|
|
673
|
+
polyglot_sites counts listed TS/JS and Go write sites. The score and
|
|
674
|
+
the verdict grade Python writes only, so when polyglot sites exist
|
|
675
|
+
and no Python write was found, the bare verdict must not stand
|
|
676
|
+
either: it reads as if the repo had no writes at all. It is qualified
|
|
677
|
+
to Python instead.
|
|
671
678
|
"""
|
|
672
679
|
rows = _score_report_rows(root, sites, gated_funcs)
|
|
673
680
|
total = len(rows)
|
|
@@ -686,6 +693,8 @@ def print_score_report(root, sites, gated_funcs, unscanned_tsjs=0):
|
|
|
686
693
|
if not total:
|
|
687
694
|
if unscanned_tsjs:
|
|
688
695
|
print("No write operations discovered in the files that were scanned.")
|
|
696
|
+
elif polyglot_sites:
|
|
697
|
+
print("No Python write operations discovered.")
|
|
689
698
|
else:
|
|
690
699
|
print("No write operations discovered.")
|
|
691
700
|
print()
|
|
@@ -837,7 +846,8 @@ def _apply_edits(lines, edits):
|
|
|
837
846
|
# writ scan, TypeScript/JavaScript: deterministic and scan-only (TS/JS files
|
|
838
847
|
# are never rewritten). Parsing needs the optional extra
|
|
839
848
|
# `pip install 'pywrit[polyglot]'` (tree-sitter); without it the scanner
|
|
840
|
-
# prints a
|
|
849
|
+
# prints a prominent warning block and keeps going (Go files ride the same
|
|
850
|
+
# extra and the same block; see the Go tier below). Rules are data (_TSJS_RULES) so
|
|
841
851
|
# other languages can reuse the same (id, kind, callee patterns, condition)
|
|
842
852
|
# shape.
|
|
843
853
|
# ---------------------------------------------------------------------------
|
|
@@ -900,18 +910,33 @@ def _tsjs_suppressed_total(suppressed):
|
|
|
900
910
|
_TSJS_INSTALL_CMD = "pip install 'pywrit[polyglot]'"
|
|
901
911
|
|
|
902
912
|
|
|
903
|
-
def _tsjs_missing_extra_block(n_files):
|
|
904
|
-
"""Prominent block for the no-polyglot-extra state: TS/JS
|
|
905
|
-
collected but none were scanned, so zero
|
|
906
|
-
gap, not a clean result. Printed in both the normal and
|
|
907
|
-
always before the GitHub App line (which stays last).
|
|
913
|
+
def _tsjs_missing_extra_block(n_files, n_go_files=0):
|
|
914
|
+
"""Prominent block for the no-polyglot-extra state: TS/JS (and Go)
|
|
915
|
+
files were collected but none were scanned, so zero polyglot findings
|
|
916
|
+
is a coverage gap, not a clean result. Printed in both the normal and
|
|
917
|
+
--score paths, always before the GitHub App line (which stays last).
|
|
918
|
+
|
|
919
|
+
The Go count rides the same block so a Go-only repo without the extra
|
|
920
|
+
shows its files went unscanned, so the block never reads as though zero
|
|
921
|
+
Go findings meant a clean result."""
|
|
908
922
|
bar = " " + "-" * 64
|
|
923
|
+
if n_files and n_go_files:
|
|
924
|
+
seen = " WARNING: %d TS/JS file(s) and %d Go file(s) were seen " \
|
|
925
|
+
"but none were scanned." % (n_files, n_go_files)
|
|
926
|
+
covered = " Python only. Zero TS/JS or Go findings is a coverage gap, not a"
|
|
927
|
+
elif n_go_files:
|
|
928
|
+
seen = " WARNING: %d Go file(s) were seen but none were scanned." \
|
|
929
|
+
% n_go_files
|
|
930
|
+
covered = " Python only. Zero Go findings is a coverage gap, not a"
|
|
931
|
+
else:
|
|
932
|
+
seen = " WARNING: %d TS/JS file(s) were seen but none were scanned." \
|
|
933
|
+
% n_files
|
|
934
|
+
covered = " Python only. Zero TS/JS findings is a coverage gap, not a"
|
|
909
935
|
return [
|
|
910
936
|
bar,
|
|
911
|
-
|
|
912
|
-
% n_files,
|
|
937
|
+
seen,
|
|
913
938
|
" The polyglot extra is not installed, so this report covers",
|
|
914
|
-
|
|
939
|
+
covered,
|
|
915
940
|
" clean result.",
|
|
916
941
|
" Install it with: " + _TSJS_INSTALL_CMD,
|
|
917
942
|
bar,
|
|
@@ -1370,6 +1395,379 @@ def _tsjs_scan_source(src, filename, parser):
|
|
|
1370
1395
|
return sites, gated
|
|
1371
1396
|
|
|
1372
1397
|
|
|
1398
|
+
# writ scan, Go (slice 1): deterministic and scan-only (Go files are never
|
|
1399
|
+
# rewritten). Same tree-sitter tier as TS/JS, same `pywrit[polyglot]`
|
|
1400
|
+
# extra plus the tree-sitter-go grammar; without the extra the
|
|
1401
|
+
# missing-extra block counts Go files too. Slice 1 covers one write
|
|
1402
|
+
# family, deliberately: database/sql Exec/ExecContext on a db/tx handle,
|
|
1403
|
+
# GORM-shaped Create/Save/Update/Updates/Delete on a db handle, HTTP
|
|
1404
|
+
# client writes (Post/Put/Patch/Delete calls and NewRequest(-WithContext)
|
|
1405
|
+
# with a write method), and generated-client resource methods of the
|
|
1406
|
+
# stripe-go shape (<client>.<Resource>.<New|Update|Delete|Create> with an
|
|
1407
|
+
# uppercase middle segment). Wider Go coverage (os/io/fs writes, sqlx,
|
|
1408
|
+
# raw-SQL conditions on Exec, non-stripe generated-client shapes,
|
|
1409
|
+
# Go-specific non-product suppression classes) ships in later slices.
|
|
1410
|
+
# ---------------------------------------------------------------------------
|
|
1411
|
+
|
|
1412
|
+
_GO_MAX_BYTES = 1000000 # larger files are almost always generated
|
|
1413
|
+
_GO_TEST_DIRS = {"tests", "testing"}
|
|
1414
|
+
|
|
1415
|
+
# chain[0] in one of these keeps its case: the selector names a package,
|
|
1416
|
+
# not a variable. Package roots are package-level rules' anchor;
|
|
1417
|
+
# everything else (db, tx, client, receiver fields like r.db) lowercases,
|
|
1418
|
+
# so a variable or field never collides with a package- or
|
|
1419
|
+
# resource-segment rule.
|
|
1420
|
+
_GO_PKG_ROOTS = {"http", "sql", "gorm"}
|
|
1421
|
+
|
|
1422
|
+
# Classifier noise guards. Uppercase two-segment selectors (s.Info.
|
|
1423
|
+
# Update) are ubiquitous in real code (structs like Params, Args, Options,
|
|
1424
|
+
# Config); the stripe-go generated-client shape is three segments
|
|
1425
|
+
# (client.Charges.New), so two-segment chains never fire the sdk rule.
|
|
1426
|
+
# The package-level pre-v87 shape (charge.New(params)) is NOT flagged in
|
|
1427
|
+
# slice 1: telling it apart from a bare constructor (paymentsvc.New)
|
|
1428
|
+
# needs import-aware detection, which is a later slice; see
|
|
1429
|
+
# tests/fixtures/go/pkgclient.go.
|
|
1430
|
+
_GO_STRUCTY_RES = {"params", "args", "arg", "opts", "options", "config",
|
|
1431
|
+
"conf", "cfg", "settings", "props", "attrs", "info",
|
|
1432
|
+
"meta", "metadata", "stat", "stats", "status", "flags",
|
|
1433
|
+
"defaults", "spec", "state", "data", "result", "results",
|
|
1434
|
+
"resp", "response", "req", "request", "ctx", "context",
|
|
1435
|
+
"env", "log", "msg", "message", "err", "error",
|
|
1436
|
+
"value", "values", "item", "items", "node", "list",
|
|
1437
|
+
"set", "map", "dict", "obj", "object", "doc", "self",
|
|
1438
|
+
"this", "types", "type", "kind", "id", "uid", "key",
|
|
1439
|
+
"name", "path", "url", "uri", "queue", "pool", "lock",
|
|
1440
|
+
"group", "batch", "builder", "factory", "mock", "fake",
|
|
1441
|
+
"test", "tmp", "tmpfs", "base", "core", "common",
|
|
1442
|
+
"util", "utils", "helper", "helpers", "internal"}
|
|
1443
|
+
|
|
1444
|
+
_GO_SQL_METHODS = {"exec", "execcontext"}
|
|
1445
|
+
_GO_ORM_METHODS = {"create", "save", "update", "updates", "delete"}
|
|
1446
|
+
_GO_HTTP_VERBS = {"post", "put", "patch", "delete"}
|
|
1447
|
+
_GO_NEW_REQUEST = {"newrequest", "newrequestwithcontext"}
|
|
1448
|
+
_GO_HTTP_WRITE_METHODS = {"post", "put", "patch", "delete"}
|
|
1449
|
+
_GO_RESOURCE_METHODS = {"new", "update", "delete", "create"}
|
|
1450
|
+
# db/tx handle receivers for database/sql and GORM-shaped writes. A
|
|
1451
|
+
# receiver segment qualifies when it names a handle by convention: it is
|
|
1452
|
+
# or ends in "db"/"tx" (db, tx, gormDB, dbtx, r.db, s.tx). Function roots
|
|
1453
|
+
# (createUser(...)) never qualify: "user" does not end in db/tx.
|
|
1454
|
+
_GO_HANDLE_RX = _re.compile(r"(db|tx)$")
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
class _GoFile:
|
|
1458
|
+
def __init__(self, src):
|
|
1459
|
+
self.src = src
|
|
1460
|
+
|
|
1461
|
+
def text(self, node):
|
|
1462
|
+
return self.src[node.start_byte:node.end_byte].decode(
|
|
1463
|
+
"utf-8", "replace")
|
|
1464
|
+
|
|
1465
|
+
def literal(self, node):
|
|
1466
|
+
"""String value of a Go interpreted/raw string literal, else None."""
|
|
1467
|
+
if node is None:
|
|
1468
|
+
return None
|
|
1469
|
+
if node.type in ("interpreted_string_literal",
|
|
1470
|
+
"raw_string_literal"):
|
|
1471
|
+
return self.text(node)[1:-1]
|
|
1472
|
+
return None
|
|
1473
|
+
|
|
1474
|
+
def chain(self, node):
|
|
1475
|
+
"""Dotted callee chain, e.g. http.NewRequest -> [http, NewRequest],
|
|
1476
|
+
client.Charges.New(...) -> [client, Charges, New()]. Selector
|
|
1477
|
+
fields and plain identifiers only; anything else on the operand
|
|
1478
|
+
side (index expressions, parenthesized values, composite-literal
|
|
1479
|
+
or nested-call receivers) folds to "?" so a rule only fires on a
|
|
1480
|
+
chain it fully understands."""
|
|
1481
|
+
if node is None:
|
|
1482
|
+
return ["?"]
|
|
1483
|
+
t = node.type
|
|
1484
|
+
if t == "identifier":
|
|
1485
|
+
return [self.text(node)]
|
|
1486
|
+
if t == "selector_expression":
|
|
1487
|
+
fld = node.child_by_field_name("field")
|
|
1488
|
+
return self.chain(node.child_by_field_name("operand")) + \
|
|
1489
|
+
[self.text(fld) if fld is not None else "?"]
|
|
1490
|
+
if t == "call_expression":
|
|
1491
|
+
c = self.chain(node.child_by_field_name("function"))
|
|
1492
|
+
c[-1] = c[-1] + "()"
|
|
1493
|
+
return c
|
|
1494
|
+
return ["?"]
|
|
1495
|
+
|
|
1496
|
+
|
|
1497
|
+
def _go_args(call):
|
|
1498
|
+
args = call.child_by_field_name("arguments")
|
|
1499
|
+
if args is None:
|
|
1500
|
+
return []
|
|
1501
|
+
return list(args.named_children)
|
|
1502
|
+
|
|
1503
|
+
|
|
1504
|
+
def _go_norm(chain):
|
|
1505
|
+
"""Case-drop a callee chain for matching. Chain roots that name a Go
|
|
1506
|
+
package (_GO_PKG_ROOTS) keep the package name as segment 0 and
|
|
1507
|
+
lowercase the selector fields; every other root (variables, receiver
|
|
1508
|
+
fields) and all "()" call segments lowercase wholesale. "?" segments
|
|
1509
|
+
are dropped; a chain that is only "?" matches nothing."""
|
|
1510
|
+
norm = []
|
|
1511
|
+
for i, c in enumerate(chain):
|
|
1512
|
+
if c == "?":
|
|
1513
|
+
continue
|
|
1514
|
+
if i == 0 and c in _GO_PKG_ROOTS:
|
|
1515
|
+
norm.append(c)
|
|
1516
|
+
else:
|
|
1517
|
+
norm.append(c.lower().rstrip("()") if c.endswith("()")
|
|
1518
|
+
else c.lower())
|
|
1519
|
+
return norm
|
|
1520
|
+
|
|
1521
|
+
|
|
1522
|
+
def _go_write_method(call, f, idx):
|
|
1523
|
+
"""arg[idx] of a NewRequest call is a write method: either a string
|
|
1524
|
+
literal ("POST") or an http.MethodX constant (http.MethodPost).
|
|
1525
|
+
Variables and http.MethodGet/Head are reads, not flagged."""
|
|
1526
|
+
args = _go_args(call)
|
|
1527
|
+
if len(args) <= idx:
|
|
1528
|
+
return False
|
|
1529
|
+
a = args[idx]
|
|
1530
|
+
lit = f.literal(a)
|
|
1531
|
+
if lit is not None:
|
|
1532
|
+
return lit.strip().lower() in _GO_HTTP_WRITE_METHODS
|
|
1533
|
+
ac = _go_norm(f.chain(a))
|
|
1534
|
+
return len(ac) == 2 and ac[0] == "http" and \
|
|
1535
|
+
ac[1].startswith("method") and \
|
|
1536
|
+
ac[1][len("method"):] in _GO_HTTP_WRITE_METHODS
|
|
1537
|
+
|
|
1538
|
+
|
|
1539
|
+
def _go_classify(call, chain, f, pkg_paths=None):
|
|
1540
|
+
"""First match wins; returns (rule_id, kind). Slice-1 family only.
|
|
1541
|
+
|
|
1542
|
+
`chain` is the raw (un-lowercased) callee chain. Handle receivers
|
|
1543
|
+
match by convention on any receiver segment (db, tx, gormDB, r.db,
|
|
1544
|
+
s.client); package roots match only through _GO_PKG_ROOTS or the
|
|
1545
|
+
file's imports, so a variable or field never trips a package-level
|
|
1546
|
+
pattern, and a bare `svc.New(...)` constructor never matches the
|
|
1547
|
+
generated-client rule (its resource segment must pass the shape
|
|
1548
|
+
checks below)."""
|
|
1549
|
+
norm = _go_norm(chain)
|
|
1550
|
+
if not norm:
|
|
1551
|
+
return None
|
|
1552
|
+
tail = norm[-1]
|
|
1553
|
+
recv = norm[:-1]
|
|
1554
|
+
is_pkg_root = chain[0] in _GO_PKG_ROOTS or \
|
|
1555
|
+
(pkg_paths is not None and chain[0] in pkg_paths)
|
|
1556
|
+
if not is_pkg_root and recv and \
|
|
1557
|
+
any(_GO_HANDLE_RX.search(c) for c in recv):
|
|
1558
|
+
if tail in _GO_SQL_METHODS:
|
|
1559
|
+
# database/sql: db.Exec / db.ExecContext, tx.Exec, r.db.Exec
|
|
1560
|
+
return "go-db-write", "db"
|
|
1561
|
+
if tail in _GO_ORM_METHODS:
|
|
1562
|
+
# GORM shape: db.Create / Save / Update / Updates / Delete
|
|
1563
|
+
return "go-orm-write", "db"
|
|
1564
|
+
if tail in _GO_HTTP_VERBS:
|
|
1565
|
+
if norm[0] == "http" and len(norm) == 2:
|
|
1566
|
+
# package-level http.Post / Put / Patch / Delete
|
|
1567
|
+
return "go-http-client-write", "http"
|
|
1568
|
+
if not is_pkg_root and recv and \
|
|
1569
|
+
any(c == "client" or c.endswith("client") for c in recv):
|
|
1570
|
+
# client.Post / s.client.Delete on an HTTP client handle
|
|
1571
|
+
return "go-http-client-write", "http"
|
|
1572
|
+
if tail in _GO_NEW_REQUEST and norm[0] == "http" and len(norm) == 2:
|
|
1573
|
+
idx = 0 if tail == "newrequest" else 1
|
|
1574
|
+
if _go_write_method(call, f, idx):
|
|
1575
|
+
return "go-http-request-write", "http"
|
|
1576
|
+
if tail in _GO_RESOURCE_METHODS and len(chain) >= 3:
|
|
1577
|
+
mid, mid_norm = chain[-2], norm[-2]
|
|
1578
|
+
if mid != "?" and mid_norm not in _GO_STRUCTY_RES:
|
|
1579
|
+
if mid[0].isupper():
|
|
1580
|
+
# generated client, stripe-go shape: <recv>.<Resource>.
|
|
1581
|
+
# <New|Update|Delete|Create>(...) with an uppercase
|
|
1582
|
+
# Resource segment; a bare `svc.New(...)` constructor has
|
|
1583
|
+
# only two segments and never matches. The package-level
|
|
1584
|
+
# pre-v87 shape (charge.New) is a later slice, not this
|
|
1585
|
+
# branch: two-segment chains never reach it.
|
|
1586
|
+
return "go-sdk-write", "sdk"
|
|
1587
|
+
return None
|
|
1588
|
+
|
|
1589
|
+
|
|
1590
|
+
def _go_func_name(node, f):
|
|
1591
|
+
name = node.child_by_field_name("name")
|
|
1592
|
+
return f.text(name) if name is not None else None
|
|
1593
|
+
|
|
1594
|
+
|
|
1595
|
+
def _go_method_receiver(node, f):
|
|
1596
|
+
recv = node.child_by_field_name("receiver")
|
|
1597
|
+
if recv is None:
|
|
1598
|
+
return None
|
|
1599
|
+
for decl in recv.named_children:
|
|
1600
|
+
typ = decl.child_by_field_name("type")
|
|
1601
|
+
while typ is not None and typ.type == "pointer_type":
|
|
1602
|
+
typ = typ.child_by_field_name("type")
|
|
1603
|
+
if typ is not None:
|
|
1604
|
+
return f.text(typ)
|
|
1605
|
+
return None
|
|
1606
|
+
|
|
1607
|
+
|
|
1608
|
+
def _go_record_import(node, f, pkg_paths):
|
|
1609
|
+
"""Local package name -> import path. Unnamed imports default to the
|
|
1610
|
+
last path segment (github.com/foo/bar -> bar), like the compiler."""
|
|
1611
|
+
path_node = node.child_by_field_name("path")
|
|
1612
|
+
path = f.literal(path_node)
|
|
1613
|
+
if not path:
|
|
1614
|
+
return
|
|
1615
|
+
name_node = node.child_by_field_name("name")
|
|
1616
|
+
name = f.text(name_node) if name_node is not None else \
|
|
1617
|
+
path.rstrip("/").rsplit("/", 1)[-1]
|
|
1618
|
+
if name in ("_", "."):
|
|
1619
|
+
return
|
|
1620
|
+
pkg_paths[name] = path
|
|
1621
|
+
|
|
1622
|
+
|
|
1623
|
+
def _go_scan_source(src, filename, parser):
|
|
1624
|
+
"""Go twin of _tsjs_scan_source: (sites, gated) where sites =
|
|
1625
|
+
[(_WriteSite, rule_id, fkey)] and gated = {(func_name, func_line)}.
|
|
1626
|
+
Module-level (package-level) writes use func ""."""
|
|
1627
|
+
f = _GoFile(src)
|
|
1628
|
+
tree = parser.parse(src)
|
|
1629
|
+
pkg_paths = {}
|
|
1630
|
+
calls, gated = [], set()
|
|
1631
|
+
stack = [(tree.root_node, ("", 0), "")]
|
|
1632
|
+
while stack:
|
|
1633
|
+
node, fkey, cls = stack.pop()
|
|
1634
|
+
t = node.type
|
|
1635
|
+
if t in ("function_declaration", "method_declaration",
|
|
1636
|
+
"func_literal"):
|
|
1637
|
+
name = _go_func_name(node, f) if t != "func_literal" else None
|
|
1638
|
+
recv = None
|
|
1639
|
+
if t == "method_declaration":
|
|
1640
|
+
recv = _go_method_receiver(node, f)
|
|
1641
|
+
if recv:
|
|
1642
|
+
cls = recv
|
|
1643
|
+
if name:
|
|
1644
|
+
fkey = (name, node.start_point[0] + 1)
|
|
1645
|
+
elif t == "import_spec":
|
|
1646
|
+
_go_record_import(node, f, pkg_paths)
|
|
1647
|
+
elif t == "call_expression":
|
|
1648
|
+
calls.append((node, fkey, cls))
|
|
1649
|
+
stack.extend((c, fkey, cls) for c in reversed(node.children))
|
|
1650
|
+
sites = []
|
|
1651
|
+
for node, fkey, cls in calls:
|
|
1652
|
+
raw = f.chain(node.child_by_field_name("function"))
|
|
1653
|
+
if any(c == "?" for c in raw[:-1]):
|
|
1654
|
+
continue # opaque receiver: index/composite/nested-call
|
|
1655
|
+
if not raw or raw[-1] == "?":
|
|
1656
|
+
continue
|
|
1657
|
+
hit = _go_classify(node, raw, f, pkg_paths)
|
|
1658
|
+
if hit:
|
|
1659
|
+
snippet = " ".join(f.text(node).split())[:100]
|
|
1660
|
+
sites.append((_WriteSite(filename, fkey[0], cls,
|
|
1661
|
+
node.start_point[0] + 1, hit[1],
|
|
1662
|
+
snippet),
|
|
1663
|
+
hit[0], fkey))
|
|
1664
|
+
return sites, gated
|
|
1665
|
+
|
|
1666
|
+
|
|
1667
|
+
def _go_verb(rel, func_name, cls_name, kind):
|
|
1668
|
+
"""infer_verb for Go: CamelCase names, `payments/refunds.go` ->
|
|
1669
|
+
payments. A leading snake_case verb prefix (createUser -> create)
|
|
1670
|
+
still lands through _infer_action after camel splitting."""
|
|
1671
|
+
snake = _re.sub(r"(?<=[a-z0-9])(?=[A-Z])", "_", func_name)
|
|
1672
|
+
base = rel.with_name(rel.name.split(".")[0] + rel.suffix)
|
|
1673
|
+
return infer_verb(base, snake, cls_name, kind)
|
|
1674
|
+
|
|
1675
|
+
|
|
1676
|
+
def _go_parsers():
|
|
1677
|
+
"""Return {"go": Parser}, or {} when the polyglot extra or the
|
|
1678
|
+
tree-sitter-go grammar is missing."""
|
|
1679
|
+
try:
|
|
1680
|
+
import tree_sitter as ts
|
|
1681
|
+
except ImportError:
|
|
1682
|
+
return {}
|
|
1683
|
+
try:
|
|
1684
|
+
try:
|
|
1685
|
+
import tree_sitter_go as tsg
|
|
1686
|
+
fn = tsg.language
|
|
1687
|
+
except ImportError:
|
|
1688
|
+
from tree_sitter_language_pack import get_language
|
|
1689
|
+
fn = lambda: get_language("go")
|
|
1690
|
+
except ImportError:
|
|
1691
|
+
return {}
|
|
1692
|
+
try:
|
|
1693
|
+
lang = fn()
|
|
1694
|
+
if not isinstance(lang, ts.Language):
|
|
1695
|
+
lang = ts.Language(lang)
|
|
1696
|
+
try:
|
|
1697
|
+
parser = ts.Parser(lang)
|
|
1698
|
+
except TypeError:
|
|
1699
|
+
parser = ts.Parser()
|
|
1700
|
+
parser.language = lang
|
|
1701
|
+
except Exception:
|
|
1702
|
+
return {}
|
|
1703
|
+
return {"go": parser}
|
|
1704
|
+
|
|
1705
|
+
|
|
1706
|
+
def _go_should_skip(path, root, excludes):
|
|
1707
|
+
rel = path.relative_to(root)
|
|
1708
|
+
if any(p in _TSJS_SKIP_DIR_NAMES or p.startswith(".")
|
|
1709
|
+
for p in rel.parts[:-1]):
|
|
1710
|
+
return True
|
|
1711
|
+
if any(p in _GO_TEST_DIRS for p in rel.parts[:-1]):
|
|
1712
|
+
return True
|
|
1713
|
+
if path.name.endswith("_test.go"):
|
|
1714
|
+
return True
|
|
1715
|
+
s = str(path)
|
|
1716
|
+
return any(e in s for e in excludes)
|
|
1717
|
+
|
|
1718
|
+
|
|
1719
|
+
def _go_collect_files(root, excludes):
|
|
1720
|
+
"""Same git-aware walk as TS/JS (tracked + untracked-not-ignored, or
|
|
1721
|
+
os.walk with the same skip dirs), minus test files and vendor/ trees
|
|
1722
|
+
(vendor/ is already in _TSJS_SKIP_DIR_NAMES)."""
|
|
1723
|
+
rels = _tsjs_git_files(root)
|
|
1724
|
+
if rels is None:
|
|
1725
|
+
rels = []
|
|
1726
|
+
for dp, dns, fns in os.walk(str(root)):
|
|
1727
|
+
dns[:] = [d for d in dns if d not in _TSJS_SKIP_DIR_NAMES
|
|
1728
|
+
and not d.startswith(".")]
|
|
1729
|
+
rels.extend(os.path.relpath(os.path.join(dp, f), str(root))
|
|
1730
|
+
for f in fns)
|
|
1731
|
+
out = set()
|
|
1732
|
+
for r in rels:
|
|
1733
|
+
p = root / r
|
|
1734
|
+
if p.suffix == ".go" and not _go_should_skip(p, root, excludes) \
|
|
1735
|
+
and p.is_file():
|
|
1736
|
+
out.add(p)
|
|
1737
|
+
return sorted(out)
|
|
1738
|
+
|
|
1739
|
+
|
|
1740
|
+
def _go_scan(root, excludes):
|
|
1741
|
+
"""Return dict with files, skipped, sites, gated, missing_extra.
|
|
1742
|
+
Never raises on a bad file: a Go file that fails to parse is skipped,
|
|
1743
|
+
never fatal. Go findings are always reported (no non-product
|
|
1744
|
+
suppression classes in slice 1; the go JSON object carries
|
|
1745
|
+
`suppressed` only when a later slice adds them)."""
|
|
1746
|
+
files = _go_collect_files(root, excludes)
|
|
1747
|
+
res = {"files": files, "skipped": [], "sites": [], "gated": set(),
|
|
1748
|
+
"missing_extra": False}
|
|
1749
|
+
if not files:
|
|
1750
|
+
return res
|
|
1751
|
+
parsers = _go_parsers()
|
|
1752
|
+
if not parsers:
|
|
1753
|
+
res["missing_extra"] = True
|
|
1754
|
+
return res
|
|
1755
|
+
for path in files:
|
|
1756
|
+
try:
|
|
1757
|
+
if path.stat().st_size > _GO_MAX_BYTES:
|
|
1758
|
+
res["skipped"].append(str(path))
|
|
1759
|
+
continue
|
|
1760
|
+
src = path.read_bytes()
|
|
1761
|
+
sites, gated = _go_scan_source(src, str(path), parsers["go"])
|
|
1762
|
+
except Exception:
|
|
1763
|
+
res["skipped"].append(str(path))
|
|
1764
|
+
continue
|
|
1765
|
+
res["sites"].extend(sites)
|
|
1766
|
+
res["gated"].update((str(path),) + g for g in gated)
|
|
1767
|
+
return res
|
|
1768
|
+
|
|
1769
|
+
|
|
1770
|
+
|
|
1373
1771
|
def _tsjs_verb(rel, func_name, cls_name, kind):
|
|
1374
1772
|
"""infer_verb for TS/JS: camelCase names, `orders.service.ts` -> orders."""
|
|
1375
1773
|
snake = _re.sub(r"(?<=[a-z0-9])(?=[A-Z])", "_", func_name)
|
|
@@ -1424,7 +1822,8 @@ def _tsjs_scan(root, excludes, include_non_product=False):
|
|
|
1424
1822
|
_RISK_ORDER = {"high": 0, "medium": 1, "low": 2}
|
|
1425
1823
|
|
|
1426
1824
|
|
|
1427
|
-
def _scan_findings_json(root, sites, gated_funcs, tsjs, ts_sites
|
|
1825
|
+
def _scan_findings_json(root, sites, gated_funcs, tsjs, ts_sites,
|
|
1826
|
+
go=None, go_sites=None):
|
|
1428
1827
|
"""Return the findings list for `writ scan --format json`.
|
|
1429
1828
|
|
|
1430
1829
|
Each finding is a dict with file (repo-relative, posix), line, function,
|
|
@@ -1459,27 +1858,47 @@ def _scan_findings_json(root, sites, gated_funcs, tsjs, ts_sites):
|
|
|
1459
1858
|
"language": "tsjs",
|
|
1460
1859
|
"snippet": (s.snippet or "")[:200],
|
|
1461
1860
|
})
|
|
1861
|
+
for s, _rid, fkey in (go_sites or []):
|
|
1862
|
+
rel = _pathlib.Path(s.file).relative_to(root)
|
|
1863
|
+
verb = _go_verb(rel, fkey[0], s.cls, s.kind)
|
|
1864
|
+
findings.append({
|
|
1865
|
+
"file": rel.as_posix(),
|
|
1866
|
+
"line": s.lineno,
|
|
1867
|
+
"function": fkey[0] or "<module>",
|
|
1868
|
+
"verb": verb,
|
|
1869
|
+
"risk": _risk_tier(verb),
|
|
1870
|
+
"gated": (s.file,) + fkey in (go or {}).get("gated", set()),
|
|
1871
|
+
"kind": s.kind,
|
|
1872
|
+
"language": "go",
|
|
1873
|
+
"snippet": (s.snippet or "")[:200],
|
|
1874
|
+
})
|
|
1462
1875
|
findings.sort(key=lambda f: (_RISK_ORDER[f["risk"]], f["file"], f["line"]))
|
|
1463
1876
|
return findings
|
|
1464
1877
|
|
|
1465
1878
|
|
|
1466
|
-
def print_scan_json(root, files, skipped, sites, gated_funcs, tsjs, ts_sites
|
|
1879
|
+
def print_scan_json(root, files, skipped, sites, gated_funcs, tsjs, ts_sites,
|
|
1880
|
+
go=None, go_sites=None):
|
|
1467
1881
|
"""Print the machine-readable scan report. Read-only: no files written."""
|
|
1468
1882
|
from . import __version__
|
|
1883
|
+
go = go or {"files": [], "skipped": [], "missing_extra": False}
|
|
1884
|
+
go_sites = go_sites or []
|
|
1469
1885
|
doc = {
|
|
1470
1886
|
"scanner": "pywrit",
|
|
1471
1887
|
"version": __version__,
|
|
1472
1888
|
"root": str(root),
|
|
1473
|
-
"files_scanned": len(files) + len(tsjs.get("files", []))
|
|
1474
|
-
|
|
1889
|
+
"files_scanned": len(files) + len(tsjs.get("files", []))
|
|
1890
|
+
+ len(go.get("files", [])),
|
|
1891
|
+
"files_skipped": len(skipped) + len(tsjs.get("skipped", []))
|
|
1892
|
+
+ len(go.get("skipped", [])),
|
|
1475
1893
|
"tsjs_hint": bool(tsjs.get("missing_extra")),
|
|
1894
|
+
"go_hint": bool(go.get("missing_extra")),
|
|
1476
1895
|
"github_app": {
|
|
1477
1896
|
"install_url": _GITHUB_APP_INSTALL_URL,
|
|
1478
1897
|
"docs_url": _GITHUB_APP_DOCS_URL,
|
|
1479
1898
|
"note": _GITHUB_APP_NOTE,
|
|
1480
1899
|
},
|
|
1481
1900
|
"findings": _scan_findings_json(root, sites, gated_funcs, tsjs,
|
|
1482
|
-
ts_sites),
|
|
1901
|
+
ts_sites, go, go_sites),
|
|
1483
1902
|
}
|
|
1484
1903
|
ts_suppressed = tsjs.get("suppressed") or {}
|
|
1485
1904
|
ts_sup_total = _tsjs_suppressed_total(ts_suppressed)
|
|
@@ -1505,13 +1924,23 @@ def print_scan_json(root, files, skipped, sites, gated_funcs, tsjs, ts_sites):
|
|
|
1505
1924
|
for c in ts_suppressed.values()
|
|
1506
1925
|
for f in c["files"]),
|
|
1507
1926
|
}
|
|
1927
|
+
if go.get("missing_extra") and go.get("files"):
|
|
1928
|
+
# The extra is missing: Go files were collected but none were
|
|
1929
|
+
# scanned. Machine consumers get the same state the text report
|
|
1930
|
+
# shouts about, instead of a bare go_hint flag.
|
|
1931
|
+
doc["go_not_scanned"] = {
|
|
1932
|
+
"reason": "missing_extra",
|
|
1933
|
+
"extra": "pywrit[polyglot]",
|
|
1934
|
+
"files": len(go["files"]),
|
|
1935
|
+
"install_command": _TSJS_INSTALL_CMD,
|
|
1936
|
+
}
|
|
1508
1937
|
print(json.dumps(doc, indent=2))
|
|
1509
1938
|
|
|
1510
1939
|
|
|
1511
1940
|
def _scan_repo(root, excludes, include_non_product=False):
|
|
1512
|
-
"""Collect Python
|
|
1941
|
+
"""Collect Python, TS/JS, and Go files and find write sites.
|
|
1513
1942
|
|
|
1514
|
-
Returns (files, skipped, sites, gated, tsjs). This is the read-only
|
|
1943
|
+
Returns (files, skipped, sites, gated, tsjs, go). This is the read-only
|
|
1515
1944
|
detection pass reused by both `writ scan` and `writ wrap`.
|
|
1516
1945
|
"""
|
|
1517
1946
|
files = _collect_files(root, excludes or [])
|
|
@@ -1534,15 +1963,25 @@ def _scan_repo(root, excludes, include_non_product=False):
|
|
|
1534
1963
|
|
|
1535
1964
|
tsjs = _tsjs_scan(root, excludes or [],
|
|
1536
1965
|
include_non_product=include_non_product)
|
|
1537
|
-
|
|
1966
|
+
go = _go_scan(root, excludes or [])
|
|
1967
|
+
return files, skipped, sites, gated, tsjs, go
|
|
1538
1968
|
|
|
1539
1969
|
|
|
1540
|
-
def _analyze_scan(root, files, skipped, sites, gated, tsjs):
|
|
1970
|
+
def _analyze_scan(root, files, skipped, sites, gated, tsjs, go=None):
|
|
1541
1971
|
"""Derive counts, the verb map, and gated totals from a scan.
|
|
1542
1972
|
|
|
1543
1973
|
Returns a dict with everything scan_cmd and wrap_cmd need to print
|
|
1544
1974
|
reports, build diffs, and write policy.
|
|
1545
1975
|
"""
|
|
1976
|
+
go = go if go is not None else {"files": [], "skipped": [], "sites": [],
|
|
1977
|
+
"gated": set(), "missing_extra": False}
|
|
1978
|
+
go_sites = go["sites"]
|
|
1979
|
+
go_by_func = {}
|
|
1980
|
+
for s, _rid, fkey in go_sites:
|
|
1981
|
+
go_by_func.setdefault((s.file,) + fkey, []).append(s)
|
|
1982
|
+
go_gated = sum(1 for s, _rid, fkey in go_sites
|
|
1983
|
+
if (s.file,) + fkey in go["gated"])
|
|
1984
|
+
|
|
1546
1985
|
ts_sites = tsjs["sites"]
|
|
1547
1986
|
ts_by_func = {}
|
|
1548
1987
|
for s, _rid, fkey in ts_sites:
|
|
@@ -1567,6 +2006,10 @@ def _analyze_scan(root, files, skipped, sites, gated, tsjs):
|
|
|
1567
2006
|
rel = _pathlib.Path(f).relative_to(root)
|
|
1568
2007
|
verbs.setdefault(_tsjs_verb(rel, func_name, ss[0].cls, ss[0].kind),
|
|
1569
2008
|
"require_grant")
|
|
2009
|
+
for (f, func_name, _ln), ss in sorted(go_by_func.items()):
|
|
2010
|
+
rel = _pathlib.Path(f).relative_to(root)
|
|
2011
|
+
verbs.setdefault(_go_verb(rel, func_name, ss[0].cls, ss[0].kind),
|
|
2012
|
+
"require_grant")
|
|
1570
2013
|
|
|
1571
2014
|
return {
|
|
1572
2015
|
"files": files,
|
|
@@ -1577,6 +2020,10 @@ def _analyze_scan(root, files, skipped, sites, gated, tsjs):
|
|
|
1577
2020
|
"ts_sites": ts_sites,
|
|
1578
2021
|
"ts_by_func": ts_by_func,
|
|
1579
2022
|
"ts_gated": ts_gated,
|
|
2023
|
+
"go": go,
|
|
2024
|
+
"go_sites": go_sites,
|
|
2025
|
+
"go_by_func": go_by_func,
|
|
2026
|
+
"go_gated": go_gated,
|
|
1580
2027
|
"by_func": by_func,
|
|
1581
2028
|
"gated_funcs": gated_funcs,
|
|
1582
2029
|
"gated_site_count": gated_site_count,
|
|
@@ -1647,10 +2094,10 @@ def scan_cmd(args):
|
|
|
1647
2094
|
print("error: not a directory: %s" % args.path, file=sys.stderr)
|
|
1648
2095
|
return 2
|
|
1649
2096
|
|
|
1650
|
-
files, skipped, sites, gated, tsjs = _scan_repo(
|
|
2097
|
+
files, skipped, sites, gated, tsjs, go = _scan_repo(
|
|
1651
2098
|
root, args.exclude or [],
|
|
1652
2099
|
include_non_product=getattr(args, "include_non_product", False))
|
|
1653
|
-
analysis = _analyze_scan(root, files, skipped, sites, gated, tsjs)
|
|
2100
|
+
analysis = _analyze_scan(root, files, skipped, sites, gated, tsjs, go)
|
|
1654
2101
|
by_func = analysis["by_func"]
|
|
1655
2102
|
ts_by_func = analysis["ts_by_func"]
|
|
1656
2103
|
gated_funcs = analysis["gated_funcs"]
|
|
@@ -1660,21 +2107,29 @@ def scan_cmd(args):
|
|
|
1660
2107
|
ts_gated = analysis["ts_gated"]
|
|
1661
2108
|
tsjs = analysis["tsjs"]
|
|
1662
2109
|
ts_sites = analysis["ts_sites"]
|
|
2110
|
+
go = analysis["go"]
|
|
2111
|
+
go_sites = analysis["go_sites"]
|
|
2112
|
+
go_by_func = analysis["go_by_func"]
|
|
2113
|
+
go_gated = analysis["go_gated"]
|
|
1663
2114
|
verbs = analysis["verbs"]
|
|
1664
2115
|
|
|
1665
2116
|
if getattr(args, "format", "text") == "json":
|
|
1666
2117
|
# Machine-readable risk report for CI. Read-only: no policy file,
|
|
1667
2118
|
# no diffs, no prompts.
|
|
1668
2119
|
print_scan_json(root, files, skipped, sites, gated_funcs, tsjs,
|
|
1669
|
-
ts_sites)
|
|
2120
|
+
ts_sites, go, go_sites)
|
|
1670
2121
|
return 0
|
|
1671
2122
|
|
|
2123
|
+
unscanned_polyglot = (len(tsjs["files"]) if tsjs["missing_extra"] else 0) \
|
|
2124
|
+
+ (len(go["files"]) if go["missing_extra"] else 0)
|
|
1672
2125
|
if args.score:
|
|
1673
|
-
unscanned = len(tsjs["files"]) if tsjs["missing_extra"] else 0
|
|
1674
2126
|
print_score_report(root, sites, gated_funcs,
|
|
1675
|
-
unscanned_tsjs=
|
|
1676
|
-
|
|
1677
|
-
|
|
2127
|
+
unscanned_tsjs=unscanned_polyglot,
|
|
2128
|
+
polyglot_sites=len(ts_sites) + len(go_sites))
|
|
2129
|
+
if tsjs["missing_extra"] or go["missing_extra"]:
|
|
2130
|
+
for line in _tsjs_missing_extra_block(
|
|
2131
|
+
len(tsjs["files"]) if tsjs["missing_extra"] else 0,
|
|
2132
|
+
len(go["files"]) if go["missing_extra"] else 0):
|
|
1678
2133
|
print(line)
|
|
1679
2134
|
print(_GITHUB_APP_LINE)
|
|
1680
2135
|
return 0
|
|
@@ -1683,10 +2138,12 @@ def scan_cmd(args):
|
|
|
1683
2138
|
print(" files scanned: %d skipped: %d" % (len(files), len(skipped)))
|
|
1684
2139
|
print(" write sites: %d in %d function(s)" % (total, len(by_func)))
|
|
1685
2140
|
print(" gated: %d/%d (%d%%)" % (gated_site_count, total, pct))
|
|
1686
|
-
if tsjs["missing_extra"]:
|
|
1687
|
-
for line in _tsjs_missing_extra_block(
|
|
2141
|
+
if tsjs["missing_extra"] or go["missing_extra"]:
|
|
2142
|
+
for line in _tsjs_missing_extra_block(
|
|
2143
|
+
len(tsjs["files"]) if tsjs["missing_extra"] else 0,
|
|
2144
|
+
len(go["files"]) if go["missing_extra"] else 0):
|
|
1688
2145
|
print(line)
|
|
1689
|
-
|
|
2146
|
+
if not tsjs["missing_extra"] and tsjs["files"]:
|
|
1690
2147
|
ts_total = len(ts_sites)
|
|
1691
2148
|
print(" ts/js files scanned: %d skipped: %d (scan only, no --apply)"
|
|
1692
2149
|
% (len(tsjs["files"]), len(tsjs["skipped"])))
|
|
@@ -1713,14 +2170,28 @@ def scan_cmd(args):
|
|
|
1713
2170
|
print(" suppressed %s: %d site(s) [%s]" % (
|
|
1714
2171
|
_pathlib.Path(f).relative_to(root),
|
|
1715
2172
|
suppressed[cls]["files"][f], cls))
|
|
2173
|
+
if not go["missing_extra"] and go["files"]:
|
|
2174
|
+
go_total = len(go_sites)
|
|
2175
|
+
print(" go files scanned: %d skipped: %d (scan only, no --apply)"
|
|
2176
|
+
% (len(go["files"]), len(go["skipped"])))
|
|
2177
|
+
print(" go write sites: %d in %d function(s)"
|
|
2178
|
+
% (go_total, len(go_by_func)))
|
|
2179
|
+
print(" go gated: %d/%d (%d%%)" % (
|
|
2180
|
+
go_gated, go_total,
|
|
2181
|
+
(100 * go_gated // go_total) if go_total else 100))
|
|
2182
|
+
for s, rid, _fkey in sorted(go_sites, key=lambda x: (x[0].file,
|
|
2183
|
+
x[0].lineno)):
|
|
2184
|
+
print(" %s:%d [%s] %s %s(): %s" % (
|
|
2185
|
+
_pathlib.Path(s.file).relative_to(root), s.lineno, s.kind,
|
|
2186
|
+
rid, s.func or "<module>", s.snippet))
|
|
1716
2187
|
if verbs:
|
|
1717
2188
|
print(" verbs discovered: %d" % len(verbs))
|
|
1718
2189
|
for vname in sorted(verbs):
|
|
1719
2190
|
print(" %s" % vname)
|
|
1720
2191
|
|
|
1721
2192
|
print_score_report(root, sites, gated_funcs,
|
|
1722
|
-
|
|
1723
|
-
|
|
2193
|
+
unscanned_tsjs=unscanned_polyglot,
|
|
2194
|
+
polyglot_sites=len(ts_sites) + len(go_sites))
|
|
1724
2195
|
|
|
1725
2196
|
if verbs:
|
|
1726
2197
|
ppath = _pathlib.Path(args.policy_out)
|
|
@@ -1734,9 +2205,15 @@ def scan_cmd(args):
|
|
|
1734
2205
|
print(" manual: %s %s() [%s]: %s" % (path, func_name, verb, why))
|
|
1735
2206
|
|
|
1736
2207
|
if not results:
|
|
1737
|
-
if ts_sites:
|
|
2208
|
+
if ts_sites and go_sites:
|
|
2209
|
+
print("nothing to instrument (ts/js and go findings are "
|
|
2210
|
+
"scan-only; gate them by hand).")
|
|
2211
|
+
elif ts_sites:
|
|
1738
2212
|
print("nothing to instrument (ts/js findings are scan-only; "
|
|
1739
2213
|
"gate them by hand).")
|
|
2214
|
+
elif go_sites:
|
|
2215
|
+
print("nothing to instrument (go findings are scan-only; "
|
|
2216
|
+
"gate them by hand).")
|
|
1740
2217
|
else:
|
|
1741
2218
|
print("nothing to instrument.")
|
|
1742
2219
|
print(_GITHUB_APP_LINE)
|
|
@@ -1779,12 +2256,13 @@ def wrap_cmd(args):
|
|
|
1779
2256
|
return 2
|
|
1780
2257
|
|
|
1781
2258
|
# 1. Scan the repo.
|
|
1782
|
-
files, skipped, sites, gated, tsjs = _scan_repo(root, args.exclude or [])
|
|
1783
|
-
analysis = _analyze_scan(root, files, skipped, sites, gated, tsjs)
|
|
2259
|
+
files, skipped, sites, gated, tsjs, go = _scan_repo(root, args.exclude or [])
|
|
2260
|
+
analysis = _analyze_scan(root, files, skipped, sites, gated, tsjs, go)
|
|
1784
2261
|
gated_funcs = analysis["gated_funcs"]
|
|
1785
2262
|
gated_site_count_before = analysis["gated_site_count"]
|
|
1786
2263
|
total_before = analysis["total"]
|
|
1787
2264
|
ts_sites = analysis["ts_sites"]
|
|
2265
|
+
go_sites = analysis["go_sites"]
|
|
1788
2266
|
verbs = analysis["verbs"]
|
|
1789
2267
|
|
|
1790
2268
|
# 2. Print the risk report summary.
|
|
@@ -1817,9 +2295,15 @@ def wrap_cmd(args):
|
|
|
1817
2295
|
root, sites, gated, gated_funcs)
|
|
1818
2296
|
|
|
1819
2297
|
if not results:
|
|
1820
|
-
if ts_sites:
|
|
2298
|
+
if ts_sites and go_sites:
|
|
2299
|
+
print("nothing to wrap (ts/js and go findings are scan-only; "
|
|
2300
|
+
"gate them by hand).")
|
|
2301
|
+
elif ts_sites:
|
|
1821
2302
|
print("nothing to wrap (ts/js findings are scan-only; "
|
|
1822
2303
|
"gate them by hand).")
|
|
2304
|
+
elif go_sites:
|
|
2305
|
+
print("nothing to wrap (go findings are scan-only; "
|
|
2306
|
+
"gate them by hand).")
|
|
1823
2307
|
else:
|
|
1824
2308
|
print("nothing to wrap.")
|
|
1825
2309
|
return 0
|
|
@@ -1894,9 +2378,9 @@ def wrap_cmd(args):
|
|
|
1894
2378
|
return 2
|
|
1895
2379
|
|
|
1896
2380
|
# 7. Re-scan and report coverage (before/after gated-site counts).
|
|
1897
|
-
_files2, _skipped2, sites2, gated2, tsjs2 = _scan_repo(
|
|
2381
|
+
_files2, _skipped2, sites2, gated2, tsjs2, go2 = _scan_repo(
|
|
1898
2382
|
root, args.exclude or [])
|
|
1899
|
-
analysis2 = _analyze_scan(root, _files2, _skipped2, sites2, gated2, tsjs2)
|
|
2383
|
+
analysis2 = _analyze_scan(root, _files2, _skipped2, sites2, gated2, tsjs2, go2)
|
|
1900
2384
|
print("wrapped %d gate(s) in %d file(s)."
|
|
1901
2385
|
% (total_gates, len(results)))
|
|
1902
2386
|
print("coverage: %d/%d -> %d/%d writes gated"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pywrit
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.14
|
|
4
4
|
Summary: Python client + CLI for Writ — the gate before the write
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Project-URL: Homepage, https://github.com/withwrit/pywrit
|
|
@@ -13,6 +13,7 @@ Provides-Extra: polyglot
|
|
|
13
13
|
Requires-Dist: tree-sitter>=0.23; extra == "polyglot"
|
|
14
14
|
Requires-Dist: tree-sitter-typescript>=0.23; extra == "polyglot"
|
|
15
15
|
Requires-Dist: tree-sitter-javascript>=0.23; extra == "polyglot"
|
|
16
|
+
Requires-Dist: tree-sitter-go>=0.23; extra == "polyglot"
|
|
16
17
|
Provides-Extra: keywords
|
|
17
18
|
Requires-Dist: ai-agents; extra == "keywords"
|
|
18
19
|
Requires-Dist: policy; extra == "keywords"
|
|
@@ -60,7 +61,7 @@ install test.
|
|
|
60
61
|
| --- | --- | --- | --- |
|
|
61
62
|
| Python | Yes | Yes, inserts `_writ_check` gates | Parsed with the stdlib `ast`, no extra needed |
|
|
62
63
|
| TypeScript / JavaScript | Yes, with `pywrit[polyglot]` | No, scan only | Needs the extra: `pip install 'pywrit[polyglot]'`. Without it, the report says so loudly: a warning block names the unscanned files instead of reporting zero findings |
|
|
63
|
-
| Go |
|
|
64
|
+
| Go | Yes, with `pywrit[polyglot]` | No, scan only | First detection family only: database/sql Exec/ExecContext, GORM Create/Save/Update/Updates/Delete, HTTP client writes and http.NewRequest with a write method, generated-client <client>.<Resource>.<New\|Update\|Delete\|Create>; package-level generated clients not yet covered. Without the extra, the report names the unscanned Go files in the same warning block as TS/JS |
|
|
64
65
|
|
|
65
66
|
## 60-second quickstart: scan → wrap → gate
|
|
66
67
|
|
|
@@ -184,6 +185,14 @@ pipeline (twilio-style generated clients, e.g. `twilio-node`).
|
|
|
184
185
|
|
|
185
186
|

|
|
186
187
|
|
|
188
|
+
### Go (scan-only)
|
|
189
|
+
|
|
190
|
+
`writ scan` also finds write sites in Go. It needs the optional extra (tree-sitter based):
|
|
191
|
+
|
|
192
|
+
pip install 'pywrit[polyglot]'
|
|
193
|
+
|
|
194
|
+
Covered in this first family: `database/sql` `Exec` / `ExecContext`, GORM `Create` / `Save` / `Update` / `Updates` / `Delete`, HTTP client `Post` / `Put` / `Patch` / `Delete` and `http.NewRequest` / `NewRequestWithContext` with a write method, and generated-client calls shaped `<client>.<Resource>.<New|Update|Delete|Create>` with an uppercase resource segment (the stripe-go shape). GET/HEAD requests, `db.Query` / `QueryRow`, and bare constructors are not flagged. `_test.go` files and `vendor/` trees are skipped. Go is **scan-only**: `writ scan --apply` never rewrites `.go` files, gate those by hand. Not yet covered: package-level generated clients (`charge.New(...)`, the pre-v87 stripe-go shape), request-pipeline calls with a variable method, and raw-SQL helpers.
|
|
195
|
+
|
|
187
196
|
## GitHub Action
|
|
188
197
|
|
|
189
198
|
Scan every pull request for ungated write sites. Findings land as check
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Deterministic tests for `writ scan` Go support (slice 1).
|
|
3
|
+
|
|
4
|
+
Fixtures live in tests/fixtures/go. Every line tagged `// expect: <rule>`
|
|
5
|
+
must be reported with that rule, and nothing else may be reported.
|
|
6
|
+
Tests that parse Go skip when the optional `polyglot` extra (tree-sitter
|
|
7
|
+
plus tree-sitter-go) is not installed; the rest run with zero extra deps.
|
|
8
|
+
Mirrors test_scan_tsjs.py.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import contextlib
|
|
13
|
+
import io
|
|
14
|
+
import pathlib
|
|
15
|
+
import re
|
|
16
|
+
import shutil
|
|
17
|
+
import subprocess
|
|
18
|
+
import sys
|
|
19
|
+
import tempfile
|
|
20
|
+
import unittest
|
|
21
|
+
from unittest import mock
|
|
22
|
+
|
|
23
|
+
HERE = pathlib.Path(__file__).resolve().parent
|
|
24
|
+
sys.path.insert(0, str(HERE.parent / "src"))
|
|
25
|
+
|
|
26
|
+
from pywrit import cli # noqa: E402
|
|
27
|
+
|
|
28
|
+
FIXTURES = HERE / "fixtures" / "go"
|
|
29
|
+
HAVE_EXTRA = bool(cli._go_parsers())
|
|
30
|
+
_BLOCK_EXTRA = {m: None for m in ("tree_sitter", "tree_sitter_go",
|
|
31
|
+
"tree_sitter_language_pack")}
|
|
32
|
+
_GO_RULES = {"go-db-write", "go-orm-write", "go-http-client-write",
|
|
33
|
+
"go-http-request-write", "go-sdk-write"}
|
|
34
|
+
_EXPECT = re.compile(r"//\s*expect:\s*([a-z0-9-]+)")
|
|
35
|
+
_FINDING = re.compile(
|
|
36
|
+
r"^ (?P<file>\S+\.go):(?P<line>\d+) \[(?P<kind>[a-z]+)\] "
|
|
37
|
+
r"(?P<rule>go-[a-z0-9-]+) (?P<func>\S+)\(\): (?P<snippet>.+)$")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _args(path, policy_out, **kw):
|
|
41
|
+
ns = argparse.Namespace(path=str(path), exclude=[], policy_out=str(policy_out),
|
|
42
|
+
apply=False, yes=False, score=False, push_policy=False,
|
|
43
|
+
key="")
|
|
44
|
+
for k, v in kw.items():
|
|
45
|
+
setattr(ns, k, v)
|
|
46
|
+
return ns
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _run_scan(path, **kw):
|
|
50
|
+
"""Run scan_cmd in-process; return (exit code, stdout)."""
|
|
51
|
+
with tempfile.TemporaryDirectory() as td:
|
|
52
|
+
buf = io.StringIO()
|
|
53
|
+
with contextlib.redirect_stdout(buf):
|
|
54
|
+
code = cli.scan_cmd(_args(path, pathlib.Path(td) / "policy.json", **kw))
|
|
55
|
+
return code, buf.getvalue()
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _expected():
|
|
59
|
+
exp = set()
|
|
60
|
+
for p in sorted(FIXTURES.rglob("*.go")):
|
|
61
|
+
for i, line in enumerate(p.read_text(encoding="utf-8").splitlines(), 1):
|
|
62
|
+
m = _EXPECT.search(line)
|
|
63
|
+
if m:
|
|
64
|
+
exp.add((str(p.relative_to(FIXTURES)), i, m.group(1)))
|
|
65
|
+
return exp
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _findings(out):
|
|
69
|
+
return {(m.group("file"), int(m.group("line")), m.group("rule"))
|
|
70
|
+
for m in map(_FINDING.match, out.splitlines()) if m}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _copy_fixtures(dst):
|
|
74
|
+
shutil.copytree(str(FIXTURES), str(dst))
|
|
75
|
+
return dst
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class FixtureRulesTest(unittest.TestCase):
|
|
79
|
+
@unittest.skipUnless(HAVE_EXTRA, "polyglot extra not installed")
|
|
80
|
+
def test_every_expected_finding_and_nothing_else(self):
|
|
81
|
+
with tempfile.TemporaryDirectory() as td:
|
|
82
|
+
root = _copy_fixtures(pathlib.Path(td) / "repo") # outside git
|
|
83
|
+
code, out = _run_scan(root)
|
|
84
|
+
self.assertEqual(code, 0)
|
|
85
|
+
self.assertEqual(_findings(out), _expected(), out)
|
|
86
|
+
|
|
87
|
+
def test_every_rule_has_a_fixture(self):
|
|
88
|
+
covered = {r for (_f, _l, r) in _expected()}
|
|
89
|
+
self.assertEqual(covered, _GO_RULES)
|
|
90
|
+
|
|
91
|
+
@unittest.skipUnless(HAVE_EXTRA, "polyglot extra not installed")
|
|
92
|
+
def test_negative_reads_not_flagged(self):
|
|
93
|
+
with tempfile.TemporaryDirectory() as td:
|
|
94
|
+
root = pathlib.Path(td)
|
|
95
|
+
shutil.copy(str(FIXTURES / "reads.go"), str(root / "reads.go"))
|
|
96
|
+
code, out = _run_scan(root)
|
|
97
|
+
self.assertEqual(code, 0)
|
|
98
|
+
self.assertIn("go files scanned: 1 skipped: 0", out)
|
|
99
|
+
self.assertIn("go write sites: 0 in 0 function(s)", out)
|
|
100
|
+
self.assertEqual(_findings(out), set())
|
|
101
|
+
self.assertIn("nothing to instrument.", out)
|
|
102
|
+
|
|
103
|
+
@unittest.skipUnless(HAVE_EXTRA, "polyglot extra not installed")
|
|
104
|
+
def test_output_format_counts_and_verbs(self):
|
|
105
|
+
with tempfile.TemporaryDirectory() as td:
|
|
106
|
+
root = _copy_fixtures(pathlib.Path(td) / "repo")
|
|
107
|
+
policy = pathlib.Path(td) / "p.json"
|
|
108
|
+
buf = io.StringIO()
|
|
109
|
+
with contextlib.redirect_stdout(buf):
|
|
110
|
+
code = cli.scan_cmd(_args(root, policy))
|
|
111
|
+
out = buf.getvalue()
|
|
112
|
+
verbs = __import__("json").loads(policy.read_text())
|
|
113
|
+
self.assertEqual(code, 0)
|
|
114
|
+
n = len(_expected())
|
|
115
|
+
self.assertIn("go files scanned: 5 skipped: 0 (scan only, no --apply)", out)
|
|
116
|
+
self.assertIn("go write sites: %d in 6 function(s)" % n, out)
|
|
117
|
+
self.assertIn("go gated: 0/%d (0%%)" % n, out)
|
|
118
|
+
self.assertIn(" db.go:13 [db] go-db-write deleteOrder(): "
|
|
119
|
+
"db.Exec(\"DELETE FROM orders WHERE id = $1\", id)", out)
|
|
120
|
+
self.assertIn(" sdk.go:6 [sdk] go-sdk-write chargeCard(): "
|
|
121
|
+
"sc.Charges.New(params)", out)
|
|
122
|
+
for v in ("db.delete", "db.create", "http.submit", "sdk.charge",
|
|
123
|
+
"sdk.nested"):
|
|
124
|
+
self.assertEqual(verbs.get(v), "require_grant", v)
|
|
125
|
+
self.assertIn("nothing to instrument (go findings are scan-only", out)
|
|
126
|
+
|
|
127
|
+
@unittest.skipUnless(HAVE_EXTRA, "polyglot extra not installed")
|
|
128
|
+
def test_verdict_qualified_when_go_sites_exist(self):
|
|
129
|
+
# The score verdict grades Python writes only. With Go sites
|
|
130
|
+
# found and no Python write, the bare "No write operations
|
|
131
|
+
# discovered." must not stand: it reads as if the repo had no
|
|
132
|
+
# writes at all. Same rule as the TS/JS case.
|
|
133
|
+
with tempfile.TemporaryDirectory() as td:
|
|
134
|
+
root = _copy_fixtures(pathlib.Path(td) / "repo")
|
|
135
|
+
code, out = _run_scan(root)
|
|
136
|
+
self.assertEqual(code, 0)
|
|
137
|
+
self.assertIn("No Python write operations discovered.", out)
|
|
138
|
+
self.assertNotIn("\nNo write operations discovered.\n", out)
|
|
139
|
+
lines = [ln for ln in out.splitlines() if ln.strip()]
|
|
140
|
+
self.assertEqual(lines[-1], GitHubAppLineTest.LINE)
|
|
141
|
+
|
|
142
|
+
@unittest.skipUnless(HAVE_EXTRA, "polyglot extra not installed")
|
|
143
|
+
def test_apply_never_rewrites_go_but_still_gates_python(self):
|
|
144
|
+
with tempfile.TemporaryDirectory() as td:
|
|
145
|
+
root = pathlib.Path(td) / "repo"
|
|
146
|
+
(root / "orders").mkdir(parents=True)
|
|
147
|
+
go = root / "orders" / "api.go"
|
|
148
|
+
go.write_text('package orders\n\nfunc createOrder(db *sql.DB) {\n'
|
|
149
|
+
'\tdb.Exec("INSERT INTO o VALUES (1)")\n}\n')
|
|
150
|
+
py = root / "orders" / "store.py"
|
|
151
|
+
py.write_text('def create_order():\n'
|
|
152
|
+
' with open("/tmp/o", "w") as f:\n'
|
|
153
|
+
' f.write("o")\n')
|
|
154
|
+
before = go.read_bytes()
|
|
155
|
+
code, out = _run_scan(root, apply=True, yes=True)
|
|
156
|
+
self.assertEqual(code, 0)
|
|
157
|
+
self.assertEqual(go.read_bytes(), before)
|
|
158
|
+
self.assertIn("_writ_check(\"orders.create\")", py.read_text())
|
|
159
|
+
self.assertIn("orders/api.go:4 [db] go-db-write createOrder()", out)
|
|
160
|
+
self.assertIn("applied 1 gate(s) to 1 file(s).", out)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class FileWalkTest(unittest.TestCase):
|
|
164
|
+
"""Go file collection is stdlib-only; runs with or without the extra."""
|
|
165
|
+
|
|
166
|
+
WRITE = 'package main\n\nfunc f() { db.Exec("DELETE FROM t") }\n'
|
|
167
|
+
|
|
168
|
+
def _tree(self, root, rels):
|
|
169
|
+
for rel in rels:
|
|
170
|
+
p = root / rel
|
|
171
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
172
|
+
p.write_text(self.WRITE)
|
|
173
|
+
|
|
174
|
+
def test_skip_dirs_and_test_files(self):
|
|
175
|
+
with tempfile.TemporaryDirectory() as td:
|
|
176
|
+
root = pathlib.Path(td)
|
|
177
|
+
skipped = ["cmd/app/main_test.go", "vendor/lib/v.go",
|
|
178
|
+
"tests/a.go", "testing/a.go", ".git/hooks/x.go",
|
|
179
|
+
"node_modules/x/y.go", "src/readme.md", "src/a.ts"]
|
|
180
|
+
kept = ["cmd/app/main.go", "internal/db/store.go", "a.go"]
|
|
181
|
+
self._tree(root, skipped + kept)
|
|
182
|
+
got = [str(p.relative_to(root))
|
|
183
|
+
for p in cli._go_collect_files(root, [])]
|
|
184
|
+
self.assertEqual(sorted(got), sorted(kept))
|
|
185
|
+
|
|
186
|
+
def test_exclude_substring(self):
|
|
187
|
+
with tempfile.TemporaryDirectory() as td:
|
|
188
|
+
root = pathlib.Path(td)
|
|
189
|
+
self._tree(root, ["src/a.go", "generated/b.go"])
|
|
190
|
+
got = [str(p.relative_to(root))
|
|
191
|
+
for p in cli._go_collect_files(root, ["generated"])]
|
|
192
|
+
self.assertEqual(got, ["src/a.go"])
|
|
193
|
+
|
|
194
|
+
@unittest.skipUnless(shutil.which("git"), "git not installed")
|
|
195
|
+
def test_respects_gitignore_inside_git_repo(self):
|
|
196
|
+
with tempfile.TemporaryDirectory() as td:
|
|
197
|
+
root = pathlib.Path(td)
|
|
198
|
+
subprocess.run(["git", "init", "-q", str(root)], check=True)
|
|
199
|
+
(root / ".gitignore").write_text("generated/\n*.gen.go\n")
|
|
200
|
+
self._tree(root, ["src/a.go", "generated/b.go", "src/c.gen.go",
|
|
201
|
+
"vendor/x/y.go"])
|
|
202
|
+
got = [str(p.relative_to(root))
|
|
203
|
+
for p in cli._go_collect_files(root, [])]
|
|
204
|
+
self.assertEqual(got, ["src/a.go"])
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
class MissingExtraTest(unittest.TestCase):
|
|
208
|
+
BLOCK = [
|
|
209
|
+
" " + "-" * 64,
|
|
210
|
+
" WARNING: 2 Go file(s) were seen but none were scanned.",
|
|
211
|
+
" The polyglot extra is not installed, so this report covers",
|
|
212
|
+
" Python only. Zero Go findings is a coverage gap, not a",
|
|
213
|
+
" clean result.",
|
|
214
|
+
" Install it with: pip install 'pywrit[polyglot]'",
|
|
215
|
+
" " + "-" * 64,
|
|
216
|
+
]
|
|
217
|
+
BOTH = [
|
|
218
|
+
" WARNING: 1 TS/JS file(s) and 1 Go file(s) were seen but none "
|
|
219
|
+
"were scanned.",
|
|
220
|
+
" Python only. Zero TS/JS or Go findings is a coverage gap, not a",
|
|
221
|
+
]
|
|
222
|
+
|
|
223
|
+
def _repo(self, root, with_python=True):
|
|
224
|
+
(root / "cmd").mkdir()
|
|
225
|
+
(root / "cmd" / "a.go").write_text(FileWalkTest.WRITE)
|
|
226
|
+
(root / "cmd" / "b.go").write_text(FileWalkTest.WRITE)
|
|
227
|
+
if with_python:
|
|
228
|
+
(root / "orders.py").write_text(
|
|
229
|
+
'def create_order(db):\n db.execute("INSERT INTO o VALUES (1)")\n')
|
|
230
|
+
|
|
231
|
+
def _last_line(self, out):
|
|
232
|
+
lines = [ln for ln in out.splitlines() if ln.strip()]
|
|
233
|
+
return lines[-1] if lines else ""
|
|
234
|
+
|
|
235
|
+
def test_go_only_repo_block_counts_go_files_and_verdict_qualified(self):
|
|
236
|
+
with tempfile.TemporaryDirectory() as td:
|
|
237
|
+
root = pathlib.Path(td)
|
|
238
|
+
self._repo(root, with_python=False)
|
|
239
|
+
with mock.patch.dict(sys.modules, _BLOCK_EXTRA):
|
|
240
|
+
self.assertEqual(cli._go_parsers(), {})
|
|
241
|
+
code, out = _run_scan(root)
|
|
242
|
+
self.assertEqual(code, 0)
|
|
243
|
+
for line in self.BLOCK:
|
|
244
|
+
self.assertIn(line, out)
|
|
245
|
+
self.assertEqual(
|
|
246
|
+
out.count("WARNING: 2 Go file(s) were seen but none were "
|
|
247
|
+
"scanned."), 1)
|
|
248
|
+
# the bare verdict must not stand while Go files went unscanned
|
|
249
|
+
self.assertIn("No write operations discovered in the files that "
|
|
250
|
+
"were scanned.", out)
|
|
251
|
+
self.assertNotIn("\nNo write operations discovered.\n", out)
|
|
252
|
+
self.assertNotIn("go files scanned", out)
|
|
253
|
+
self.assertNotIn("ts/js files scanned", out)
|
|
254
|
+
self.assertEqual(self._last_line(out), GitHubAppLineTest.LINE)
|
|
255
|
+
|
|
256
|
+
def test_block_counts_both_languages(self):
|
|
257
|
+
with tempfile.TemporaryDirectory() as td:
|
|
258
|
+
root = pathlib.Path(td)
|
|
259
|
+
(root / "a.go").write_text(FileWalkTest.WRITE)
|
|
260
|
+
(root / "b.ts").write_text(
|
|
261
|
+
'export function f(){ return fetch("/x", { method: "POST" }); }\n')
|
|
262
|
+
with mock.patch.dict(sys.modules, _BLOCK_EXTRA):
|
|
263
|
+
code, out = _run_scan(root)
|
|
264
|
+
self.assertEqual(code, 0)
|
|
265
|
+
for line in self.BOTH:
|
|
266
|
+
self.assertIn(line, out)
|
|
267
|
+
self.assertEqual(self._last_line(out), GitHubAppLineTest.LINE)
|
|
268
|
+
|
|
269
|
+
def test_score_path_block_and_qualified_verdict(self):
|
|
270
|
+
with tempfile.TemporaryDirectory() as td:
|
|
271
|
+
root = pathlib.Path(td)
|
|
272
|
+
self._repo(root, with_python=False)
|
|
273
|
+
with mock.patch.dict(sys.modules, _BLOCK_EXTRA):
|
|
274
|
+
code, out = _run_scan(root, score=True)
|
|
275
|
+
self.assertEqual(code, 0)
|
|
276
|
+
for line in self.BLOCK:
|
|
277
|
+
self.assertIn(line, out)
|
|
278
|
+
self.assertIn("No write operations discovered in the files that "
|
|
279
|
+
"were scanned.", out)
|
|
280
|
+
self.assertEqual(self._last_line(out), GitHubAppLineTest.LINE)
|
|
281
|
+
|
|
282
|
+
def test_json_report_records_unscanned_go(self):
|
|
283
|
+
import json as j
|
|
284
|
+
with tempfile.TemporaryDirectory() as td:
|
|
285
|
+
root = pathlib.Path(td)
|
|
286
|
+
self._repo(root, with_python=False)
|
|
287
|
+
with mock.patch.dict(sys.modules, _BLOCK_EXTRA):
|
|
288
|
+
code, out = _run_scan(root, format="json")
|
|
289
|
+
self.assertEqual(code, 0, out)
|
|
290
|
+
doc = j.loads(out)
|
|
291
|
+
self.assertTrue(doc["go_hint"])
|
|
292
|
+
self.assertEqual(doc["go_not_scanned"], {
|
|
293
|
+
"reason": "missing_extra",
|
|
294
|
+
"extra": "pywrit[polyglot]",
|
|
295
|
+
"files": 2,
|
|
296
|
+
"install_command": "pip install 'pywrit[polyglot]'",
|
|
297
|
+
})
|
|
298
|
+
self.assertEqual(doc["findings"], [])
|
|
299
|
+
self.assertIn("github_app", doc)
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
class JsonReportTest(unittest.TestCase):
|
|
303
|
+
"""`writ scan --format json` carries Go findings like TS/JS ones."""
|
|
304
|
+
|
|
305
|
+
@unittest.skipUnless(HAVE_EXTRA, "polyglot extra not installed")
|
|
306
|
+
def test_json_mode_reports_go_finding(self):
|
|
307
|
+
import json as j
|
|
308
|
+
with tempfile.TemporaryDirectory() as td:
|
|
309
|
+
root = _copy_fixtures(pathlib.Path(td) / "repo")
|
|
310
|
+
code, out = _run_scan(root, format="json")
|
|
311
|
+
self.assertEqual(code, 0, out)
|
|
312
|
+
doc = j.loads(out)
|
|
313
|
+
self.assertFalse(doc["go_hint"])
|
|
314
|
+
self.assertEqual(doc["files_scanned"], 5)
|
|
315
|
+
go_findings = [f for f in doc["findings"]
|
|
316
|
+
if f["language"] == "go"]
|
|
317
|
+
self.assertEqual(len(go_findings), len(_expected()), doc)
|
|
318
|
+
f = next(f for f in go_findings if f["file"] == "db.go"
|
|
319
|
+
and f["line"] == 13)
|
|
320
|
+
self.assertEqual((f["function"], f["verb"], f["kind"], f["gated"]),
|
|
321
|
+
("deleteOrder", "db.delete", "db", False))
|
|
322
|
+
self.assertIn('db.Exec("DELETE FROM orders', f["snippet"])
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
class GitHubAppLineTest(unittest.TestCase):
|
|
326
|
+
"""The GitHub App line is the last line of the report with Go sites."""
|
|
327
|
+
|
|
328
|
+
LINE = ("Run this scan on every push and pull request with the Writ "
|
|
329
|
+
"GitHub App: check runs on each change, history in the portal. "
|
|
330
|
+
"Install: https://github.com/apps/writ-scanner "
|
|
331
|
+
"(guide: https://docs.withwrit.com/git-hub-app)")
|
|
332
|
+
|
|
333
|
+
def test_line_is_last_with_go_sites(self):
|
|
334
|
+
with tempfile.TemporaryDirectory() as td:
|
|
335
|
+
root = pathlib.Path(td)
|
|
336
|
+
(root / "a.go").write_text(FileWalkTest.WRITE)
|
|
337
|
+
code, out = _run_scan(root)
|
|
338
|
+
self.assertEqual(code, 0)
|
|
339
|
+
lines = [ln for ln in out.splitlines() if ln.strip()]
|
|
340
|
+
self.assertEqual(lines[-1], self.LINE)
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
if __name__ == "__main__":
|
|
344
|
+
unittest.main()
|
|
@@ -126,6 +126,21 @@ class FixtureRulesTest(unittest.TestCase):
|
|
|
126
126
|
self.assertEqual(verbs.get(v), "require_grant", v)
|
|
127
127
|
self.assertIn("nothing to instrument (ts/js findings are scan-only", out)
|
|
128
128
|
|
|
129
|
+
@unittest.skipUnless(HAVE_EXTRA, "polyglot extra not installed")
|
|
130
|
+
def test_verdict_qualified_when_tsjs_sites_exist(self):
|
|
131
|
+
# The score verdict grades Python writes only. With TS/JS sites
|
|
132
|
+
# found and no Python write, the bare "No write operations
|
|
133
|
+
# discovered." must not stand: it reads as if the repo had no
|
|
134
|
+
# writes at all. Same rule as the Go case.
|
|
135
|
+
with tempfile.TemporaryDirectory() as td:
|
|
136
|
+
root = _copy_fixtures(pathlib.Path(td) / "repo")
|
|
137
|
+
code, out = _run_scan(root)
|
|
138
|
+
self.assertEqual(code, 0)
|
|
139
|
+
self.assertIn("No Python write operations discovered.", out)
|
|
140
|
+
self.assertNotIn("\nNo write operations discovered.\n", out)
|
|
141
|
+
lines = [ln for ln in out.splitlines() if ln.strip()]
|
|
142
|
+
self.assertEqual(lines[-1], GitHubAppLineTest.LINE)
|
|
143
|
+
|
|
129
144
|
@unittest.skipUnless(HAVE_EXTRA, "polyglot extra not installed")
|
|
130
145
|
def test_apply_never_rewrites_tsjs_but_still_gates_python(self):
|
|
131
146
|
with tempfile.TemporaryDirectory() as td:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|