GithubHelp home page GithubHelp logo

option to disable headers about pino-http HOT 6 CLOSED

pinojs avatar pinojs commented on June 2, 2024 1
option to disable headers

from pino-http.

Comments (6)

sethtomy avatar sethtomy commented on June 2, 2024 24

For those coming to this as I did, please be aware that the serializer will mutate the incoming request.

If your intention is to only remove from the logs:

export const httpLogger = pinoHttp({
  logger,
  redact: {
      paths: 'req.headers.authorization',
    }
  },
})

from pino-http.

jtokoph avatar jtokoph commented on June 2, 2024 5

Note that something like delete r.headers['cookie'] will remove the cookie from the request. It's generally safer to have the serializer return a new object for a couple reasons:

  • A pure function that doesn't mutate it's input is more likely to be safe and correct.
  • Redacting/blocklists will almost always result in leaked data. For example, you might redact the cookie header, but a user of your API might attempt to set the Authorization header with an access token.
  • Redacting/blocklists will need perpetual maintenance. Standards change, and there could be a new sensitive header that your code didn't expect.

Moving to a pure function that specifically pulls out the values you need in your logs is safer. Here is an example of what I've used:

const logger = require('pino-http')({
  serializers: {
    req: pino.stdSerializers.wrapRequestSerializer((req) => {
      return {
        id: req.raw.id,
        method: req.raw.method,
        path: req.raw.url.split('?')[0], // Remove query params which might be sensitive
        // Allowlist useful headers
        headers: {
          host: req.raw.headers.host,
          'user-agent': req.raw.headers['user-agent'],
          referer: req.raw.headers.referer,
        }
      };
    }),
    res: pino.stdSerializers.wrapResponseSerializer((res) => {
      return {
        statusCode: res.raw.statusCode,
        // Allowlist useful headers
        headers: {
          'content-type': res.raw.headers['content-type'],
          'content-length': res.raw.headers['content-length'],
        }
      };
    }),
  },
});

from pino-http.

kirillgroshkov avatar kirillgroshkov commented on June 2, 2024 2

What worked for me (I needed to only delete cookie from headers, not the whole headers):

export const httpLogger = pinoHttp({
  logger,
  serializers: {
    req: pino.stdSerializers.wrapRequestSerializer(r => {
      delete r.headers['cookie']
      return r
    }),
  },
})

from pino-http.

davidmarkclements avatar davidmarkclements commented on June 2, 2024

So that can be done like so

var http = require('http')
var server = http.createServer(handle)
var logger = require('pino-http')({
  serializers: {
    req: (req) => ({
      id: req.id,
      method: req.method,
      url: req.url
    })
  }
})

function handle (req, res) {
  logger(req, res)
  res.end('hello world')
}

server.listen(3000)

That gets rid of header, and also remoteAddress and remotePort

from pino-http.

davidmarkclements avatar davidmarkclements commented on June 2, 2024

serializers needs to be added to readme

from pino-http.

stephenmathieson avatar stephenmathieson commented on June 2, 2024

perfect, thanks!

from pino-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.