Skip to content

Fix for issue #3740 #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/routes/projects/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ const setFilter = (value, keyword, fieldName) => {
return buildEsQueryWithFilter(value, keyword, MATCH_TYPE_EXACT_PHRASE, fieldName);
};

/**
* ES need to skip special chars else it is considered as RegEx
*
* @param {String} query query being searched for
* @return {String} result after parsing
*/
function escapeElasticsearchQuery(query) {
const chars = ['\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']',
'^', '"', '~', '*', '?', ':', '/', '<', '>'];
let result = query;
_.forEach(chars, (item) => {
result = result.replace(item, `\\${item}`);
});
return result;
}

/**
* Parse the ES search criteria and prepare search request body
*
Expand Down Expand Up @@ -426,7 +442,7 @@ const parseElasticSearchCriteria = (criteria, fields, order) => {

if (!keyword) {
// Not a specific field search nor an exact phrase search, do a wildcard match
keyword = criteria.filters.keyword;
keyword = escapeElasticsearchQuery(keywordCriterion);
matchType = MATCH_TYPE_WILDCARD;
}

Expand Down
44 changes: 42 additions & 2 deletions src/routes/projects/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const data = [
type: 'generic',
billingAccountId: 1,
name: 'test1',
description: 'test project1',
description: 'test project1 abc/d',
status: 'active',
details: {
utm: {
Expand Down Expand Up @@ -1065,7 +1065,7 @@ describe('LIST Project', () => {
resJson.should.have.lengthOf(1);
resJson[0].should.have.property('description');
resJson[0].should.not.have.property('cancelReason');
resJson[0].description.should.be.eq('test project1');
resJson[0].description.should.be.eq('test project1 abc/d');
done();
}
});
Expand Down Expand Up @@ -1244,6 +1244,46 @@ describe('LIST Project', () => {
}
});
});

it('should find a project by quoted keyword with a special symbol in the name', (done) => {
request(server)
.get('/v5/projects/?keyword="abc/d"')
.set({
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) => {
if (err) {
done(err);
} else {
const resJson = res.body;
should.exist(resJson);
resJson.should.have.lengthOf(1);
done();
}
});
});

it('should find a project by keyword with a special symbol in the name', (done) => {
request(server)
.get('/v5/projects/?keyword=abc/d')
.set({
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) => {
if (err) {
done(err);
} else {
const resJson = res.body;
should.exist(resJson);
resJson.should.have.lengthOf(1);
done();
}
});
});
});
});
});