Skip to content

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

4.6 KB
Raw Blame
rules:
    - id: hog-json-parse-outside-json-helper
      message: |
          `JSON.parse` outside `hog/src/core/json.ts`. Its result is `any`, so the code that reads
          it trusts a shape no one checked, and a file or answer of another shape fails far from
          where hog read it.

          Read JSON with a zod schema instead: `parseJson(text, Schema, failure)` when a bad document
          must stop the command, `parseJsonOrUndefined(text, Schema)` when it counts as missing. Tests use `readJson` from
          `@/testing`.
      severity: ERROR
      languages: [typescript, javascript]
      paths:
          include:
              - '/hog/**'
          exclude:
              - '/hog/src/core/json.ts'
      # The bare reference also catches `lines.map(JSON.parse)`.
      pattern: JSON.parse
      metadata:
          category: best-practice

    - id: hog-host-cast-outside-host-module
      message: |
          A cast to a branded type outside the module that defines the brand. The brand promises
          the value passed its schema, and a cast skips that check.

          Get the value from the brand's factory, such as `parseHost`, `parseProjectId` or
          `parsePersonalApiKey`, or embed the brand's schema in the schema of the document it comes
          from.
      severity: ERROR
      languages: [typescript]
      paths:
          include:
              - '/hog/**'
          exclude:
              - '/hog/src/posthog/host.ts'
      # A regex, because semgrep binds only `Array` in `x as Array<Host>`. The `not` patterns drop
      # matches inside comments, strings and import renames.
      patterns:
          - pattern-regex: (?<=[\w$)\]'"`>]\s)as\s+[\w$.<>,|&\[\]\s()]*?(?<![\w.$])(Host|HostKey)(?![\w$])
          - pattern-not-regex: (?<!:)//[^\n]*
          - pattern-not-regex: /\*[\s\S]*?\*/
          - pattern-not-regex: '"(?:[^"\\\n]|\\.)*"'
          - pattern-not-regex: "'(?:[^'\\\\\\n]|\\\\.)*'"
          - pattern-not-regex: '`(?:[^`\\]|\\.)*`'
          - pattern-not-inside: import { ... } from '...'
      metadata:
          category: best-practice

    - id: hog-id-cast-outside-ids-module
      message: |
          A cast to a branded type outside the module that defines the brand. The brand promises
          the value passed its schema, and a cast skips that check.

          Get the value from the brand's factory, such as `parseHost`, `parseProjectId` or
          `parsePersonalApiKey`, or embed the brand's schema in the schema of the document it comes
          from.
      severity: ERROR
      languages: [typescript]
      paths:
          include:
              - '/hog/**'
          exclude:
              - '/hog/src/posthog/ids.ts'
      # A regex, because semgrep binds only `Array` in `x as Array<Host>`. The `not` patterns drop
      # matches inside comments, strings and import renames.
      patterns:
          - pattern-regex: (?<=[\w$)\]'"`>]\s)as\s+[\w$.<>,|&\[\]\s()]*?(?<![\w.$])(ProjectId|OrganizationId)(?![\w$])
          - pattern-not-regex: (?<!:)//[^\n]*
          - pattern-not-regex: /\*[\s\S]*?\*/
          - pattern-not-regex: '"(?:[^"\\\n]|\\.)*"'
          - pattern-not-regex: "'(?:[^'\\\\\\n]|\\\\.)*'"
          - pattern-not-regex: '`(?:[^`\\]|\\.)*`'
          - pattern-not-inside: import { ... } from '...'
      metadata:
          category: best-practice

    - id: hog-token-cast-outside-tokens-module
      message: |
          A cast to a branded type outside the module that defines the brand. The brand promises
          the value passed its schema, and a cast skips that check.

          Get the value from the brand's factory, such as `parseHost`, `parseProjectId` or
          `parsePersonalApiKey`, or embed the brand's schema in the schema of the document it comes
          from.
      severity: ERROR
      languages: [typescript]
      paths:
          include:
              - '/hog/**'
          exclude:
              - '/hog/src/posthog/tokens.ts'
      # A regex, because semgrep binds only `Array` in `x as Array<Host>`. The `not` patterns drop
      # matches inside comments, strings and import renames.
      patterns:
          - pattern-regex: (?<=[\w$)\]'"`>]\s)as\s+[\w$.<>,|&\[\]\s()]*?(?<![\w.$])(PersonalApiKey|ProjectToken|ProjectSecretApiKey|TeamSecretToken|OAuthAccessToken|OAuthRefreshToken|WorkloadToken|BearerToken)(?![\w$])
          - pattern-not-regex: (?<!:)//[^\n]*
          - pattern-not-regex: /\*[\s\S]*?\*/
          - pattern-not-regex: '"(?:[^"\\\n]|\\.)*"'
          - pattern-not-regex: "'(?:[^'\\\\\\n]|\\\\.)*'"
          - pattern-not-regex: '`(?:[^`\\]|\\.)*`'
          - pattern-not-inside: import { ... } from '...'
      metadata:
          category: best-practice