Skip to content

Commit 11978a1

Browse files
committed
cveListSearch: make all CVE ID only searches consistent by quoting the ID
1 parent d9ae3d6 commit 11978a1

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

src/stores/cveListSearch.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { useErrorMessageStore } from './cveRecord';
33
import { useGenericGlobalsStore } from './genericGlobals';
44
import axios from 'axios';
55

6+
// Users may search for a CVE ID by either specifying it with or without quotes.
7+
// The regular expresssion object defined here will detect if a string is solely
8+
// a CVE ID (quotes optional). It's used to extract the CVE ID. This helps to
9+
// yield consistent results independent of whether the user has enclosed the CVE
10+
// ID in quotes.
11+
12+
const cveIdRegex = '(?<cveid>CVE-\\d{4}-\\d{4,7})';
13+
14+
const searchCveIdRegex = `^"?${cveIdRegex}"?\$`;
15+
16+
const searchCveIdRe = RegExp(searchCveIdRegex, 'i');
17+
618
export const useCveListSearchStore = defineStore('cveListSearch ', {
719
state: () => {
820
return {
@@ -66,30 +78,46 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
6678
}
6779
},
6880
actions: {
81+
cveIdSearchTerm() {
82+
83+
// This method returns true if the search query is a CVE ID, optionally
84+
// surrounded by quotes. If this is the case, the CVE ID is extracted,
85+
// normalized, and the query set to the quoted CVE ID. By enclosing
86+
// the CVE ID in quotes, the search won't break it up into its parts
87+
// which is certainly not what the user expects. This will make the
88+
// search quicker and provide better results for CVE ID only searches.
89+
90+
const cveIdMatch = searchCveIdRe.exec(this.query);
91+
if (cveIdMatch) {
92+
this.cveId = cveIdMatch.groups.cveid.toUpperCase();
93+
this.query = `"${this.cveId}"`;
94+
}
95+
96+
return cveIdMatch !== null;
97+
},
6998
decrement(field) {
7099
this[field] -= 1;
71100
},
72-
isCveIdPattern() {
73-
return new RegExp(/^CVE-\d{4}-\d{4,7}$/, 'i').test(this.query);
74-
},
75101
resetResults() {
76102
this.searchResults = [];
77103
this.totalSearchResultCount = 0;
78104
},
79105
async search() {
80106
this.isSearching = true;
81-
try{
107+
108+
const isCveIdSearch = this.cveIdSearchTerm();
109+
110+
try {
82111

83112
// * query search service
84113
this.totalExecutingRequests = 1;
114+
85115
await this.getSearchResults();
86116

87-
if (this.isCveIdPattern()) {
117+
if (isCveIdSearch) {
88118

89119
// The user's query is one CVE ID.
90120

91-
this.cveId = this.query.toUpperCase();
92-
93121
if (this.searchResults.length === 1
94122
&& this.searchResults[0].cveId === this.cveId) {
95123

@@ -116,7 +144,7 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
116144
}
117145
} catch (error) {
118146
// if record is not found, find potential reserved/rejected ID
119-
if (this.isCveIdPattern() && Object.keys(this.recordData).length === 0) {
147+
if (isCveIdSearch && Object.keys(this.recordData).length === 0) {
120148
await this.getIdData();
121149
} else
122150
throw new Error(`search() >> error with getSearchResults(), getRecordData(): ${error}`);

0 commit comments

Comments
 (0)