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:
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:
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:
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:
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:
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:
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:
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:
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:
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.
