Skip to content

Commit 2557bbf

Browse files
committed
feat: Don't allow negative values for expiresIn
1 parent 1aed252 commit 2557bbf

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ unless the `client` options is provided to override them.
2121
☣️ Legacy reap behaviors use DynamoDB [`scan`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/classes/scancommand.html)
2222
functionality that can incur significant costs. Should instead enable [DynamoDB TTL](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html)
2323
and select the `expires` field. TODO should we just remove it since we're already making a breaking change?
24-
- `expiresIn` Optional set the number of seconds for DynamoDB TTL. Defaults to the cookie's maxAge.
24+
- `expiresIn` Optional set the number of seconds for DynamoDB TTL. Defaults to the cookie's maxAge. Must be a positive integer.
2525

2626
## Usage
2727

lib/connect-dynamodb.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,20 @@ module.exports = function (connect) {
6868
if (this.reapInterval > 0) {
6969
this._reap = setInterval(this.reap.bind(this), this.reapInterval);
7070
}
71-
this.expiresIn = null == options.expiresIn ? 0 : options.expiresIn
71+
if (options.expiresIn) {
72+
if (!Number.isInteger(options.expiresIn)) {
73+
console.warn("`expiresIn` must be an integer. Reverting to default behaviour");
74+
this.expiresIn = 0;
75+
}
76+
else if (options.expiresIn < 0) {
77+
console.warn("Negative `expiresIn` values are not supported. Reverting to default behaviour");
78+
this.expiresIn = 0;
79+
} else {
80+
this.expiresIn = options.expiresIn;
81+
}
82+
} else {
83+
this.expiresIn = 0;
84+
}
7285
}
7386

7487
/*

test/test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ describe("DynamoDBStore", () => {
6969
})
7070
.finally(done);
7171
});
72+
73+
it("should store a valid expiresIn", () => {
74+
const store = new DynamoDBStore({
75+
table: "sessions-test",
76+
expiresIn: 3600
77+
});
78+
store.expiresIn.should.equal(3600);
79+
});
80+
81+
it("should revert expiresIn to 0 when set to a non-integer", () => {
82+
const consoleWarnStub = sinon.stub(console, 'warn');
83+
const store = new DynamoDBStore({
84+
table: "sessions-test",
85+
expiresIn: 1.5
86+
});
87+
store.expiresIn.should.equal(0);
88+
consoleWarnStub.restore();
89+
});
90+
91+
it("should revert expiresIn to 0 when set to a negative integer", () => {
92+
const consoleWarnStub = sinon.stub(console, 'warn');
93+
const store = new DynamoDBStore({
94+
table: "sessions-test",
95+
expiresIn: -10
96+
});
97+
store.expiresIn.should.equal(0);
98+
consoleWarnStub.restore();
99+
});
72100
});
73101

74102
describe("Initializing", () => {

0 commit comments

Comments
 (0)