All postsEngineering

Auditing the permissions your AI assistant wrote

Generated code tends to over-grant: wildcard IAM, unscoped DRF querysets, half-done RLS. Tested greps, Semgrep and Rego rules, and a CI job that blocks it.

Johnny Unar12 min read

01

why generated permissions come out too wide

Ask a coding assistant to let a Lambda write to S3 and there's a good chance you get "Action": "s3:*" on "Resource": "*". Ask it for a DRF viewset over invoices and you'll often get queryset = Invoice.objects.all() with IsAuthenticated as the only permission class. Neither of these is a hallucination in the usual sense. Both are the shortest path to code that runs on the first try, and an assistant is rewarded for exactly that, while a person writing the policy by hand has to look up action names, gets an AccessDenied, and narrows things down out of sheer annoyance. That friction used to be a quiet least-privilege control, and generated code removes it.

The review step doesn't put it back, because an over-broad policy looks the same as a correct one at a glance. The JSON is valid, terraform plan is clean, the viewset passes its tests, and the diff has the shape a reviewer expects. If the tests were generated alongside the code they test the happy path, which works fine with too much access. So the access model of the codebase drifts wider one reasonable-looking PR at a time.

This post is a playbook for teams that let assistants touch Terraform, Django views or Postgres schemas: a quick grep pass to find what's already there, what off-the-shelf scanners catch and miss, custom Semgrep and Rego rules for the gaps, a check for half-finished row-level security, and a CI job that fails the build. Every rule and query below was run against fixture files with both bad and good cases.

02

a ten-minute grep pass

Before building anything, find out how bad it is. These patterns work on raw IAM JSON and on Terraform HCL, including jsonencode() blocks, aws_iam_policy_document data sources and arrays split over several lines:

bash
1# service-level wildcards ("s3:*", "dynamodb:*") in JSON, HCL and arrays
2rg -n '"[a-z0-9-]+:\*"' -g '*.tf' -g '*.json'
3
4# a bare "*" as the action or the resource, also as the first item of a multi-line array
5rg -n -U -i 'actions?"?\s*[:=]\s*(\[\s*)?"\*"' -g '*.tf' -g '*.json'
6rg -n -U -i 'resources?"?\s*[:=]\s*(\[\s*)?"\*"' -g '*.tf' -g '*.json'
7
8# NotAction / not_actions, which almost never belongs in an Allow statement
9rg -n -i 'not_?actions?"?\s*[:=]' -g '*.tf' -g '*.json'
10
11# GCP basic roles bound to anything
12rg -n 'roles/(owner|editor)\b' -g '*.tf'
13
14# DRF views that expose a whole table
15rg -n 'queryset\s*=\s*\w+\.objects\.all\(\)' -g '*.py'

The old single-line pattern people pass around, something like "Action":\s*"s3:\*", misses arrays, HCL and NotAction, which together are most of what assistants actually write. The patterns above have their own limits: a bare "*" that isn't the first item of an array slips through, and "Resource": "*" is sometimes required because some actions don't support resource-level permissions. Treat the output as a list to read, not a gate. The gate comes later, from tools that parse the structure instead of the text.

NotAction deserves a word because it reads like a restriction. "Effect": "Allow", "NotAction": "iam:*", "Resource": "*" grants every action in every AWS service except IAM, which is close to admin. An assistant reaches for it when asked to "allow everything except IAM", and a reviewer skimming for * in the Action field won't see it.

03

what off-the-shelf scanners catch

The claim that SAST tools miss over-broad IAM isn't true anymore, at least for infrastructure code. Running Checkov 3.4 on the fixtures flagged "Action": "*" (CKV_AWS_1 and CKV_AWS_49 on policy documents), wildcard resources on restrictable actions (CKV_AWS_355 for aws_iam_policy, CKV_AWS_356 for aws_iam_policy_document), and the project-level roles/editor binding (CKV_GCP_117, "basic roles are not used at project level", plus CKV_GCP_42 and CKV_GCP_49). Trivy, which absorbed tfsec (the tfsec README now says it "is now part of Trivy"), flagged the unrestricted S3 policy and the editor binding with its default checks. Turn both on. They're free and they cover the worst cases.

What they didn't flag was dynamodb:* scoped to a single table ARN, or sqs:* on one queue. That's a reasonable choice for a general-purpose scanner, because the resource is pinned, but it still lets a worker that only reads jobs run DeleteTable on that table. Whether that matters depends on the service, which is exactly the kind of judgement a generic rule can't make, so it belongs in your own rules.

The real blind spot is application-level authorization. A taint-flow engine has nothing to say about Invoice.objects.all(), because it's a legal queryset with no untrusted input reaching a dangerous sink. The vulnerability lives in the gap between what the view returns and who should see it, and that intent isn't in the AST. Broken access control is A01 in both the 2021 and the 2025 OWASP Top 10, and this is the most common way it shows up in Django code.

04

the django idor and the rules that catch it

Here's the default an assistant tends to produce, next to the fix:

python
1class LeakyInvoiceViewSet(viewsets.ModelViewSet):
2 queryset = Invoice.objects.all()
3 serializer_class = InvoiceSerializer
4 permission_classes = [IsAuthenticated]
5
6
7class InvoiceViewSet(viewsets.ModelViewSet):
8 serializer_class = InvoiceSerializer
9 permission_classes = [IsAuthenticated]
10
11 def get_queryset(self):
12 return Invoice.objects.filter(owner=self.request.user)
13
14 def perform_create(self, serializer):
15 serializer.save(owner=self.request.user)

With the leaky version any logged-in user can fetch /invoices/<id>/ for any ID. Scoping get_queryset fixes retrieve, update, delete and list in one place, because DRF's generic views look objects up through it, so someone else's invoice comes back as a 404 rather than a 403 and doesn't confirm that the ID exists. has_object_permission isn't a substitute: the DRF permissions docs say object-level checks run when get_object() is called and are not applied to each instance in a list, and they recommend filtering the queryset instead. perform_create matters too, since without it a client can post someone else's owner if the serializer exposes the field.

Pin the behaviour with tests that log in as one user and poke at another user's rows. These run against Django 6.1 and DRF 3.18:

python
1class InvoiceAccessTests(TestCase):
2 def setUp(self):
3 User = get_user_model()
4 self.alice = User.objects.create_user("alice")
5 self.bob = User.objects.create_user("bob")
6 self.bobs_invoice = Invoice.objects.create(owner=self.bob, total_cents=4200)
7 self.client = APIClient()
8 self.client.force_authenticate(self.alice)
9
10 def test_cannot_read_other_users_invoice(self):
11 r = self.client.get(f"/invoices/{self.bobs_invoice.pk}/")
12 self.assertEqual(r.status_code, 404)
13
14 def test_cannot_delete_other_users_invoice(self):
15 r = self.client.delete(f"/invoices/{self.bobs_invoice.pk}/")
16 self.assertEqual(r.status_code, 404)
17 self.assertTrue(Invoice.objects.filter(pk=self.bobs_invoice.pk).exists())

Tests only protect the views someone remembered to test, so add Semgrep rules that look at every viewset. The first rule fails on a class-level .objects.all() with no get_queryset override. The second warns on a get_queryset that never touches self.request.user, which catches Invoice.objects.select_related("customer").all() while letting self.request.user.organization style tenant scoping through:

yaml
1rules:
2 - id: drf-viewset-unscoped-queryset
3 languages: [python]
4 severity: ERROR
5 message: $VIEW exposes $MODEL.objects.all() with no get_queryset. Any authenticated user can read any row by ID.
6 patterns:
7 - pattern: |
8 class $VIEW(...):
9 ...
10 queryset = $MODEL.objects.all()
11 ...
12 - pattern-not: |
13 class $VIEW(...):
14 ...
15 def get_queryset(self, ...):
16 ...
17 ...
18
19 - id: drf-get-queryset-ignores-user
20 languages: [python]
21 severity: WARNING
22 message: get_queryset never looks at self.request.user. Check that rows are scoped to the caller.
23 patterns:
24 - pattern: |
25 def get_queryset(self, ...):
26 ...
27 - pattern-not: |
28 def get_queryset(self, ...):
29 ...
30 <... self.request.user ...>
31 ...

Some views really are public, like a list of countries. Mark those with # nosemgrep: drf-viewset-unscoped-queryset on the class line and a comment saying why, so the exception is visible in the diff and gets reviewed like any other change. The rule is a heuristic, so it won't catch scoping through a custom manager or a mixin, and if your codebase scopes that way, write the rule against your mixin's name instead.

05

row-level security that was only half written

Postgres RLS is a good backstop for multi-tenant data, and it's also easy to get half right, which is what generated migrations tend to do. They add ENABLE ROW LEVEL SECURITY, maybe forget the policy, and connect the app as the table owner. The PostgreSQL docs spell out the rules that matter. RLS enabled with no policy means default-deny, table owners bypass RLS unless the table has FORCE ROW LEVEL SECURITY, and superusers and roles with BYPASSRLS always bypass it. Here's what that looks like on Postgres 18, run as a superuser:

sql
1CREATE ROLE app_owner;
2CREATE ROLE app_user;
3CREATE SCHEMA app AUTHORIZATION app_owner;
4GRANT USAGE ON SCHEMA app TO app_user;
5
6SET ROLE app_owner;
7CREATE TABLE app.invoices (id bigint PRIMARY KEY, tenant_id bigint NOT NULL, total_cents bigint NOT NULL);
8INSERT INTO app.invoices VALUES (1, 1, 1000), (2, 2, 2000);
9GRANT SELECT ON app.invoices TO app_user;
10ALTER TABLE app.invoices ENABLE ROW LEVEL SECURITY;
11
12SET ROLE app_user; SELECT count(*) FROM app.invoices; -- 0: no policy means deny
13SET ROLE app_owner; SELECT count(*) FROM app.invoices; -- 2: the owner skips RLS
14
15ALTER TABLE app.invoices FORCE ROW LEVEL SECURITY;
16SELECT count(*) FROM app.invoices; -- 0: now the owner is filtered too

How this fails depends on how the app connects. If it connects as a non-owner role, a forgotten policy breaks the feature loudly enough that someone notices. If it connects as the owner, which is common when the same role runs migrations and serves traffic, everything works and nothing is filtered. And if it connects as postgres, no amount of FORCE helps. This query lists every table that has RLS enabled but no policy, or that isn't forced:

sql
1SELECT c.oid::regclass AS table_name,
2 c.relforcerowsecurity AS forced,
3 count(p.polname) AS policies,
4 pg_get_userbyid(c.relowner) AS owner
5FROM pg_class c
6JOIN pg_namespace n ON n.oid = c.relnamespace
7LEFT JOIN pg_policy p ON p.polrelid = c.oid
8WHERE c.relkind IN ('r', 'p')
9 AND c.relrowsecurity
10 AND n.nspname NOT IN ('pg_catalog', 'information_schema')
11GROUP BY c.oid, c.relforcerowsecurity, c.relowner
12HAVING count(p.polname) = 0 OR NOT c.relforcerowsecurity
13ORDER BY 1;

If your app genuinely connects as a separate non-owner role, drop the OR NOT c.relforcerowsecurity part, though we'd keep it anyway because roles have a way of getting merged later. One more detail from the same demo: a policy written as current_setting('app.tenant_id')::bigint raises an error when the setting is missing, while current_setting('app.tenant_id', true) returns NULL and silently matches nothing. We prefer the error, because a request that forgot to set the tenant should fail loudly.

06

policy as code on the terraform plan

The Semgrep rules for Terraform are short, and because they're structural they catch arrays, single strings and jsonencode() alike:

yaml
1rules:
2 - id: iam-wildcard-action
3 languages: [hcl]
4 severity: ERROR
5 message: IAM statement grants a wildcard action ($ACT). List the actions the service calls.
6 patterns:
7 - pattern-either:
8 - pattern: actions = [..., $ACT, ...]
9 - pattern: Action = [..., $ACT, ...]
10 - pattern: Action = $ACT
11 - metavariable-regex:
12 metavariable: $ACT
13 regex: ^"(\*|[a-zA-Z0-9-]+:\*)"$
14
15 - id: iam-not-action
16 languages: [hcl]
17 severity: ERROR
18 message: NotAction with Allow grants everything except the listed actions.
19 pattern-either:
20 - pattern: not_actions = $X
21 - pattern: NotAction = $X
22
23 - id: gcp-basic-role
24 languages: [hcl]
25 severity: ERROR
26 message: Basic role $ROLE on a project. Use a predefined or custom role.
27 patterns:
28 - pattern: role = $ROLE
29 - metavariable-regex:
30 metavariable: $ROLE
31 regex: ^"roles/(owner|editor)"$

Source-level rules can't see through modules, variables or a policy assembled from locals, though. For that, check the plan instead of the source. terraform show -json on a saved plan gives you every resource with the attribute values known at plan time, including every policy as a rendered JSON string, whether it came from jsonencode() or a policy document data source. The aws_iam_policy_document data source renders a single action as a string rather than a one-item array, while jsonencode() keeps whatever you wrote, so the Rego has to handle both. This conftest policy (OPA 1.x syntax) denies wildcard actions, Allow with NotAction, and GCP basic roles outside an explicit allowlist:

rego
1package main
2
3import rego.v1
4
5policy_types := {"aws_iam_policy", "aws_iam_role_policy", "aws_iam_user_policy", "aws_iam_group_policy"}
6
7basic_roles := {"roles/owner", "roles/editor"}
8
9# Exceptions are reviewed in the same PR that adds them.
10allowed_basic := {"google_project_iam_member.ci_bootstrap"}
11
12as_list(x) := x if is_array(x)
13
14as_list(x) := [x] if is_string(x)
15
16statements(p) := p.Statement if is_array(p.Statement)
17
18statements(p) := [p.Statement] if is_object(p.Statement)
19
20wildcard(a) if a == "*"
21
22wildcard(a) if endswith(a, ":*")
23
24deny contains msg if {
25 rc := input.resource_changes[_]
26 rc.type in policy_types
27 doc := json.unmarshal(rc.change.after.policy)
28 st := statements(doc)[_]
29 st.Effect == "Allow"
30 a := as_list(st.Action)[_]
31 wildcard(a)
32 msg := sprintf("%s grants wildcard action %q", [rc.address, a])
33}
34
35deny contains msg if {
36 rc := input.resource_changes[_]
37 rc.type in policy_types
38 doc := json.unmarshal(rc.change.after.policy)
39 st := statements(doc)[_]
40 st.Effect == "Allow"
41 st.NotAction
42 msg := sprintf("%s uses Allow with NotAction", [rc.address])
43}
44
45deny contains msg if {
46 rc := input.resource_changes[_]
47 startswith(rc.type, "google_project_iam_")
48 rc.change.after.role in basic_roles
49 not rc.address in allowed_basic
50 msg := sprintf("%s binds basic role %s", [rc.address, rc.change.after.role])
51}

On the fixture plan this reported five failures (the s3:*, dynamodb:* and sqs:* policies, the NotAction statement, and the roles/editor binding) and passed the scoped s3:GetObject policy and the roles/storage.objectViewer binding. Write a couple of conftest verify unit tests for the allowlist and the single-statement case so the policy doesn't rot when someone edits it.

Rules tell you a grant is too wide, but not what the right one is. For that, use the cloud's own usage data instead of asking the assistant again. IAM Access Analyzer policy generation reads up to 90 days of CloudTrail activity for a role or user and produces a policy template with the actions it saw. It can't see action-level activity for data events such as S3 object reads, and it never includes iam:PassRole, so you'll fill those in by hand. On GCP, the IAM recommender looks at up to 90 days of permission usage and suggests removing a role or replacing it with a smaller one. Recommendations for basic roles are free at project, folder and organization level, and the rest need a paid Security Command Center tier. Neither applies anything automatically, which is how it should be.

07

wiring it into ci and review

All of this belongs in a separate CI job so it shows up as its own check and can't get buried under formatting noise. This workflow passes actionlint:

yaml
1name: permissions
2
3on:
4 pull_request:
5
6permissions:
7 contents: read
8
9jobs:
10 authz-rules:
11 runs-on: ubuntu-latest
12 steps:
13 - uses: actions/checkout@v7
14 - uses: astral-sh/setup-uv@v10
15 - name: Semgrep (custom rules only)
16 run: uvx semgrep scan --metrics=off --config ci/semgrep/ --error .
17 - name: Checkov
18 run: uvx checkov -d infra --framework terraform --compact --quiet
19
20 terraform-policy:
21 runs-on: ubuntu-latest
22 steps:
23 - uses: actions/checkout@v7
24 - uses: hashicorp/setup-terraform@v4
25 with:
26 terraform_wrapper: false
27 - name: Plan to JSON
28 working-directory: infra
29 run: |
30 terraform init -input=false
31 terraform plan -input=false -lock=false -out=tfplan.bin
32 terraform show -json tfplan.bin > plan.json
33 - name: Conftest
34 run: >
35 docker run --rm -v "$PWD:/project" -w /project
36 openpolicyagent/conftest:v0.71.1
37 test infra/plan.json -p ci/policy
38
39 rls:
40 runs-on: ubuntu-latest
41 services:
42 postgres:
43 image: postgres:18
44 env:
45 POSTGRES_PASSWORD: postgres
46 ports:
47 - 5432:5432
48 options: >-
49 --health-cmd pg_isready
50 --health-interval 5s
51 --health-timeout 5s
52 --health-retries 10
53 env:
54 DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
55 steps:
56 - uses: actions/checkout@v7
57 - name: Apply migrations
58 run: ./scripts/migrate.sh
59 - name: Fail on RLS tables without policies or FORCE
60 run: |
61 rows=$(psql "$DATABASE_URL" -tA -v ON_ERROR_STOP=1 -f ci/rls-check.sql)
62 if [ -n "$rows" ]; then
63 echo "$rows"
64 exit 1
65 fi

The plan step needs cloud credentials for any data source that reads real infrastructure, so in practice it runs with a read-only role through OIDC, and ./scripts/migrate.sh stands in for whatever applies your migrations. The --config ci/semgrep/ flag runs only your own rules, which keeps the job fast and the findings specific.

Make every one of these fail the build. A warning is something the team learns to scroll past within a week, while a red check forces someone to either narrow the grant or write an exception with a reason in the same diff. That's the friction the assistant took away, put back at the one point where the code enters the main branch.

The last piece is a habit for reviewers. When a PR adds or changes an IAM policy, a role binding, a permission class, a get_queryset or an RLS policy, the reviewer writes one sentence in the thread: the smallest set of access this needs, and whether the code matches it. For most PRs that takes thirty seconds. It works because it forces the reviewer to read the grant instead of recognising its shape, and the shape is exactly what generated code gets right.

Thanks for reading.
Johnny Unar

Written by

Johnny Unar

Keep reading

Stop patching.Start building.

Tell us what's slowing you down. We'll bring coffee and opinions.