id: npm-access-token

info:
  name: NPM Access Token (fine-grained)
  author: DhiyaneshDK
  severity: high
  reference:
    - https://github.com/praetorian-inc/noseyparker/blob/main/crates/noseyparker/data/default/builtin/rules/npm.yml
    - https://docs.npmjs.com/about-access-tokens
    - https://github.com/github/roadmap/issues/557
    - https://github.blog/changelog/2022-12-06-limit-scope-of-npm-tokens-with-the-new-granular-access-tokens/
  classification:
    cwe-id: CWE-522,CWE-540
  metadata:
    verified: true
    max-request: 1
  tags: npm,token,exposure,vuln

flow: http(1) && http(2)

http:
  - method: GET
    path:
      - "{{BaseURL}}"

    matchers:
      - type: regex
        part: body
        name: token
        regex:
          - "(npm_[A-Za-z0-9]{36})"
        internal: true

  - raw:
      - |
        @Host: https://registry.npmjs.org:443
        GET /-/whoami HTTP/1.1
        Host: registry.npmjs.org
        Authorization: Bearer {{token}}

    disable-path-automerge: true
    matchers:
      - type: dsl
        dsl:
          - 'status_code == 200'
          - 'contains(body, "username")'
          - 'contains(content_type, "application/json")'
        condition: and

    extractors:
      - type: dsl
        dsl:
          - token
# digest: 490a00463044022048737d2636f5fd36981a8b14a3aa524e3d06e34e316293b348740ecda76d943c02204d30beaa7fa025161f40713a93f861305772d8091aa394b9a96c99dbf14e3fa3:922c64590222798bb761d5b6d8e72950