Skip to content

Improved injected request detection #4116

@neenhouse

Description

@neenhouse

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: any
  • module version: >= 4.0.2
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi
  • any other relevant information:

What problem are you trying to solve?

We have a use case where we have designed a handler that needs to determine if a request has been injected by @hapi/shot to bypass trusted context validation. Consider the following code:

function skipValidationCheck(req) {
    // inspect request to determine if injected by server, in which case it is already trusted.
    const isRequestInjected = require('@hapi/shot').isInjection(request.raw.req);
    return isRequestInjected;
}

This approach works when a single version of @hapi/shot is installed locally because the underlying symbol on the request object prototype will match the one from the singleton method exported from @hapi/shot. This however breaks down when multiple versions of @hapi/shot are installed due to exported symbols mismatching.

Do you have a new or modified API suggestion to solve the problem?

Proposal is to do one of the following:

  • Modify Request.prototype to add isInjected property boolean to consistently identify injected requests.
  • or, modify Symbols to export less unique value

I would probably lean to the first bullet as it maintains uniqueness check of symbol, but I don't fully understand the requirements of that. Second bullet would be a simpler approach.

It might also help to modify isInjected documentation to clarify returns true only when versions of module match, unless that was not the intended behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions