Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit f106bbc

Browse files
author
Tomasz Kostuch
authored
Merge pull request #476 from seSze/bugfix/elastic-search-query-missing-source
Allow filter elasticsearch request for security reasons
2 parents aead6d8 + 0e7a99f commit f106bbc

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

config/default.json

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
"review"
4444
],
4545
"apiVersion": "5.6",
46+
"useRequestFilter": false,
47+
"overwriteRequestSourceParams": false,
48+
"requestParamsBlacklist": [],
4649
"cacheRequest": false,
4750
"searchScoring": {
4851
"attributes": {
@@ -84,21 +87,21 @@
8487
"tax_class_id": "tci",
8588
"description": "desc",
8689
"minimal_regular_price": "mrp",
87-
"final_price": "fp",
88-
"price": "p",
90+
"final_price": "fp",
91+
"price": "p",
8992
"special_price": "sp",
9093
"original_final_price": "ofp",
9194
"original_price": "op",
9295
"original_special_price": "osp",
93-
"final_price_incl_tax": "fpit",
96+
"final_price_incl_tax": "fpit",
9497
"original_price_incl_tax": "opit",
9598
"price_incl_tax": "pit",
96-
"special_price_incl_tax": "spit",
99+
"special_price_incl_tax": "spit",
97100
"final_price_tax": "fpt",
98101
"price_tax": "pt",
99102
"special_price_tax": "spt",
100103
"original_price_tax": "opt",
101-
"image": "i",
104+
"image": "i",
102105
"small_image": "si",
103106
"thumbnail": "t"
104107
},
@@ -109,7 +112,7 @@
109112
"default": 10,
110113
"size": 10,
111114
"color": 10
112-
},
115+
},
113116
"priceFilterKey": "final_price",
114117
"priceFilters": {
115118
"ranges": [
@@ -118,7 +121,7 @@
118121
{ "from": 100, "to": 150 },
119122
{ "from": 150 }
120123
]
121-
}
124+
}
122125
},
123126
"varnish": {
124127
"host": "185.246.52.88",
@@ -237,7 +240,7 @@
237240
"useOnlyDefaultUserGroupId": false
238241
},
239242
"review": {
240-
"defaultReviewStatus": 2
243+
"defaultReviewStatus": 2
241244
},
242245
"bodyLimit": "100kb",
243246
"corsHeaders": [
@@ -350,15 +353,6 @@
350353
"includeFields": [ "attribute_code", "id", "entity_type_id", "options", "default_value", "is_user_defined", "frontend_label", "attribute_id", "default_frontend_label", "is_visible_on_front", "is_visible", "is_comparable" ],
351354
"loadByAttributeMetadata": false
352355
},
353-
"productList": {
354-
"sort": "",
355-
"includeFields": [ "type_id", "sku", "product_links", "tax_class_id", "special_price", "special_to_date", "special_from_date", "name", "price", "priceInclTax", "originalPriceInclTax", "originalPrice", "specialPriceInclTax", "id", "image", "sale", "new", "url_key" ],
356-
"excludeFields": [ "configurable_children", "description", "configurable_options", "sgn" ]
357-
},
358-
"productListWithChildren": {
359-
"includeFields": [ "type_id", "sku", "name", "tax_class_id", "special_price", "special_to_date", "special_from_date", "price", "priceInclTax", "originalPriceInclTax", "originalPrice", "specialPriceInclTax", "id", "image", "sale", "new", "configurable_children.image", "configurable_children.sku", "configurable_children.price", "configurable_children.special_price", "configurable_children.priceInclTax", "configurable_children.specialPriceInclTax", "configurable_children.originalPrice", "configurable_children.originalPriceInclTax", "configurable_children.color", "configurable_children.size", "product_links", "url_key"],
360-
"excludeFields": [ "description", "sgn"]
361-
},
362356
"product": {
363357
"excludeFields": [ "updated_at", "created_at", "attribute_set_id", "status", "visibility", "tier_prices", "options_container", "msrp_display_actual_price_type", "has_options", "stock.manage_stock", "stock.use_config_min_qty", "stock.use_config_notify_stock_qty", "stock.stock_id", "stock.use_config_backorders", "stock.use_config_enable_qty_inc", "stock.enable_qty_increments", "stock.use_config_manage_stock", "stock.use_config_min_sale_qty", "stock.notify_stock_qty", "stock.use_config_max_sale_qty", "stock.use_config_max_sale_qty", "stock.qty_increments", "small_image"],
364358
"includeFields": null,

src/lib/elastic.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const _ = require('lodash')
33
const fs = require('fs');
44
const jsonFile = require('jsonfile')
55
const es = require('@elastic/elasticsearch')
6+
const querystring = require('querystring')
67

78
function _updateQueryStringParameter (uri, key, value) {
89
var re = new RegExp('([?&])' + key + '=.*?(&|#|$)', 'i');
@@ -31,6 +32,39 @@ function adjustIndexName (indexName, entityType, config) {
3132
}
3233
}
3334

35+
function decorateBackendUrl (entityType, url, req, config) {
36+
if (config.elasticsearch.useRequestFilter && typeof config.entities[entityType] === 'object') {
37+
const urlParts = url.split('?')
38+
const { includeFields, excludeFields } = config.entities[entityType]
39+
40+
const filteredParams = Object.keys(req.query)
41+
.filter(key => !config.elasticsearch.requestParamsBlacklist.includes(key))
42+
.reduce((object, key) => {
43+
object[key] = req.query[key]
44+
return object
45+
}, {})
46+
47+
let _source_include = includeFields || []
48+
let _source_exclude = excludeFields || []
49+
50+
if (!config.elasticsearch.overwriteRequestSourceParams) {
51+
const requestSourceInclude = req.query._source_include || []
52+
const requestSourceExclude = req.query._source_exclude || []
53+
_source_include = [...includeFields, ...requestSourceInclude]
54+
_source_exclude = [...excludeFields, ...requestSourceExclude]
55+
}
56+
57+
const urlParams = {
58+
...filteredParams,
59+
_source_include,
60+
_source_exclude
61+
}
62+
url = `${urlParts[0]}?${querystring.stringify(urlParams)}`
63+
}
64+
65+
return url
66+
}
67+
3468
function adjustBackendProxyUrl (req, indexName, entityType, config) {
3569
let url
3670
const queryString = require('query-string');
@@ -58,7 +92,8 @@ function adjustBackendProxyUrl (req, indexName, entityType, config) {
5892
if (!url.startsWith('http')) {
5993
url = config.elasticsearch.protocol + '://' + url
6094
}
61-
return url
95+
96+
return decorateBackendUrl(entityType, url, req, config)
6297
}
6398

6499
function adjustQuery (esQuery, entityType, config) {

0 commit comments

Comments
 (0)