javascript.express.security.express-wkhtml-injection.express-wkhtmltopdf-injection

profile photo of returntocorpreturntocorp
Author
3,077
Download Count*

If unverified user data can reach the wkhtmltopdf methods it can result in Server-Side Request Forgery vulnerabilities

Run Locally

Run in CI

Defintion

rules:
  - id: express-wkhtmltopdf-injection
    message: If unverified user data can reach the `wkhtmltopdf` methods it can
      result in Server-Side Request Forgery vulnerabilities
    metadata:
      owasp:
        - A10:2021 - Server-Side Request Forgery (SSRF)
      cwe:
        - "CWE-918: Server-Side Request Forgery (SSRF)"
      category: security
      technology:
        - express
      references:
        - https://www.npmjs.com/package/wkhtmltopdf
      cwe2022-top25: true
      cwe2021-top25: true
      subcategory:
        - vuln
      likelihood: MEDIUM
      impact: MEDIUM
      confidence: LOW
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]
    languages:
      - javascript
      - typescript
    severity: ERROR
    mode: taint
    pattern-sources:
      - patterns:
          - pattern-either:
              - pattern-inside: function ... ($REQ, $RES) {...}
              - pattern-inside: function ... ($REQ, $RES, $NEXT) {...}
              - patterns:
                  - pattern-either:
                      - pattern-inside: $APP.$METHOD(..., function $FUNC($REQ, $RES) {...})
                      - pattern-inside: $APP.$METHOD(..., function $FUNC($REQ, $RES, $NEXT) {...})
                  - metavariable-regex:
                      metavariable: $METHOD
                      regex: ^(get|post|put|head|delete|options)$
          - pattern-either:
              - pattern: $REQ.query
              - pattern: $REQ.body
              - pattern: $REQ.params
              - pattern: $REQ.cookies
              - pattern: $REQ.headers
      - patterns:
          - pattern-either:
              - pattern-inside: |
                  ({ $REQ }: Request,$RES: Response, $NEXT: NextFunction) =>
                  {...}
              - pattern-inside: |
                  ({ $REQ }: Request,$RES: Response) => {...}
          - focus-metavariable: $REQ
          - pattern-either:
              - pattern: params
              - pattern: query
              - pattern: cookies
              - pattern: headers
              - pattern: body
    pattern-sinks:
      - patterns:
          - pattern-inside: |
              $WK = require('wkhtmltopdf');
              ...
          - pattern: $WK($SINK,...)
          - focus-metavariable: $SINK

Examples

express-wkhtml-injection.js

const express = require('express')
const app = express()
const port = 3000
const wkhtmltopdf = require('wkhtmltopdf')
const wkhtmltoimage = require('wkhtmltoimage')

app.get('/', async (req, res) => {
  // ruleid: express-wkhtmltopdf-injection
  const pdf = wkhtmltopdf(req.query.q, { output: 'vuln.pdf' })
  res.send(pdf)
})

app.post('/ok', async (req, res) => {
  // ok: express-wkhtmltopdf-injection
  const pdf = wkhtmltopdf('<html></html>', { output: 'vuln.pdf' })
  res.send(pdf)
})

app.post('/test', async (req, res) => {
  // ruleid: express-wkhtmltoimage-injection
  const img = wkhtmltoimage.generate(req.body, { output: 'vuln.pdf' })
  res.send(img)
})

app.post('/test-ok', async (req, res) => {
  // ok: express-wkhtmltoimage-injection
  const data = '<html></html>'
  const img = wkhtmltoimage.generate(data, { output: 'vuln.pdf' })
  res.send(img)
})

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))