diff --git a/CHANGELOG.md b/CHANGELOG.md index 94a43ed..936c688 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.2...main) +## [Unreleased](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.3...main) +## [v1.1.3](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.2...v1.1.3) + +### Fixed +- Fixed ascii characters in tokens +- Fixed Postgres connection timeout + ## [v1.1.2](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.1...v1.1.2) ### Added diff --git a/src/database/create-postgres-pool.ts b/src/database/create-postgres-pool.ts index 14fb702..1da2484 100644 --- a/src/database/create-postgres-pool.ts +++ b/src/database/create-postgres-pool.ts @@ -5,6 +5,9 @@ export function createPostgresPool(config: DBConfig) { return createPool( typeof config === 'string' ? config - : `postgres://${config.username}:${config.password}@${config.host}:${config.port}/${config.database}` + : `postgres://${config.username}:${config.password}@${config.host}:${config.port}/${config.database}`, + { + connectionTimeout: 'DISABLE_TIMEOUT', + } ); } diff --git a/src/utility/parse-token.ts b/src/utility/parse-token.ts index 36054e2..fd07c0b 100644 --- a/src/utility/parse-token.ts +++ b/src/utility/parse-token.ts @@ -1,4 +1,5 @@ import { ApplicationState, JWTConfig, Scopes } from '../types'; +import { safeJsonParse } from './safe-json-parse'; export function parseToken( rawToken: string, @@ -13,7 +14,11 @@ export function parseToken( try { const payload = Buffer.from(base64Payload, 'base64'); - const token = JSON.parse(payload.toString('ascii')); + const tokenResp = safeJsonParse(payload.toString('utf-8')); + if (tokenResp.error) { + throw new Error(`Invalid token JSON encoding`); + } + const token = tokenResp.result; if (!token || !token.sub || !token.scope || !token.iss) { return; diff --git a/src/utility/safe-json-parse.ts b/src/utility/safe-json-parse.ts new file mode 100644 index 0000000..5fd4ca9 --- /dev/null +++ b/src/utility/safe-json-parse.ts @@ -0,0 +1,9 @@ +export function safeJsonParse(json: string): { result: T, error: false } | { error: true } { + try { + return { result: JSON.parse(json), error: false }; + } catch (e) { + console.log('Error parsing JSON', e); + console.log(json); + return { error: true }; + } +} diff --git a/tsconfig.json b/tsconfig.json index 9efd8e7..af14a39 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,7 @@ "resolveJsonModule": true, "noImplicitAny": true, "downlevelIteration": true, - "noUnusedLocals": true, + "noUnusedLocals": false, "noFallthroughCasesInSwitch": true, "paths": { // "PACKAGE_NAME": ["../SERVICE_NAME"]