pywrit 0.2.17__tar.gz → 0.2.19__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.17/src/pywrit.egg-info → pywrit-0.2.19}/PKG-INFO +3 -3
- {pywrit-0.2.17 → pywrit-0.2.19}/README.md +2 -2
- {pywrit-0.2.17 → pywrit-0.2.19}/pyproject.toml +1 -1
- {pywrit-0.2.17 → pywrit-0.2.19}/src/pywrit/__init__.py +1 -1
- {pywrit-0.2.17 → pywrit-0.2.19}/src/pywrit/cli.py +151 -6
- {pywrit-0.2.17 → pywrit-0.2.19/src/pywrit.egg-info}/PKG-INFO +3 -3
- {pywrit-0.2.17 → pywrit-0.2.19}/tests/test_scan_go.py +7 -4
- {pywrit-0.2.17 → pywrit-0.2.19}/LICENSE +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/setup.cfg +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/src/pywrit/client.py +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/src/pywrit/errors.py +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/src/pywrit.egg-info/SOURCES.txt +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/src/pywrit.egg-info/dependency_links.txt +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/src/pywrit.egg-info/entry_points.txt +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/src/pywrit.egg-info/requires.txt +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/src/pywrit.egg-info/top_level.txt +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/tests/test_client.py +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/tests/test_scan_json.py +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/tests/test_scan_python.py +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/tests/test_scan_tsjs.py +0 -0
- {pywrit-0.2.17 → pywrit-0.2.19}/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.19
|
|
4
4
|
Summary: Python client + CLI for Writ — the gate before the write
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Project-URL: Homepage, https://withwrit.com
|
|
@@ -61,7 +61,7 @@ install test.
|
|
|
61
61
|
| --- | --- | --- | --- |
|
|
62
62
|
| Python | Yes | Yes, inserts `_writ_check` gates | Parsed with the stdlib `ast`, no extra needed |
|
|
63
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 |
|
|
64
|
-
| Go | Yes, with `pywrit[polyglot]` | No, scan only | First detection family plus request-pipeline Call/RawRequest
|
|
64
|
+
| Go | Yes, with `pywrit[polyglot]` | No, scan only | First detection family plus request-pipeline Call/RawRequest, package-level generated clients, and GORM Raw raw-SQL writes: 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>, request-pipeline X.Call/X.RawRequest with a write method, package-level generated clients <pkg>.<New\|Update\|Delete\|Create> on an imported resource-shaped package, GORM Raw `db.Raw` / `tx.Raw` with a write-first SQL string literal, including WITH-CTE writes. Without the extra, the report names the unscanned Go files in the same warning block as TS/JS |
|
|
65
65
|
|
|
66
66
|
## 60-second quickstart: scan → wrap → gate
|
|
67
67
|
|
|
@@ -191,7 +191,7 @@ pipeline (twilio-style generated clients, e.g. `twilio-node`).
|
|
|
191
191
|
|
|
192
192
|
pip install 'pywrit[polyglot]'
|
|
193
193
|
|
|
194
|
-
Covered in this first family plus request-pipeline and package-level generated-client detection: `database/sql` `Exec` / `ExecContext`, GORM `Create` / `Save` / `Update` / `Updates` / `Delete`, HTTP client `Post` / `Put` / `Patch` / `Delete` and `http.NewRequest` / `NewRequestWithContext` with a write method, generated-client calls shaped `<client>.<Resource>.<New|Update|Delete|Create>` with an uppercase resource segment (the stripe-go shape), request-pipeline calls `X.Call(method, ...)` / `X.RawRequest(method, ...)` where argument 0 is a write method (the `stripe.Backend` shape, e.g. `c.B.Call(http.MethodPost, "/v1/accounts", ...)`), and package-level generated-client calls `<pkg>.<New|Update|Delete|Create>(...)` where `<pkg>` is imported in the file and the import path's last segment is resource-shaped, not structy (`params`, `config`, ...) and not service-, client-, library-, or test-double-shaped (`svc`, `service`, `client`, `api`, `sdk`, `internal`, `mock`, `fake`). This is the pre-v87 stripe-go shape (`charge.New(params)`), aliased imports included: the guard reads the import path, so `acmeinvoice "example.com/acme/acme-payments/invoice"` fires while `paymentsvc.New(cfg)` does not. GET/HEAD requests, `db.Query` / `QueryRow`, bare constructors (`.New` on a local variable or on an imported service- or structy-shaped package), and pipeline calls with a variable method or a non-method first argument 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:
|
|
194
|
+
Covered in this first family plus request-pipeline and package-level generated-client detection: `database/sql` `Exec` / `ExecContext`, GORM `Create` / `Save` / `Update` / `Updates` / `Delete`, HTTP client `Post` / `Put` / `Patch` / `Delete` and `http.NewRequest` / `NewRequestWithContext` with a write method, generated-client calls shaped `<client>.<Resource>.<New|Update|Delete|Create>` with an uppercase resource segment (the stripe-go shape), request-pipeline calls `X.Call(method, ...)` / `X.RawRequest(method, ...)` where argument 0 is a write method (the `stripe.Backend` shape, e.g. `c.B.Call(http.MethodPost, "/v1/accounts", ...)`), and package-level generated-client calls `<pkg>.<New|Update|Delete|Create>(...)` where `<pkg>` is imported in the file and the import path's last segment is resource-shaped, not structy (`params`, `config`, ...) and not service-, client-, library-, or test-double-shaped (`svc`, `service`, `client`, `api`, `sdk`, `internal`, `mock`, `fake`). This is the pre-v87 stripe-go shape (`charge.New(params)`), aliased imports included: the guard reads the import path, so `acmeinvoice "example.com/acme/acme-payments/invoice"` fires while `paymentsvc.New(cfg)` does not. GORM raw-SQL calls `db.Raw("INSERT INTO ...")` / `tx.Raw("DELETE FROM ...")` fire when argument 0 is a string literal (interpreted or backtick) whose first keyword is a write verb (INSERT, UPDATE, DELETE, REPLACE, TRUNCATE, MERGE), case-insensitive; a variable, a read-first literal (SELECT), or a Raw call on a non-db/tx receiver never fires. A leading WITH is graded past: the CTE list is parsed and the main statement's first keyword is graded the same way, so WITH ... DELETE / INSERT / UPDATE fires while WITH ... SELECT stays silent; a write verb inside a CTE query never decides the grade, and a malformed WITH grades as a read. GET/HEAD requests, `db.Query` / `QueryRow`, bare constructors (`.New` on a local variable or on an imported service- or structy-shaped package), and pipeline calls with a variable method or a non-method first argument 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: other Go write patterns.
|
|
195
195
|
|
|
196
196
|
## GitHub Action
|
|
197
197
|
|
|
@@ -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 | Yes, with `pywrit[polyglot]` | No, scan only | First detection family plus request-pipeline Call/RawRequest
|
|
41
|
+
| Go | Yes, with `pywrit[polyglot]` | No, scan only | First detection family plus request-pipeline Call/RawRequest, package-level generated clients, and GORM Raw raw-SQL writes: 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>, request-pipeline X.Call/X.RawRequest with a write method, package-level generated clients <pkg>.<New\|Update\|Delete\|Create> on an imported resource-shaped package, GORM Raw `db.Raw` / `tx.Raw` with a write-first SQL string literal, including WITH-CTE writes. 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
|
|
|
@@ -168,7 +168,7 @@ pipeline (twilio-style generated clients, e.g. `twilio-node`).
|
|
|
168
168
|
|
|
169
169
|
pip install 'pywrit[polyglot]'
|
|
170
170
|
|
|
171
|
-
Covered in this first family plus request-pipeline and package-level generated-client detection: `database/sql` `Exec` / `ExecContext`, GORM `Create` / `Save` / `Update` / `Updates` / `Delete`, HTTP client `Post` / `Put` / `Patch` / `Delete` and `http.NewRequest` / `NewRequestWithContext` with a write method, generated-client calls shaped `<client>.<Resource>.<New|Update|Delete|Create>` with an uppercase resource segment (the stripe-go shape), request-pipeline calls `X.Call(method, ...)` / `X.RawRequest(method, ...)` where argument 0 is a write method (the `stripe.Backend` shape, e.g. `c.B.Call(http.MethodPost, "/v1/accounts", ...)`), and package-level generated-client calls `<pkg>.<New|Update|Delete|Create>(...)` where `<pkg>` is imported in the file and the import path's last segment is resource-shaped, not structy (`params`, `config`, ...) and not service-, client-, library-, or test-double-shaped (`svc`, `service`, `client`, `api`, `sdk`, `internal`, `mock`, `fake`). This is the pre-v87 stripe-go shape (`charge.New(params)`), aliased imports included: the guard reads the import path, so `acmeinvoice "example.com/acme/acme-payments/invoice"` fires while `paymentsvc.New(cfg)` does not. GET/HEAD requests, `db.Query` / `QueryRow`, bare constructors (`.New` on a local variable or on an imported service- or structy-shaped package), and pipeline calls with a variable method or a non-method first argument 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:
|
|
171
|
+
Covered in this first family plus request-pipeline and package-level generated-client detection: `database/sql` `Exec` / `ExecContext`, GORM `Create` / `Save` / `Update` / `Updates` / `Delete`, HTTP client `Post` / `Put` / `Patch` / `Delete` and `http.NewRequest` / `NewRequestWithContext` with a write method, generated-client calls shaped `<client>.<Resource>.<New|Update|Delete|Create>` with an uppercase resource segment (the stripe-go shape), request-pipeline calls `X.Call(method, ...)` / `X.RawRequest(method, ...)` where argument 0 is a write method (the `stripe.Backend` shape, e.g. `c.B.Call(http.MethodPost, "/v1/accounts", ...)`), and package-level generated-client calls `<pkg>.<New|Update|Delete|Create>(...)` where `<pkg>` is imported in the file and the import path's last segment is resource-shaped, not structy (`params`, `config`, ...) and not service-, client-, library-, or test-double-shaped (`svc`, `service`, `client`, `api`, `sdk`, `internal`, `mock`, `fake`). This is the pre-v87 stripe-go shape (`charge.New(params)`), aliased imports included: the guard reads the import path, so `acmeinvoice "example.com/acme/acme-payments/invoice"` fires while `paymentsvc.New(cfg)` does not. GORM raw-SQL calls `db.Raw("INSERT INTO ...")` / `tx.Raw("DELETE FROM ...")` fire when argument 0 is a string literal (interpreted or backtick) whose first keyword is a write verb (INSERT, UPDATE, DELETE, REPLACE, TRUNCATE, MERGE), case-insensitive; a variable, a read-first literal (SELECT), or a Raw call on a non-db/tx receiver never fires. A leading WITH is graded past: the CTE list is parsed and the main statement's first keyword is graded the same way, so WITH ... DELETE / INSERT / UPDATE fires while WITH ... SELECT stays silent; a write verb inside a CTE query never decides the grade, and a malformed WITH grades as a read. GET/HEAD requests, `db.Query` / `QueryRow`, bare constructors (`.New` on a local variable or on an imported service- or structy-shaped package), and pipeline calls with a variable method or a non-method first argument 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: other Go write patterns.
|
|
172
172
|
|
|
173
173
|
## GitHub Action
|
|
174
174
|
|
|
@@ -1395,7 +1395,7 @@ def _tsjs_scan_source(src, filename, parser):
|
|
|
1395
1395
|
return sites, gated
|
|
1396
1396
|
|
|
1397
1397
|
|
|
1398
|
-
# writ scan, Go (slices 1-
|
|
1398
|
+
# writ scan, Go (slices 1-5): deterministic and scan-only (Go files are
|
|
1399
1399
|
# never rewritten). Same tree-sitter tier as TS/JS, same
|
|
1400
1400
|
# `pywrit[polyglot]` extra plus the tree-sitter-go grammar; without the
|
|
1401
1401
|
# extra the missing-extra block counts Go files too. Slice 1 covers one
|
|
@@ -1408,9 +1408,13 @@ def _tsjs_scan_source(src, filename, parser):
|
|
|
1408
1408
|
# middle segment). Slice 2 adds request-pipeline Call/RawRequest with a
|
|
1409
1409
|
# write method as argument 0. Slice 3 adds import-aware package-level
|
|
1410
1410
|
# generated clients (<pkg>.<New|Update|Delete|Create>, the pre-v87
|
|
1411
|
-
# stripe-go shape: charge.New(params)).
|
|
1412
|
-
#
|
|
1413
|
-
#
|
|
1411
|
+
# stripe-go shape: charge.New(params)). Slice 4 adds the GORM raw-SQL
|
|
1412
|
+
# helper: <db|tx>.Raw(<write SQL string literal>) graded on the
|
|
1413
|
+
# literal's first keyword. Slice 5 grades past a leading WITH: the
|
|
1414
|
+
# main statement after the CTE list is graded on its first keyword the
|
|
1415
|
+
# same way. Wider Go coverage (os/io/fs writes, sqlx,
|
|
1416
|
+
# raw-SQL conditions on Exec, Go-specific
|
|
1417
|
+
# non-product suppression classes) ships in later slices.
|
|
1414
1418
|
# ---------------------------------------------------------------------------
|
|
1415
1419
|
|
|
1416
1420
|
_GO_MAX_BYTES = 1000000 # larger files are almost always generated
|
|
@@ -1423,6 +1427,16 @@ _GO_TEST_DIRS = {"tests", "testing"}
|
|
|
1423
1427
|
# resource-segment rule.
|
|
1424
1428
|
_GO_PKG_ROOTS = {"http", "sql", "gorm"}
|
|
1425
1429
|
|
|
1430
|
+
# Write-first SQL verbs for the slice-4/5 GORM raw-SQL grade: a Raw()
|
|
1431
|
+
# string literal is a write only when its first keyword (leading
|
|
1432
|
+
# whitespace stripped) is one of these, case-insensitive. A leading
|
|
1433
|
+
# WITH (slice 5) is graded past: the main statement after the CTE
|
|
1434
|
+
# list is graded on its first keyword the same way. Read-first
|
|
1435
|
+
# statements (SELECT, SHOW, DESCRIBE, EXPLAIN, and WITH ... SELECT)
|
|
1436
|
+
# never fire.
|
|
1437
|
+
_GO_RAWSQL_WRITE_VERBS = ("insert", "update", "delete", "replace",
|
|
1438
|
+
"truncate", "merge")
|
|
1439
|
+
|
|
1426
1440
|
# Classifier noise guards. Uppercase two-segment selectors (s.Info.
|
|
1427
1441
|
# Update) are ubiquitous in real code (structs like Params, Args, Options,
|
|
1428
1442
|
# Config); the stripe-go generated-client shape is three segments
|
|
@@ -1489,6 +1503,131 @@ _GO_PKGCLIENT_NONRESOURCE = (
|
|
|
1489
1503
|
)
|
|
1490
1504
|
|
|
1491
1505
|
|
|
1506
|
+
def _go_sql_first_kw(lit):
|
|
1507
|
+
"""The first keyword that decides the write grade of a Raw() SQL
|
|
1508
|
+
literal, else None when no keyword qualifies. Normally the
|
|
1509
|
+
literal's first keyword; for a leading WITH (slice 5) the CTE list
|
|
1510
|
+
is walked and the keyword is the main statement's first keyword
|
|
1511
|
+
instead. The CTE walk is a real parse, not a substring search for
|
|
1512
|
+
a write verb: keywords inside a CTE query never decide the grade.
|
|
1513
|
+
Anything not keyword-shaped (an empty literal, one that starts
|
|
1514
|
+
with "(" or "--") and a WITH statement off the CTE-list shape
|
|
1515
|
+
(unbalanced parens, a CTE list that never terminates, a paren
|
|
1516
|
+
right after WITH, no main verb) never reaches _GO_RAWSQL_WRITE_VERBS,
|
|
1517
|
+
so it falls through as a read."""
|
|
1518
|
+
s = lit.lstrip()
|
|
1519
|
+
m = _re.match(r"[A-Za-z_][A-Za-z0-9_$]*", s)
|
|
1520
|
+
first = m.group(0).lower() if m else ""
|
|
1521
|
+
if first != "with":
|
|
1522
|
+
return first or None
|
|
1523
|
+
i = m.end()
|
|
1524
|
+
n = len(s)
|
|
1525
|
+
if i < n and s[i] == "(":
|
|
1526
|
+
# not the name-first CTE-list shape (a row list): off shape
|
|
1527
|
+
return None
|
|
1528
|
+
|
|
1529
|
+
def ws(j):
|
|
1530
|
+
while j < n and s[j].isspace():
|
|
1531
|
+
j += 1
|
|
1532
|
+
return j
|
|
1533
|
+
|
|
1534
|
+
def word(j):
|
|
1535
|
+
j = ws(j)
|
|
1536
|
+
k = j
|
|
1537
|
+
while k < n and (s[k].isalnum() or s[k] in "_$"):
|
|
1538
|
+
k += 1
|
|
1539
|
+
return (s[j:k].lower(), k) if k > j else (None, k)
|
|
1540
|
+
|
|
1541
|
+
def ident(j):
|
|
1542
|
+
j = ws(j)
|
|
1543
|
+
w, k = word(j)
|
|
1544
|
+
if w is not None:
|
|
1545
|
+
return k
|
|
1546
|
+
if j < n and s[j] in '"`[': # a quoted CTE name
|
|
1547
|
+
close = "]" if s[j] == "[" else s[j]
|
|
1548
|
+
k = j + 1
|
|
1549
|
+
while k < n:
|
|
1550
|
+
if s[k] == close:
|
|
1551
|
+
if k + 1 < n and s[k + 1] == close:
|
|
1552
|
+
k += 2
|
|
1553
|
+
continue
|
|
1554
|
+
return k + 1
|
|
1555
|
+
k += 1
|
|
1556
|
+
return -1
|
|
1557
|
+
|
|
1558
|
+
def parens(j):
|
|
1559
|
+
j = ws(j)
|
|
1560
|
+
if j >= n or s[j] != "(":
|
|
1561
|
+
return -1
|
|
1562
|
+
depth = 0
|
|
1563
|
+
while j < n:
|
|
1564
|
+
if s[j] == "(":
|
|
1565
|
+
depth += 1
|
|
1566
|
+
elif s[j] == ")":
|
|
1567
|
+
depth -= 1
|
|
1568
|
+
if depth == 0:
|
|
1569
|
+
return j + 1
|
|
1570
|
+
j += 1
|
|
1571
|
+
return -1
|
|
1572
|
+
|
|
1573
|
+
if word(i)[0] == "recursive":
|
|
1574
|
+
i = word(i)[1]
|
|
1575
|
+
while True:
|
|
1576
|
+
# each CTE definition: name, optional column list, AS,
|
|
1577
|
+
# optional (NOT) MATERIALIZED, one balanced query group
|
|
1578
|
+
if ws(i) >= n:
|
|
1579
|
+
return None # no name after WITH or comma: malformed
|
|
1580
|
+
i = ident(i)
|
|
1581
|
+
if i < 0:
|
|
1582
|
+
return None
|
|
1583
|
+
j = ws(i)
|
|
1584
|
+
if j < n and s[j] == "(": # optional column list
|
|
1585
|
+
i = parens(i)
|
|
1586
|
+
if i < 0:
|
|
1587
|
+
return None
|
|
1588
|
+
w, i = word(i)
|
|
1589
|
+
if w != "as":
|
|
1590
|
+
return None
|
|
1591
|
+
w, i = word(i)
|
|
1592
|
+
if w == "materialized":
|
|
1593
|
+
i = word(i)[1]
|
|
1594
|
+
elif w == "not":
|
|
1595
|
+
w, i = word(i)
|
|
1596
|
+
if w != "materialized":
|
|
1597
|
+
return None
|
|
1598
|
+
i = word(i)[1]
|
|
1599
|
+
i = parens(i) # the CTE query: may nest parens, commas, verbs
|
|
1600
|
+
if i < 0:
|
|
1601
|
+
return None
|
|
1602
|
+
j = ws(i)
|
|
1603
|
+
if j < n and s[j] == ",":
|
|
1604
|
+
i = j + 1
|
|
1605
|
+
continue
|
|
1606
|
+
return word(i)[0] # the main verb, or None when missing
|
|
1607
|
+
|
|
1608
|
+
|
|
1609
|
+
def _go_rawsql_write(call, f):
|
|
1610
|
+
"""Slice-4 grade for the GORM Raw helper: the call is a write only
|
|
1611
|
+
when argument 0 is an interpreted or backtick string literal whose
|
|
1612
|
+
grading keyword (case-insensitive) is a write verb in
|
|
1613
|
+
_GO_RAWSQL_WRITE_VERBS. A variable, a concatenation, a fmt.Sprintf
|
|
1614
|
+
call, or any non-literal first argument never fires (literal()
|
|
1615
|
+
returns None); read-first SQL (SELECT, SHOW, DESCRIBE, EXPLAIN)
|
|
1616
|
+
falls through as a read. Slice 5: the grading keyword of a leading
|
|
1617
|
+
WITH statement is the main statement verb past the CTE list, so
|
|
1618
|
+
WITH ... DELETE / INSERT / UPDATE fires while WITH ... SELECT
|
|
1619
|
+
stays silent; a malformed WITH statement grades as a read."""
|
|
1620
|
+
args = _go_args(call)
|
|
1621
|
+
if not args:
|
|
1622
|
+
return False
|
|
1623
|
+
lit = f.literal(args[0])
|
|
1624
|
+
if lit is None:
|
|
1625
|
+
return False
|
|
1626
|
+
return _go_sql_first_kw(lit) in _GO_RAWSQL_WRITE_VERBS
|
|
1627
|
+
|
|
1628
|
+
|
|
1629
|
+
|
|
1630
|
+
|
|
1492
1631
|
class _GoFile:
|
|
1493
1632
|
def __init__(self, src):
|
|
1494
1633
|
self.src = src
|
|
@@ -1587,8 +1726,9 @@ def _go_import_resource_name(path):
|
|
|
1587
1726
|
|
|
1588
1727
|
def _go_classify(call, chain, f, pkg_paths=None):
|
|
1589
1728
|
"""First match wins; returns (rule_id, kind). Slice-1 family, the
|
|
1590
|
-
slice-3 package-level generated-client family,
|
|
1591
|
-
request-pipeline family (Call / RawRequest)
|
|
1729
|
+
slice-3 package-level generated-client family, the slice-2
|
|
1730
|
+
request-pipeline family (Call / RawRequest), and the slice-4 GORM
|
|
1731
|
+
raw-SQL family (Raw with a write-first SQL literal).
|
|
1592
1732
|
|
|
1593
1733
|
`chain` is the raw (un-lowercased) callee chain. Handle receivers
|
|
1594
1734
|
match by convention on any receiver segment (db, tx, gormDB, r.db,
|
|
@@ -1612,6 +1752,11 @@ def _go_classify(call, chain, f, pkg_paths=None):
|
|
|
1612
1752
|
if tail in _GO_ORM_METHODS:
|
|
1613
1753
|
# GORM shape: db.Create / Save / Update / Updates / Delete
|
|
1614
1754
|
return "go-orm-write", "db"
|
|
1755
|
+
if tail == "raw" and _go_rawsql_write(call, f):
|
|
1756
|
+
# GORM raw SQL (slices 4-5): db.Raw / tx.Raw, only when
|
|
1757
|
+
# the literal SQL's grading keyword is a write verb (past
|
|
1758
|
+
# a leading WITH when present).
|
|
1759
|
+
return "go-rawsql-write", "db"
|
|
1615
1760
|
if tail in _GO_HTTP_VERBS:
|
|
1616
1761
|
if norm[0] == "http" and len(norm) == 2:
|
|
1617
1762
|
# package-level http.Post / Put / Patch / Delete
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pywrit
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.19
|
|
4
4
|
Summary: Python client + CLI for Writ — the gate before the write
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Project-URL: Homepage, https://withwrit.com
|
|
@@ -61,7 +61,7 @@ install test.
|
|
|
61
61
|
| --- | --- | --- | --- |
|
|
62
62
|
| Python | Yes | Yes, inserts `_writ_check` gates | Parsed with the stdlib `ast`, no extra needed |
|
|
63
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 |
|
|
64
|
-
| Go | Yes, with `pywrit[polyglot]` | No, scan only | First detection family plus request-pipeline Call/RawRequest
|
|
64
|
+
| Go | Yes, with `pywrit[polyglot]` | No, scan only | First detection family plus request-pipeline Call/RawRequest, package-level generated clients, and GORM Raw raw-SQL writes: 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>, request-pipeline X.Call/X.RawRequest with a write method, package-level generated clients <pkg>.<New\|Update\|Delete\|Create> on an imported resource-shaped package, GORM Raw `db.Raw` / `tx.Raw` with a write-first SQL string literal, including WITH-CTE writes. Without the extra, the report names the unscanned Go files in the same warning block as TS/JS |
|
|
65
65
|
|
|
66
66
|
## 60-second quickstart: scan → wrap → gate
|
|
67
67
|
|
|
@@ -191,7 +191,7 @@ pipeline (twilio-style generated clients, e.g. `twilio-node`).
|
|
|
191
191
|
|
|
192
192
|
pip install 'pywrit[polyglot]'
|
|
193
193
|
|
|
194
|
-
Covered in this first family plus request-pipeline and package-level generated-client detection: `database/sql` `Exec` / `ExecContext`, GORM `Create` / `Save` / `Update` / `Updates` / `Delete`, HTTP client `Post` / `Put` / `Patch` / `Delete` and `http.NewRequest` / `NewRequestWithContext` with a write method, generated-client calls shaped `<client>.<Resource>.<New|Update|Delete|Create>` with an uppercase resource segment (the stripe-go shape), request-pipeline calls `X.Call(method, ...)` / `X.RawRequest(method, ...)` where argument 0 is a write method (the `stripe.Backend` shape, e.g. `c.B.Call(http.MethodPost, "/v1/accounts", ...)`), and package-level generated-client calls `<pkg>.<New|Update|Delete|Create>(...)` where `<pkg>` is imported in the file and the import path's last segment is resource-shaped, not structy (`params`, `config`, ...) and not service-, client-, library-, or test-double-shaped (`svc`, `service`, `client`, `api`, `sdk`, `internal`, `mock`, `fake`). This is the pre-v87 stripe-go shape (`charge.New(params)`), aliased imports included: the guard reads the import path, so `acmeinvoice "example.com/acme/acme-payments/invoice"` fires while `paymentsvc.New(cfg)` does not. GET/HEAD requests, `db.Query` / `QueryRow`, bare constructors (`.New` on a local variable or on an imported service- or structy-shaped package), and pipeline calls with a variable method or a non-method first argument 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:
|
|
194
|
+
Covered in this first family plus request-pipeline and package-level generated-client detection: `database/sql` `Exec` / `ExecContext`, GORM `Create` / `Save` / `Update` / `Updates` / `Delete`, HTTP client `Post` / `Put` / `Patch` / `Delete` and `http.NewRequest` / `NewRequestWithContext` with a write method, generated-client calls shaped `<client>.<Resource>.<New|Update|Delete|Create>` with an uppercase resource segment (the stripe-go shape), request-pipeline calls `X.Call(method, ...)` / `X.RawRequest(method, ...)` where argument 0 is a write method (the `stripe.Backend` shape, e.g. `c.B.Call(http.MethodPost, "/v1/accounts", ...)`), and package-level generated-client calls `<pkg>.<New|Update|Delete|Create>(...)` where `<pkg>` is imported in the file and the import path's last segment is resource-shaped, not structy (`params`, `config`, ...) and not service-, client-, library-, or test-double-shaped (`svc`, `service`, `client`, `api`, `sdk`, `internal`, `mock`, `fake`). This is the pre-v87 stripe-go shape (`charge.New(params)`), aliased imports included: the guard reads the import path, so `acmeinvoice "example.com/acme/acme-payments/invoice"` fires while `paymentsvc.New(cfg)` does not. GORM raw-SQL calls `db.Raw("INSERT INTO ...")` / `tx.Raw("DELETE FROM ...")` fire when argument 0 is a string literal (interpreted or backtick) whose first keyword is a write verb (INSERT, UPDATE, DELETE, REPLACE, TRUNCATE, MERGE), case-insensitive; a variable, a read-first literal (SELECT), or a Raw call on a non-db/tx receiver never fires. A leading WITH is graded past: the CTE list is parsed and the main statement's first keyword is graded the same way, so WITH ... DELETE / INSERT / UPDATE fires while WITH ... SELECT stays silent; a write verb inside a CTE query never decides the grade, and a malformed WITH grades as a read. GET/HEAD requests, `db.Query` / `QueryRow`, bare constructors (`.New` on a local variable or on an imported service- or structy-shaped package), and pipeline calls with a variable method or a non-method first argument 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: other Go write patterns.
|
|
195
195
|
|
|
196
196
|
## GitHub Action
|
|
197
197
|
|
|
@@ -31,7 +31,7 @@ _BLOCK_EXTRA = {m: None for m in ("tree_sitter", "tree_sitter_go",
|
|
|
31
31
|
"tree_sitter_language_pack")}
|
|
32
32
|
_GO_RULES = {"go-db-write", "go-orm-write", "go-http-client-write",
|
|
33
33
|
"go-http-request-write", "go-sdk-write", "go-pipeline-write",
|
|
34
|
-
"go-pkgclient-write"}
|
|
34
|
+
"go-pkgclient-write", "go-rawsql-write"}
|
|
35
35
|
_EXPECT = re.compile(r"//\s*expect:\s*([a-z0-9-]+)")
|
|
36
36
|
_FINDING = re.compile(
|
|
37
37
|
r"^ (?P<file>\S+\.go):(?P<line>\d+) \[(?P<kind>[a-z]+)\] "
|
|
@@ -113,8 +113,8 @@ class FixtureRulesTest(unittest.TestCase):
|
|
|
113
113
|
verbs = __import__("json").loads(policy.read_text())
|
|
114
114
|
self.assertEqual(code, 0)
|
|
115
115
|
n = len(_expected())
|
|
116
|
-
self.assertIn("go files scanned:
|
|
117
|
-
self.assertIn("go write sites: %d in
|
|
116
|
+
self.assertIn("go files scanned: 8 skipped: 0 (scan only, no --apply)", out)
|
|
117
|
+
self.assertIn("go write sites: %d in 13 function(s)" % n, out)
|
|
118
118
|
self.assertIn("go gated: 0/%d (0%%)" % n, out)
|
|
119
119
|
self.assertIn(" db.go:13 [db] go-db-write deleteOrder(): "
|
|
120
120
|
"db.Exec(\"DELETE FROM orders WHERE id = $1\", id)", out)
|
|
@@ -122,6 +122,9 @@ class FixtureRulesTest(unittest.TestCase):
|
|
|
122
122
|
"sc.Charges.New(params)", out)
|
|
123
123
|
self.assertIn(" pkgclient.go:25 [sdk] go-pkgclient-write "
|
|
124
124
|
"legacyCharge(): charge.New(params)", out)
|
|
125
|
+
self.assertIn(" rawsql.go:16 [db] go-rawsql-write "
|
|
126
|
+
"backfillUsers(): db.Raw(\"INSERT INTO audit_log "
|
|
127
|
+
"(event) VALUES ('x')\")", out)
|
|
125
128
|
for v in ("db.delete", "db.create", "http.submit", "sdk.charge",
|
|
126
129
|
"sdk.nested"):
|
|
127
130
|
self.assertEqual(verbs.get(v), "require_grant", v)
|
|
@@ -314,7 +317,7 @@ class JsonReportTest(unittest.TestCase):
|
|
|
314
317
|
self.assertEqual(code, 0, out)
|
|
315
318
|
doc = j.loads(out)
|
|
316
319
|
self.assertFalse(doc["go_hint"])
|
|
317
|
-
self.assertEqual(doc["files_scanned"],
|
|
320
|
+
self.assertEqual(doc["files_scanned"], 8)
|
|
318
321
|
go_findings = [f for f in doc["findings"]
|
|
319
322
|
if f["language"] == "go"]
|
|
320
323
|
self.assertEqual(len(go_findings), len(_expected()), doc)
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|