diff --git a/package.json b/package.json index a5a47b6c9..22b8be224 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,6 @@ "bcrypt": "5.0.1", "bittorrent-tracker": "^9.0.0", "bluebird": "^3.5.0", - "body-parser": "^1.12.4", "bull": "^3.4.2", "bytes": "^3.0.0", "chokidar": "^3.4.2", diff --git a/server.ts b/server.ts index 1834256d5..66c9173ca 100644 --- a/server.ts +++ b/server.ts @@ -7,7 +7,6 @@ if (isTestInstance()) { } // ----------- Node modules ----------- -import * as bodyParser from 'body-parser' import * as express from 'express' import * as morgan from 'morgan' import * as cors from 'cors' @@ -170,14 +169,22 @@ app.use(morgan('combined', { skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' })) +// Response helpers used for errors +app.use(apiResponseHelpers) + // For body requests -app.use(bodyParser.urlencoded({ extended: false })) -app.use(bodyParser.json({ +app.use(express.urlencoded({ extended: false })) +app.use(express.json({ type: [ 'application/json', 'application/*+json' ], limit: '500kb', - verify: (req: express.Request, _, buf: Buffer) => { + verify: (req: express.Request, res: express.Response, buf: Buffer) => { const valid = isHTTPSignatureDigestValid(buf, req) - if (valid !== true) throw new Error('Invalid digest') + if (valid !== true) { + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Invalid digest' + }) + } } })) @@ -187,9 +194,6 @@ app.use(cookieParser()) // W3C DNT Tracking Status app.use(advertiseDoNotTrack) -// Response helpers used in developement -app.use(apiResponseHelpers) - // ----------- Views, routes and static files ----------- // API @@ -222,23 +226,22 @@ if (cliOptions.client) app.use('/', clientsRouter) // ----------- Errors ----------- -// Catch 404 and forward to error handler -app.use(function (req, res, next) { - const err = new Error('Not Found') - err['status'] = HttpStatusCode.NOT_FOUND_404 - next(err) +// Catch unmatched routes +app.use((req, res: express.Response) => { + res.status(HttpStatusCode.NOT_FOUND_404).end() }) -app.use(function (err, req, res, next) { +// Catch thrown errors +app.use((err, req, res: express.Response, next) => { + // Format error to be logged let error = 'Unknown error.' if (err) { error = err.stack || err.message || err } - - // Sequelize error + // Handling Sequelize error traces const sql = err.parent ? err.parent.sql : undefined - logger.error('Error in controller.', { err: error, sql }) + return res.fail({ status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500, message: err.message, diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index e3ff93cdd..bca59a83c 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts @@ -131,8 +131,8 @@ const apiResponseHelpers = (req, res: express.Response, next = null) => { res.fail = (options) => { const { data, status, message, title, type, docs, instance } = { data: null, - status: HttpStatusCode.BAD_REQUEST_400, - ...options + ...options, + status: options.status || HttpStatusCode.BAD_REQUEST_400 } const extension = new ProblemDocumentExtension({ diff --git a/support/doc/api/openapi.yaml b/support/doc/api/openapi.yaml index 52a834056..9f40d74c6 100644 --- a/support/doc/api/openapi.yaml +++ b/support/doc/api/openapi.yaml @@ -46,6 +46,7 @@ info: { "detail": "Video not found", + "docs": "https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo", "status": 404, "title": "Not Found", "type": "about:blank" @@ -67,6 +68,7 @@ info: { "detail": "Incorrect request parameters: id", + "docs": "https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo", "instance": "/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180", "invalid-params": { "id": { diff --git a/yarn.lock b/yarn.lock index 4731b61f4..41cba2477 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1658,7 +1658,7 @@ bn.js@^5.1.1: resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002" integrity sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw== -body-parser@1.19.0, body-parser@^1.12.4: +body-parser@1.19.0: version "1.19.0" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a" integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==