-
Any way to set fastify.addHook('onRequest', async (req) => {
req.ip = '01.02.03.04';
}); Plugin looks like: async function internalIpOnly(fastify) {
fastify.addHook('onRequest', async (req, reply) => {
const { ip } = req;
if (
process.env.NODE_ENV === 'production'
&& !ip.startsWith('01.02.03')
) {
reply.status(StatusCodes.FORBIDDEN);
throw new Error(`IP address is not internal: ${ip}`);
}
});
} |
Beta Was this translation helpful? Give feedback.
Answered by
onosendi
Feb 2, 2023
Replies: 3 comments 4 replies
-
Looks great! |
Beta Was this translation helpful? Give feedback.
3 replies
-
I would just move the async function internalIpOnly(fastify) {
if (process.env.NODE_ENV === 'production') {
fastify.addHook('onRequest', async (req, reply) => {
const { ip } = req;
if (
!ip.startsWith('01.02.03')
) {
reply.status(StatusCodes.FORBIDDEN);
throw new Error(`IP address is not internal: ${ip}`);
}
});
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
This seemed to do the trick. UpdateThe better solution would be to use light-my-request's const res = await fastify.inject({
method: 'get',
url: '/foo',
remoteAddress: '01.02.03.04',
}); Old answerfastify.addHook('onRequest', async (req) => {
Object.defineProperty(req, 'ip', {
value: '01.02.03.04',
});
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
onosendi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This seemed to do the trick.
Update
The better solution would be to use light-my-request's
remoteAddress
option:Old answer