From 690368d36453a4f2f4d27211150cfc8a61651d9a Mon Sep 17 00:00:00 2001 From: RAFAEL TEIXEIRA DAVOLI Date: Thu, 22 Oct 2020 18:15:32 -0300 Subject: [PATCH 1/5] cors header --- src/routes/translate/get.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/routes/translate/get.js b/src/routes/translate/get.js index 4e7a3e6..67b49d9 100644 --- a/src/routes/translate/get.js +++ b/src/routes/translate/get.js @@ -47,6 +47,7 @@ module.exports = (req, res, next) => { translations.retrieve(value.params.language, sanitize(value.query.text)) .then(response => { res.locals = response + res.set('Access-Control-Allow-Origin', '*') next(null, req, res) }) .catch(error => next(error)) From 7374dea452fd8ccc88d5e5330754d566fdd504dd Mon Sep 17 00:00:00 2001 From: RAFAEL TEIXEIRA DAVOLI Date: Tue, 27 Oct 2020 14:45:01 -0300 Subject: [PATCH 2/5] no translation found handling --- src/controllers/translations/retrieve.js | 12 ++++++------ src/routes/translate/get.js | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/controllers/translations/retrieve.js b/src/controllers/translations/retrieve.js index e58046b..5d910f5 100644 --- a/src/controllers/translations/retrieve.js +++ b/src/controllers/translations/retrieve.js @@ -1,17 +1,17 @@ const text = require('src/controllers/text') const mongo = require('src/databases/mongo') -const NotFoundError = require('src/errors/not-found') module.exports = async (language, signed) => { + const translationData = { signed, spoken: null } const signedProcessedText = await text.process(signed) const translation = await retrieveData(language, 'translations', { signed: signedProcessedText.hash }) - if (!translation) throw new NotFoundError() - - const spoken = await retrieveData(language, 'spoken', { hash: translation.spoken }) - - return { signed, spoken: spoken.text } + if (translation) { + const spoken = await retrieveData(language, 'spoken', { hash: translation.spoken }) + translationData.spoken = spoken ? spoken.text : null + } + return translationData } function retrieveData (language, collection, query) { diff --git a/src/routes/translate/get.js b/src/routes/translate/get.js index 67b49d9..9532132 100644 --- a/src/routes/translate/get.js +++ b/src/routes/translate/get.js @@ -51,7 +51,7 @@ module.exports = (req, res, next) => { next(null, req, res) }) .catch(error => next(error)) -} + } function sanitize (text) { return text.replace(/['"]+/g, '') From d3beb2a8510a423bd37397757f8596f6bfe5eaeb Mon Sep 17 00:00:00 2001 From: RAFAEL TEIXEIRA DAVOLI Date: Thu, 29 Oct 2020 09:48:58 -0300 Subject: [PATCH 3/5] identation fix --- src/routes/translate/get.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/translate/get.js b/src/routes/translate/get.js index 9532132..67b49d9 100644 --- a/src/routes/translate/get.js +++ b/src/routes/translate/get.js @@ -51,7 +51,7 @@ module.exports = (req, res, next) => { next(null, req, res) }) .catch(error => next(error)) - } +} function sanitize (text) { return text.replace(/['"]+/g, '') From ef403b2b3dcd4dda96a56426802db0b64cc91463 Mon Sep 17 00:00:00 2001 From: RAFAEL TEIXEIRA DAVOLI Date: Thu, 26 Nov 2020 09:43:47 -0300 Subject: [PATCH 4/5] addressing comments --- README.md | 14 ++++++++++++++ src/controllers/translations/retrieve.js | 9 +++++---- src/routes/translate/get.js | 14 +++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f636b7c..09c6b2a 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,20 @@ NLU_URL= NLU_API_KEY= ``` +In the same `.env` file, add a comma separated list of origins that will be allowed to make cross site requests. + +If any origin can make the requests, just use the "*" value. + +``` +ALLOWED_ORIGINS= +``` + +For example: +``` +ALLOWED_ORIGINS=http://localhost:3001 +``` + + ### Customize Create an `.env` file to start the application in [cluster mode](https://nodejs.org/api/cluster.html#cluster_cluster), change its running port, log level, directory and maximum size. The variable names and default values are: diff --git a/src/controllers/translations/retrieve.js b/src/controllers/translations/retrieve.js index 5d910f5..7d8960f 100644 --- a/src/controllers/translations/retrieve.js +++ b/src/controllers/translations/retrieve.js @@ -4,13 +4,14 @@ const mongo = require('src/databases/mongo') module.exports = async (language, signed) => { const translationData = { signed, spoken: null } const signedProcessedText = await text.process(signed) - const translation = await retrieveData(language, 'translations', { signed: signedProcessedText.hash }) - if (translation) { - const spoken = await retrieveData(language, 'spoken', { hash: translation.spoken }) - translationData.spoken = spoken ? spoken.text : null + if (!translation) { + throw new NotFoundError() } + + const spoken = await retrieveData(language, 'spoken', { hash: translation.spoken }) + translationData.spoken = spoken ? spoken.text : null return translationData } diff --git a/src/routes/translate/get.js b/src/routes/translate/get.js index 67b49d9..d9ee148 100644 --- a/src/routes/translate/get.js +++ b/src/routes/translate/get.js @@ -29,6 +29,7 @@ const joi = require('joi') const translations = require('src/controllers/translations') const BadRequestError = require('src/errors/bad-request') +require('dotenv').config() const schema = joi.object({ params: { @@ -47,7 +48,18 @@ module.exports = (req, res, next) => { translations.retrieve(value.params.language, sanitize(value.query.text)) .then(response => { res.locals = response - res.set('Access-Control-Allow-Origin', '*') + const allowedOriginsString = process.env.ALLOWED_ORIGINS; + if (allowedOriginsString) { + if (allowedOriginsString === "*") { + res.set('Access-Control-Allow-Origin', '*') + } else { + const allowedOriginsArray = allowedOriginsString.split(",") + const requestOrigin = req.headers.origin; + if (allowedOriginsArray.includes(requestOrigin)) { + res.set('Access-Control-Allow-Origin', requestOrigin) + } + } + } next(null, req, res) }) .catch(error => next(error)) From a2be912b65988881fa5f55d35cd2e45247193406 Mon Sep 17 00:00:00 2001 From: RAFAEL TEIXEIRA DAVOLI Date: Thu, 26 Nov 2020 09:46:34 -0300 Subject: [PATCH 5/5] test error fix --- src/controllers/translations/retrieve.js | 1 + src/routes/translate/get.js | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/controllers/translations/retrieve.js b/src/controllers/translations/retrieve.js index 7d8960f..fac5271 100644 --- a/src/controllers/translations/retrieve.js +++ b/src/controllers/translations/retrieve.js @@ -1,5 +1,6 @@ const text = require('src/controllers/text') const mongo = require('src/databases/mongo') +const NotFoundError = require('src/errors/not-found') module.exports = async (language, signed) => { const translationData = { signed, spoken: null } diff --git a/src/routes/translate/get.js b/src/routes/translate/get.js index d9ee148..b4afeae 100644 --- a/src/routes/translate/get.js +++ b/src/routes/translate/get.js @@ -48,13 +48,13 @@ module.exports = (req, res, next) => { translations.retrieve(value.params.language, sanitize(value.query.text)) .then(response => { res.locals = response - const allowedOriginsString = process.env.ALLOWED_ORIGINS; + const allowedOriginsString = process.env.ALLOWED_ORIGINS if (allowedOriginsString) { - if (allowedOriginsString === "*") { + if (allowedOriginsString === '*') { res.set('Access-Control-Allow-Origin', '*') } else { - const allowedOriginsArray = allowedOriginsString.split(",") - const requestOrigin = req.headers.origin; + const allowedOriginsArray = allowedOriginsString.split(',') + const requestOrigin = req.headers.origin if (allowedOriginsArray.includes(requestOrigin)) { res.set('Access-Control-Allow-Origin', requestOrigin) }