From de9304de9f44239c688f2daba2657604f24537e1 Mon Sep 17 00:00:00 2001 From: kleydon Date: Wed, 28 Jun 2023 11:28:48 -0700 Subject: [PATCH 1/3] fix: dont get expired session * Fix for issue #115 * Adjusted tslint configuration to un-break it --- package.json | 1 - src/lib/prisma-session-store.spec.ts | 19 ++++++++++++++++++- src/lib/prisma-session-store.ts | 15 ++++++++++++++- tslint.json | 6 ++++-- yarn.lock | 17 ----------------- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 00ade09..f1d298d 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,6 @@ "supertest": "^6.1.6", "ts-jest": "^27.0.7", "tslint": "^6.1.3", - "tslint-config-suiyobi": "^0.4.8", "typescript": "^4.4.4" }, "config": { diff --git a/src/lib/prisma-session-store.spec.ts b/src/lib/prisma-session-store.spec.ts index 4336431..5a35814 100755 --- a/src/lib/prisma-session-store.spec.ts +++ b/src/lib/prisma-session-store.spec.ts @@ -121,6 +121,23 @@ describe('PrismaSessionStore', () => { expect(callback).toHaveBeenCalledWith(); }); + it('should not get items for an expired session', async () => { + const [store, { findUniqueMock, deleteMock }] = freshStore(); + const callback = jest.fn(); + const s1 = { + sid: 'sid-0', + expiresAt: createExpiration(-1, { rounding: 100 }), + }; + findUniqueMock.mockResolvedValue(s1); + deleteMock.mockResolvedValue(undefined); + + await expect(store.get('sid-0', callback)).resolves.toStrictEqual( + undefined + ); + expect(deleteMock).toHaveBeenCalledWith({ where: { sid: s1.sid } }); + expect(callback).toHaveBeenCalledWith(); + }); + it('should resolve to a promise', async () => { const [store, { findUniqueMock }] = freshStore(); @@ -139,7 +156,7 @@ describe('PrismaSessionStore', () => { await store.get('sid-0', callback); expect(callback).toHaveBeenCalledWith( - new SyntaxError('Unexpected token i in JSON at position 2') + new SyntaxError(`Expected property name or '}' in JSON at position 2`) ); }); diff --git a/src/lib/prisma-session-store.ts b/src/lib/prisma-session-store.ts index 7b9fc05..9a180d0 100755 --- a/src/lib/prisma-session-store.ts +++ b/src/lib/prisma-session-store.ts @@ -280,7 +280,9 @@ export class PrismaSessionStore extends Store { callback?: (err?: unknown, val?: SessionData) => void ) => { if (!(await this.validateConnection())) return callback?.(); - const session = await this.prisma[this.sessionModelName] + const p = this.prisma[this.sessionModelName]; + + const session = await p .findUnique({ where: { sid }, }) @@ -289,6 +291,17 @@ export class PrismaSessionStore extends Store { if (session === null) return callback?.(); try { + // If session has has expired (allowing for missing 'expiresAt' and 'sid' fields) + if ( + session.sid && + session.expiresAt && + new Date().valueOf() >= session.expiresAt.valueOf() + ) { + this.logger.log(`Session with sid: ${sid} expired; deleting.`); + await p.delete({ where: { sid } }); + return callback?.(); + } + const result = this.serializer.parse(session.data ?? '{}') as SessionData; if (callback) defer(callback, undefined, result); diff --git a/tslint.json b/tslint.json index 4fc956f..2675e7b 100644 --- a/tslint.json +++ b/tslint.json @@ -1,7 +1,9 @@ { - "extends": "tslint-config-suiyobi", + "extends": "tslint:recommended", "rules": { "no-big-function": false, - "max-file-line-count": false + "max-file-line-count": false, + "ban-types": false, + "variable-name": false } } diff --git a/yarn.lock b/yarn.lock index e402aa3..101c719 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6573,23 +6573,6 @@ tslint-clean-code@latest: memoize-decorator "^1.0.2" tsutils "2.7.1" -tslint-config-suiyobi@^0.4.8: - version "0.4.8" - resolved "https://registry.yarnpkg.com/tslint-config-suiyobi/-/tslint-config-suiyobi-0.4.8.tgz#7c0cddc1658b329bc694d729538ec422ac405679" - integrity sha512-Jfr0K6vuIJBvk3wWwc9MT6OaQT+zgZ580SWwQ/4qK6Y8aC/x09B6SE4ECxLc+fL8UhXwBP4HLbou9SVz56IB+A== - dependencies: - "@angular-devkit/schematics" "^10.1.0" - doctrine "^3.0.0" - immutable "^4.0.0-rc.12" - rxjs-tslint-rules latest - tslint-clean-code latest - tslint-etc latest - tslint-plugin-decorator-member-ordering "^0.0.1" - tslint-plugin-prettier latest - tsutils "^3.17.1" - tsutils-etc "^1.3.1" - typescript-tslint-plugin latest - tslint-etc@latest: version "1.13.10" resolved "https://registry.yarnpkg.com/tslint-etc/-/tslint-etc-1.13.10.tgz#5700b0d83f0a243cb7d32b0b7ec2662cb5998bce" From 7ef28a8cfbbe042d860d7b8b6141fefb28aacf21 Mon Sep 17 00:00:00 2001 From: kleydon Date: Wed, 28 Jun 2023 12:02:23 -0700 Subject: [PATCH 2/3] fix: adjusted a test --- src/lib/prisma-session-store.spec.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/prisma-session-store.spec.ts b/src/lib/prisma-session-store.spec.ts index 5a35814..cc68ab5 100755 --- a/src/lib/prisma-session-store.spec.ts +++ b/src/lib/prisma-session-store.spec.ts @@ -156,7 +156,12 @@ describe('PrismaSessionStore', () => { await store.get('sid-0', callback); expect(callback).toHaveBeenCalledWith( - new SyntaxError(`Expected property name or '}' in JSON at position 2`) + new SyntaxError( + `Expected property name or '}' in JSON at position 2` + ) || + new SyntaxError( + `SyntaxError: Unexpected token i in JSON at position 2` + ) ); }); From 15e8381ef1823ca1e8628782d57127005b4fcf8f Mon Sep 17 00:00:00 2001 From: kleydon Date: Wed, 28 Jun 2023 12:16:01 -0700 Subject: [PATCH 3/3] fix: fixed test case --- src/lib/prisma-session-store.spec.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/lib/prisma-session-store.spec.ts b/src/lib/prisma-session-store.spec.ts index cc68ab5..b3f479b 100755 --- a/src/lib/prisma-session-store.spec.ts +++ b/src/lib/prisma-session-store.spec.ts @@ -156,12 +156,7 @@ describe('PrismaSessionStore', () => { await store.get('sid-0', callback); expect(callback).toHaveBeenCalledWith( - new SyntaxError( - `Expected property name or '}' in JSON at position 2` - ) || - new SyntaxError( - `SyntaxError: Unexpected token i in JSON at position 2` - ) + expect.objectContaining({ name: 'SyntaxError' }) ); });