:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(devex): add semgrep-devex framework + first rule (#58298)
Julian Bez6a4b2f01aa16
diff --git a/.github/workflows/ci-security.yaml b/.github/workflows/ci-security.yamlindex 79987896206f..d0b69fd2a7cf 100644--- a/.github/workflows/ci-security.yaml+++ b/.github/workflows/ci-security.yaml@@ -56,7 +56,7 @@ jobs:--error \--metrics=off \--verbose \- common/ ee/ posthog/ products/ services/llm-gateway/ services/stripe-mock/+ common/ ee/ frontend/ posthog/ products/ services/llm-gateway/ services/stripe-mock/semgrep-go:runs-on: ubuntu-latest@@ -193,6 +193,45 @@ jobs:--exclude ./services/ \.+ # Devex / hygiene rules. Two passes:+ # - WARNING-severity rules print findings without failing the job+ # (used while migrating away from a pattern with a known backlog).+ # - ERROR-severity rules fail the job on any finding (regression guards).+ # See `.semgrep/devex-rules/README.md`.+ semgrep-devex:+ runs-on: ubuntu-latest+ timeout-minutes: 10+ container:+ image: semgrep/semgrep++ steps:+ - name: Checkout+ uses: actions/checkout@v6++ # Pass 1: print WARNING-severity findings. No --error — this step+ # does not fail CI. Backlogs from migration-in-progress rules show+ # up here.+ - name: Warnings (informational)+ run: |+ semgrep \+ --config ".semgrep/devex-rules/" \+ --severity=WARNING \+ --metrics=off \+ --verbose \+ common/ ee/ frontend/ posthog/ products/++ # Pass 2: enforce ERROR-severity rules. Any finding fails the+ # job — regression guards live here.+ - name: Errors (blocking)+ run: |+ semgrep \+ --config ".semgrep/devex-rules/" \+ --severity=ERROR \+ --error \+ --metrics=off \+ --verbose \+ common/ ee/ frontend/ posthog/ products/+semgrep-test-rules:runs-on: ubuntu-latesttimeout-minutes: 5@@ -215,6 +254,7 @@ jobs:- semgrep-js- semgrep-products-frontend- semgrep-general+ - semgrep-devex- semgrep-test-rulesname: Semgrep Checks Passruns-on: ubuntu-latest@@ -247,6 +287,10 @@ jobs:echo "semgrep-general did not complete successfully."exit 1fi+ if [[ "${{ needs.semgrep-devex.result }}" != "success" && "${{ needs.semgrep-devex.result }}" != "skipped" ]]; then+ echo "semgrep-devex did not complete successfully."+ exit 1+ fiif [[ "${{ needs.semgrep-test-rules.result }}" != "success" && "${{ needs.semgrep-test-rules.result }}" != "skipped" ]]; thenecho "semgrep-test-rules did not complete successfully."exit 1diff --git a/.semgrep/devex-rules/README.md b/.semgrep/devex-rules/README.mdnew file mode 100644index 000000000000..41d1297dd4a6--- /dev/null+++ b/.semgrep/devex-rules/README.md@@ -0,0 +1,26 @@+# Devex semgrep rules++Devex / hygiene rules for the PostHog codebase. Run by the `semgrep-devex` job in `ci-security.yaml` in two passes:++1. **Warnings (informational)** — `semgrep --severity=WARNING` runs all `severity: WARNING` rules. Findings appear in the CI run output but the step does not fail. Use this mode when a rule has a non-zero backlog of existing violations — the list shows up in CI without blocking master while the codebase is cleaned up.+2. **Errors (blocking)** — `semgrep --severity=ERROR --error` runs all `severity: ERROR` rules and fails CI on any finding. Use this mode for hard regression guards: patterns that should never appear, or migration-style rules whose backlog has been cleaned up.++## Picking a severity++Pick the severity that matches the rule's intent:++- **ERROR** when zero violations are expected — either because the pattern is genuinely new (regression guard) or because a previously-WARNING rule has been cleaned up.+- **WARNING** when there's a known non-zero backlog that will be migrated over time.++A WARNING rule typically gets flipped to ERROR once its backlog hits zero. The rule stays in `.semgrep/devex-rules/`; only the severity changes. To flip a rule:++1. Verify locally that `semgrep --config .semgrep/devex-rules/<rule>.yaml common/ ee/ frontend/ posthog/ products/` returns zero findings.+2. Edit the rule file: change `severity: WARNING` to `severity: ERROR`.+3. Update the rule's `message:` to drop any "informational, not blocking" phrasing — it's now blocking.++## Conventions for new rules++- Decide severity by expected finding count: zero → ERROR; non-zero → WARNING.+- Include the migration playbook (or a pointer to a skill) in the rule's `message:` field — that's what reviewers and authors see when the rule fires.+- Scope with `paths.include:` rather than relying only on `paths.exclude:` — narrower scope means faster scans and clearer intent.+- For legitimate exceptions, prefer `paths.exclude:` for categorical cases (whole dirs/files) and `# nosemgrep: <rule-id>` for case-by-case exemptions at the call site.diff --git a/.semgrep/devex-rules/no-at-current-in-api-urls.yaml b/.semgrep/devex-rules/no-at-current-in-api-urls.yamlnew file mode 100644index 000000000000..2d17f6757055--- /dev/null+++ b/.semgrep/devex-rules/no-at-current-in-api-urls.yaml@@ -0,0 +1,82 @@+rules:+ - id: no-at-current-in-api-urls+ message: |+ `@current` in API URLs is deprecated. Use explicit IDs from+ `currentTeamLogic` / `currentProjectLogic` / `currentOrganizationLogic`+ instead — `currentTeamIdStrict` / `currentProjectIdStrict` /+ `currentOrganizationIdStrict` are non-nullable selectors that throw+ if accessed before the data is loaded.++ Why: `@current` is implicit server-side state. The frontend has the+ resolved ID; sending it explicitly removes a class of bugs caused by+ frontend/backend disagreement about which project/org is "current".++ Context: PR #51110 migrated the bulk of frontend calls. A small set+ of bootstrap files (teamLogic, organizationLogic, projectLogic,+ welcomeDialogLogic, lib/api.ts) and the toolbar still use `@current`+ intentionally — they're exempted via paths.exclude below.++ This rule is currently informational (WARNING severity). Findings+ are the backlog of call sites still to migrate. Once the backlog+ hits zero this rule's severity will be switched to ERROR so+ regressions get blocked.+ languages: [typescript, javascript]+ severity: WARNING+ paths:+ include:+ - 'frontend/'+ - 'products/*/frontend/'+ exclude:+ # tests and fixtures legitimately exercise the `@current` route+ - '**/*.test.ts'+ - '**/*.test.tsx'+ - '**/test/**'+ - '**/__tests__/**'+ - '**/*.stories.tsx'+ - '**/*.stories.ts'+ - '**/__mocks__/**'+ - '**/mocks/**'+ # toolbar runs in third-party page context — separate concern+ - '/frontend/src/toolbar/'+ # documented bootstrap exceptions: explicit ID isn't loaded yet+ - '/frontend/src/scenes/teamLogic.tsx'+ - '/frontend/src/scenes/organizationLogic.tsx'+ - '/frontend/src/scenes/projectLogic.ts'+ - '/frontend/src/scenes/welcome/welcomeDialogLogic.ts'+ - '/frontend/src/lib/api.ts'+ pattern-either:+ # api.<verb>($URL, ...) with @current in the URL string/template literal+ - patterns:+ - pattern-either:+ - pattern: api.get($URL, ...)+ - pattern: api.post($URL, ...)+ - pattern: api.put($URL, ...)+ - pattern: api.patch($URL, ...)+ - pattern: api.delete($URL, ...)+ - pattern: api.create($URL, ...)+ - pattern: api.update($URL, ...)+ - metavariable-regex:+ metavariable: $URL+ regex: '.*@current.*'+ # Semgrep's TypeScript parser treats generic-typed calls+ # (api.create<T>(...)) as a different AST shape that the patterns above+ # don't match — same limitation as documented in prefer-codegen-api.yaml.+ # Fall back to a regex; bounds type args by `(` so nested generics like+ # api.get<PaginatedResponse<Foo>>(...) still match.+ - pattern-regex: '\bapi\.(get|post|put|patch|delete|create|update)<[^(]*>\s*\([^)]*@current[^)]*'+ # raw fetch with full URL+ - patterns:+ - pattern: fetch($URL, ...)+ - metavariable-regex:+ metavariable: $URL+ regex: '.*@current.*'+ # combineUrl for query string building+ - patterns:+ - pattern: combineUrl($URL, ...)+ - metavariable-regex:+ metavariable: $URL+ regex: '.*@current.*'+ metadata:+ category: best-practice+ subcategory:+ - auditdiff --git a/products/tasks/frontend/logics/taskDetailSceneLogic.ts b/products/tasks/frontend/logics/taskDetailSceneLogic.tsindex d84b38a2bac3..ab6d6a4ab4a9 100644--- a/products/tasks/frontend/logics/taskDetailSceneLogic.ts+++ b/products/tasks/frontend/logics/taskDetailSceneLogic.ts@@ -377,6 +377,7 @@ export const taskDetailSceneLogic = kea<taskDetailSceneLogicType>([return () => {}}+ // TODO(no-at-current-in-api-urls): migrate to `currentProjectIdStrict`. Rule misses this site because the URL is bound to a const and passed to fetch via the variable.const streamUrl = `/api/projects/@current/tasks/${props.taskId}/runs/${runId}/stream/`const toolMap = buildToolMap(values.streamEntries)let eventIndex = values.streamEntries.length