[MediaWiki-commits] [Gerrit] mediawiki...recommendation-api[master]: Add source parameter validation

2017-08-02 Thread Mobrovac (Code Review)
Mobrovac has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/366222 )

Change subject: Add source parameter validation
..


Add source parameter validation

Bug: T169656
Change-Id: I07f7de1edc711a8aebb5bbaf1c732691b67b2662
---
M routes/translation.js
M spec.yaml
2 files changed, 22 insertions(+), 12 deletions(-)

Approvals:
  Mobrovac: Verified; Looks good to me, approved



diff --git a/routes/translation.js b/routes/translation.js
index 191bed9..713505e 100644
--- a/routes/translation.js
+++ b/routes/translation.js
@@ -15,8 +15,22 @@
  */
 let app;
 
+/**
+ * Regular expression used for validating the source parameter
+ * @type {RegExp}
+ */
+const sourceValidator = /^[a-zA-Z]+(-[a-zA-Z]+)*$/;
 
 function recommend(req, res, source, target, seed) {
+if (!sourceValidator.test(source)) {
+throw new sUtil.HTTPError({
+status: 400,
+type: 'bad_request',
+title: 'Bad request',
+detail: 'source parameter was invalid'
+});
+}
+
 let count = 24;
 if (req.query && req.query.count) {
 count = parseInt(req.query.count, 10);
@@ -40,22 +54,11 @@
 });
 }
 
-
-/**
- * GET /articles/{source}
- * Gets the articles existing in source but missing in domain.
- */
-router.get('/articles/:source', (req, res) => {
-const target = req.params.domain.split('.')[0];
-return recommend(req, res, req.params.source, target);
-});
-
-
 /**
  * GET /articles/{source}/{seed}
  * Gets the articles existing in source but missing in domain based on seed.
  */
-router.get('/articles/:source/:seed', (req, res) => {
+router.get('/articles/:source/:seed?', (req, res) => {
 const target = req.params.domain.split('.')[0];
 return recommend(req, res, req.params.source, target, req.params.seed);
 });
diff --git a/spec.yaml b/spec.yaml
index dfa7c22..aa4b31c 100644
--- a/spec.yaml
+++ b/spec.yaml
@@ -136,3 +136,10 @@
   count: -123
   response:
 status: 400
+- title: incorrectly formatted source
+  request:
+params:
+  source: en-
+  domain: de.wikipedia.org
+  response:
+status: 400

-- 
To view, visit https://gerrit.wikimedia.org/r/366222
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I07f7de1edc711a8aebb5bbaf1c732691b67b2662
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/services/recommendation-api
Gerrit-Branch: master
Gerrit-Owner: Nschaaf 
Gerrit-Reviewer: Brian Wolff 
Gerrit-Reviewer: Mobrovac 
Gerrit-Reviewer: Nschaaf 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits


[MediaWiki-commits] [Gerrit] mediawiki...recommendation-api[master]: Add source parameter validation

2017-07-19 Thread Nschaaf (Code Review)
Nschaaf has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/366222 )

Change subject: Add source parameter validation
..

Add source parameter validation

Bug: T169656
Change-Id: I07f7de1edc711a8aebb5bbaf1c732691b67b2662
---
M routes/translation.js
1 file changed, 2 insertions(+), 2 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/services/recommendation-api 
refs/changes/22/366222/1

diff --git a/routes/translation.js b/routes/translation.js
index 191bed9..12ba009 100644
--- a/routes/translation.js
+++ b/routes/translation.js
@@ -45,7 +45,7 @@
  * GET /articles/{source}
  * Gets the articles existing in source but missing in domain.
  */
-router.get('/articles/:source', (req, res) => {
+router.get('/articles/:source([a-zA-Z]+)', (req, res) => {
 const target = req.params.domain.split('.')[0];
 return recommend(req, res, req.params.source, target);
 });
@@ -55,7 +55,7 @@
  * GET /articles/{source}/{seed}
  * Gets the articles existing in source but missing in domain based on seed.
  */
-router.get('/articles/:source/:seed', (req, res) => {
+router.get('/articles/:source([a-zA-Z]+)/:seed', (req, res) => {
 const target = req.params.domain.split('.')[0];
 return recommend(req, res, req.params.source, target, req.params.seed);
 });

-- 
To view, visit https://gerrit.wikimedia.org/r/366222
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I07f7de1edc711a8aebb5bbaf1c732691b67b2662
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/recommendation-api
Gerrit-Branch: master
Gerrit-Owner: Nschaaf 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits