Laslo
Lasloā€¢6mo ago

Undefined IP on requests

I'm using express and mongo on Deno, and i'm also trying to use express-rate-limit, however, i'm getting the following error on rate-limit because the request.ip is undefined:
ValidationError: An undefined 'request.ip' was detected. This might indicate a misconfiguration or the connection being destroyed prematurely. See https://express-rate-limit.github.io/ERR_ERL_UNDEFINED_IP_ADDRESS/ for more information.
at Object.ip (file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:97:13)
at Object.wrappedValidations.<computed> [as ip] (file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:296:22)
at Object.keyGenerator (file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:549:20)
at file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:601:32
at Object.runMicrotasks (ext:core/01_core.js:934:26)
at processTicksAndRejections (ext:deno_node/_next_tick.ts:53:10)
at runNextTicks (ext:deno_node/_next_tick.ts:71:3)
at eventLoopTick (ext:core/01_core.js:189:21)
at async file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:583:5 {
name: "ValidationError",
code: "ERR_ERL_UNDEFINED_IP_ADDRESS",
help: "https://express-rate-limit.github.io/ERR_ERL_UNDEFINED_IP_ADDRESS/"
}
ValidationError: An undefined 'request.ip' was detected. This might indicate a misconfiguration or the connection being destroyed prematurely. See https://express-rate-limit.github.io/ERR_ERL_UNDEFINED_IP_ADDRESS/ for more information.
at Object.ip (file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:97:13)
at Object.wrappedValidations.<computed> [as ip] (file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:296:22)
at Object.keyGenerator (file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:549:20)
at file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:601:32
at Object.runMicrotasks (ext:core/01_core.js:934:26)
at processTicksAndRejections (ext:deno_node/_next_tick.ts:53:10)
at runNextTicks (ext:deno_node/_next_tick.ts:71:3)
at eventLoopTick (ext:core/01_core.js:189:21)
at async file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:583:5 {
name: "ValidationError",
code: "ERR_ERL_UNDEFINED_IP_ADDRESS",
help: "https://express-rate-limit.github.io/ERR_ERL_UNDEFINED_IP_ADDRESS/"
}
I've found this answer on a similar question that suggests adding app.set('trust proxy', 1); i tried it, but the ip continues undefined.
19 Replies
Deno AI Helper
Deno AI Helperā€¢6mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the information you've provided, there are a couple of things to note: 1. The express-rate-limit package is designed for use with Express, a web application framework for Node.js, not Deno. Deno has its own set of modules and standard libraries, and it does not natively support Node.js modules without some form of compatibility layer. 2. The error message and stack trace you've provided seem to indicate that you're trying to use Node.js modules (express-slow-down and express-rate-limit) within a Deno environment, as evidenced by the file paths containing node_modules. This is not directly supported by Deno. 3. The app.set('trust proxy', 1); configuration is specific to Express in a Node.js environment. Deno's standard library and third-party modules for Deno do not recognize this setting. If you are trying to implement rate limiting in a Deno application, you would need to look for a Deno-specific module or write your own middleware to handle this functionality. You can search for Deno modules on https://deno.land/x or other Deno-related module registries. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. If you are working within the Deno runtime, it's important to use Deno-compatible modules and patterns.
Laslo
Lasloā€¢6mo ago
How to reproduce:
"use strict"

import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import rateLimit from 'express-rate-limit';

const app = express();

mongoose.connect("...");
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() { console.log('Connected to MongoDB'); });

app.use(bodyParser.json());
app.set('trust proxy', 1);

const limiter = rateLimit({ windowMs: 1 * 60 * 1000, max: 3, });
app.post('/api/users', limiter, async (req, res) =>
{
console.log('ip:', req.ip);
console.log('conn:', req.connection)
console.log('conn.remoteAddress:', req.connection.remoteAddress)
console.log('client:', req.client);
console.log('headers:', req.headers);
console.log('rawHeaders:', req.rawHeaders);
console.log('X-Forwarded-For:', req.headers['x-forwarded-for']);
console.dir('req:', req);
try {
const variable = await db.collection("users").findOne({ [req.body.user]: { $exists: true } })
if (!variable) return res.status(404).send('Not found');
res.send(variable);
}
catch (error) { res.status(500).send('Internal Server Error'); }
});

app.listen(5000, () => { console.log(`running`); });
"use strict"

import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import rateLimit from 'express-rate-limit';

const app = express();

mongoose.connect("...");
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() { console.log('Connected to MongoDB'); });

app.use(bodyParser.json());
app.set('trust proxy', 1);

const limiter = rateLimit({ windowMs: 1 * 60 * 1000, max: 3, });
app.post('/api/users', limiter, async (req, res) =>
{
console.log('ip:', req.ip);
console.log('conn:', req.connection)
console.log('conn.remoteAddress:', req.connection.remoteAddress)
console.log('client:', req.client);
console.log('headers:', req.headers);
console.log('rawHeaders:', req.rawHeaders);
console.log('X-Forwarded-For:', req.headers['x-forwarded-for']);
console.dir('req:', req);
try {
const variable = await db.collection("users").findOne({ [req.body.user]: { $exists: true } })
if (!variable) return res.status(404).send('Not found');
res.send(variable);
}
catch (error) { res.status(500).send('Internal Server Error'); }
});

app.listen(5000, () => { console.log(`running`); });
Testing locally i'm able to see my ip on all these variables, while on Deno this is what being printed:
ip: undefined
conn: FakeSocket {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
remoteAddress: undefined,
remotePort: undefined,
encrypted: undefined,
writable: true,
readable: true,
[Symbol(kCapture)]: false
}
conn.remoteAddress: undefined
client: undefined
headers:
{
"accept-encoding": "gzip, deflate",
"accept-language": "en-US,en,*",
"content-length": "14",
"content-type": "application/json",
host: "....deno.dev",
"user-agent": "Mozilla/5.0"
}
rawHeaders: undefined
client: undefined
X-Forwarded-For: undefined
req: <ref *1> IncomingMessageForServer {
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: [],
flowing: true,
ended: true,
endEmitted: true,
reading: false,
constructed: true,
sync: false,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
destroyed: true,
errored: null,
closed: true,
closeEmitted: false,
defaultEncoding: "utf8",
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: false,
dataEmitted: true,
decoder: null,
encoding: null,
[Symbol(kPaused)]: false
},
_read: [AsyncFunction: read],
_destroy: [Function: destroy],
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
url: "/api/users",
method: "POST",
next: [Function: next],
baseUrl: "",
originalUrl: "/api/users",
_parsedUrl: Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null
ip: undefined
conn: FakeSocket {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
remoteAddress: undefined,
remotePort: undefined,
encrypted: undefined,
writable: true,
readable: true,
[Symbol(kCapture)]: false
}
conn.remoteAddress: undefined
client: undefined
headers:
{
"accept-encoding": "gzip, deflate",
"accept-language": "en-US,en,*",
"content-length": "14",
"content-type": "application/json",
host: "....deno.dev",
"user-agent": "Mozilla/5.0"
}
rawHeaders: undefined
client: undefined
X-Forwarded-For: undefined
req: <ref *1> IncomingMessageForServer {
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: [],
flowing: true,
ended: true,
endEmitted: true,
reading: false,
constructed: true,
sync: false,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
destroyed: true,
errored: null,
closed: true,
closeEmitted: false,
defaultEncoding: "utf8",
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: false,
dataEmitted: true,
decoder: null,
encoding: null,
[Symbol(kPaused)]: false
},
_read: [AsyncFunction: read],
_destroy: [Function: destroy],
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
url: "/api/users",
method: "POST",
next: [Function: next],
baseUrl: "",
originalUrl: "/api/users",
_parsedUrl: Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null
The ip is undefined and its missing all headers, i'm not sure if this is a bug or if i'm missing something, any help is appreciated. obs: i had to split the question on multiple comments because discord was limiting number of characters https://github.com/denoland/deno/issues/21980 sad, this already had been reported at 2022 and still didnt fixed
marvinh.
marvinh.ā€¢5mo ago
FYI: It turned out to be a bug in the node compat layer. The fix for this will be included in the next Deno release
Laslo
Lasloā€¢5mo ago
ty, how do i know when this version is released? also, will be possible to read the ip as req.ip?
Leokuma
Leokumaā€¢5mo ago
Releases are published here: https://github.com/denoland/deno/releases They are usually published in #announcements too Normally Deno will also check for newer versions automatically and show you a message on the terminal, unless you've disabled it
Laslo
Lasloā€¢5mo ago
Yes, but the ip will be on request.ip or will need to look for it somewhere else?
marvinh.
marvinh.ā€¢5mo ago
With the bug fix express works as expected and the IP will be accessible on request.ip The fact that it wasn't accessible was the bug that the PR fixes
Laslo
Lasloā€¢5mo ago
No description
Laslo
Lasloā€¢5mo ago
i saw that Deno updated to 1.4.0 and the changelog mention your commit but I continue getting undefined when reading the request ip
marvinh.
marvinh.ā€¢5mo ago
Deno Deploy hasn't been updated to Deno 1.4.0 yet
Laslo
Lasloā€¢5mo ago
No description
marvinh.
marvinh.ā€¢5mo ago
yup, the fix is in CLI, but not on Deno Deploy yet
Laslo
Lasloā€¢5mo ago
i thought was the same, where i can follow about the deploy
marvinh.
marvinh.ā€¢5mo ago
I don't think that part is communicated publicly. Typically it happens in 1-2 weeks. I've been pushing internally for a while to sync Deno Deploy more timely so that it works the same in Deno Deploy as it does in CLI
Laslo
Lasloā€¢5mo ago
hello, do you have any idea if it still will take long to your fixes get in deploy?
marvinh.
marvinh.ā€¢5mo ago
My understanding is that the fix is already available in Deploy. Do you still see the error?
Laslo
Lasloā€¢5mo ago
No description
Laslo
Lasloā€¢5mo ago
? :samurai_deno: šŸ‘Ž
marvinh.
marvinh.ā€¢5mo ago
If it still errors then it looks like it's not updated on Deploy yet. We're currently in the middle of restructuring the relationship between CLI and Deno Deploy to ensure that they are more in sync and that bug fixes in the CLI are available on Deploy in a timely manner. This is work that's more difficult than anticipated and I don't have a timeline when the fix will be available.
More Posts