-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcveRecordSearchModule.vue
265 lines (224 loc) · 8.48 KB
/
cveRecordSearchModule.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<template>
<div class="mt-1">
<div v-if="websiteEnv !== 'prd'" role="alert" class="notification is-warning is-light">
<div class="is-flex is-align-items-flex-start mt-1" style="max-width: 820px; column-gap: 1rem;">
<div class="pl-3">
<p id="alertIconVariousFormts" class="is-hidden">alert</p>
<font-awesome-icon size="1x" icon="exclamation-triangle" role="alert"
aria-labelledby="alertIconVariousFormts" aria-hidden="false" />
</div>
<div class="is-flex-grow-5">
<p class="is-size-7">
<span class="has-text-weight-bold">CNAs</span> – to view your <span class="has-text-weight-bold">test data</span>
(your draft records) select “<span class="has-text-weight-bold">Find a Test CVE Record/ID (Legacy)</span>” in the drop-down menu and
provide a CVE ID to find a specific CVE Record.
</p>
</div>
</div>
</div>
<div class="field has-addons mt-1">
<p class="control">
<span v-if="websiteEnv !== 'prd'" class="select cve-search-selector">
<select v-model="searchType">
<option>Search CVE List</option>
<option>Find a Test CVE Record/ID (Legacy)</option>
</select>
</span>
</p>
<p class="control is-expanded">
<input v-if="searchTypeBoolean" v-model.trim="queryString" @keyup="onKeyUp"
@keyup.enter="validate" type="text" class="input cve-id-input is-expanded"
placeholder="Enter keywords (e.g.: CVE ID, sql injection, etc.)" />
<input v-else v-model.trim="cveId" @keyup="onKeyUp" @keyup.enter="validate"
type="text" class="input cve-id-input" placeholder="Enter CVE ID (CVE-YYYY-NNNN)" />
</p>
<p class="control">
<button @click="validate" class="button cve-button cve-button-accent-warm"
:class="{ 'is-loading': cveListSearchStore.isSearching, 'disabled': cveListSearchStore.isSearchButtonDisabled }"
:aria-disabled="cveListSearchStore.isSearchButtonDisabled"
:disabled="cveListSearchStore.isSearchButtonDisabled">
{{ searchTypeBoolean ? 'Search' : 'Find'}}
</button>
</p>
</div>
<div class="notification is-warning is-light" role="alert" v-if="errorMessage.length > 0">
<div class="is-flex is-align-content-flex-start">
<p id="alertIcon" class="is-hidden">alert</p>
<font-awesome-icon style="flex: 0 0 40px; margin-top:3px" size="lg" icon="exclamation-triangle" role="alert"
aria-labelledby="alertIcon" aria-hidden="false" />
<p class="cve-help-text">
{{ errorMessage }}
</p>
</div>
</div>
</div>
</template>
<script setup>
import { useCveListSearchStore } from '@/stores/cveListSearch';
import { computed, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
// Legacy Search Import
import { usecveRecordStore } from '@/stores/cveRecord';
import { useGenericGlobalsStore } from '@/stores/genericGlobals';
const cveIdRegex = /^CVE\p{Pd}(?<year>\d{4})\p{Pd}(?<id>\d{4,})$/iu;
const wordRegex = /^[a-z0-9 ]+$/i;
const cveStartYear = 1999;
let cveListSearchStore = useCveListSearchStore();
const route = useRoute();
const router = useRouter();
let prevSearchValue = ref('');
let queryString = ref('');
let errorMessage = ref('');
let cveGenericGlobalsStore = useGenericGlobalsStore();
let cveRecordStore = usecveRecordStore();
let searchType = ref('Search CVE List');
let cveId = cveRecordStore.cveId;
// this seems redundant, but it fixes an edge case.
// if a user searches for a particular field, then on the results page flips the toggle, THEN refreshes without searching, this will keep the correct helper text showing.
let searchTypeBoolean = computed(() => {
return searchType.value == 'Search CVE List' ? true : false;
});
watch(searchType, () => {
resetStates();
});
watch(
() => route.query,
() => {
if (searchTypeBoolean.value && route.query?.query) {
queryString.value = route.query.query.trim();
validate();
}
}
)
function resetStates() {
cveListSearchStore.searchType = cveGenericGlobalsStore.useSearch = searchTypeBoolean.value;
cveId = '';
queryString.value = cveListSearchStore.query = '';
showHelpMessage('');
cveListSearchStore.isSearchButtonDisabled = true;
}
function showHelpMessage(helpText) {
if (helpText.length) {
errorMessage.value = helpText;
cveListSearchStore.showHelpText = true;
} else {
errorMessage.value = '';
cveListSearchStore.showHelpText = false;
}
}
function startSearch() {
// We only want to flip the search item _When we actually do a search_ otherwise we should default back to what we were on a page refresh
if (searchTypeBoolean.value) {
cveGenericGlobalsStore.setUseSearch(true);
cveGenericGlobalsStore.setCurrentServicesUrl(`https://${import.meta.env.VITE_CVE_SERVICES_BASE_URL}`)
}
else {
cveGenericGlobalsStore.setUseSearch(false);
cveGenericGlobalsStore.setCurrentServicesUrl(cveGenericGlobalsStore.cveServiceTestBaseUrl)
}
if (cveGenericGlobalsStore.useSearch) {
cveListSearchStore.$reset();
cveListSearchStore.query = queryString.value;
if (route.name != 'SearchResults' || !route.query?.query
|| (route.query.query != cveListSearchStore.query)) {
router.push({name: 'SearchResults',
query: {query: cveListSearchStore.query}});
} else
cveListSearchStore.search();
} else {
const lookupPath = `/CVERecord?id=${cveId}`;
router.push({path: lookupPath, query: {id: cveId}})
}
}
function validateQueryString() {
const contentMessage = 'Enter words of alphanumeric characters OR a single CVD ID (CVE-YYYY-NNNN).';
const isSearch = searchTypeBoolean.value;
const searchValue = isSearch ? queryString.value : cveId;
prevSearchValue.value = searchValue;
cveListSearchStore.isSearchButtonDisabled = true;
showHelpMessage('');
if (!searchValue)
return !cveListSearchStore.isSearchButtonDisabled;
const cveIdMatch = cveIdRegex.exec(searchValue)
if (cveIdMatch) {
const year = parseInt(cveIdMatch.groups.year);
const id = cveIdMatch.groups.id;
const currentYear = new Date().getFullYear();
if (cveStartYear <= year && year <= currentYear) {
const normalizedCveId = `CVE-${year}-${id}`;
if (isSearch)
queryString.value = normalizedCveId;
else
cveId = normalizedCveId;
} else {
showHelpMessage(`CVE ID year must be within ${cveStartYear}-${currentYear}`);
}
}
if (isSearch) {
// Search: if the query string isn't a CVE ID and it doesn't just consist
// of "typical" words (alphanumeric phrases), then it's an error.
if (!cveIdMatch && !wordRegex.test(searchValue)) {
showHelpMessage(contentMessage);
} else if (!errorMessage.value) {
// The provided search string is good.
cveListSearchStore.isSearchButtonDisabled = false;
}
} else if (cveIdMatch) {
// Legacy Find by CVE ID and it's in the correct format.
cveListSearchStore.isSearchButtonDisabled = false;
} else if (!errorMessage.value) {
// Legacy Find by CVE ID but the query string is not a CVE ID.
showHelpMessage('Required CVE ID format: CVE-YYYY-NNNN');
}
return !cveListSearchStore.isSearchButtonDisabled;
}
function onKeyUp() {
if (cveListSearchStore.isSearchButtonDisabled) {
const isSearch = searchTypeBoolean.value;
const searchValue = isSearch ? queryString.value : cveId;
if (prevSearchValue.value !== searchValue)
cveListSearchStore.isSearchButtonDisabled = false;
}
}
function validate() {
if (validateQueryString()) {
try {
cveListSearchStore.isSearchButtonDisabled = true;
startSearch();
} finally {
cveListSearchStore.isSearchButtonDisabled = false;
}
} else if (route.name != 'home' || route?.query) {
router.push({name: 'home', query: {}});
}
}
const websiteEnv = computed(() => {
return import.meta.env.VITE_WEBSITE_ENVIRONMENT;
});
</script>
<style scoped lang="scss">
.disabled {
opacity: 0.7 !important;
background-color: #3d4551 !important;
color: white !important;
border-color: #dbdbdb;
box-shadow: none;
}
.notification {
margin: 0 0 2px 0 !important;
padding: 2px 10px 2px 10px !important;
}
@media screen and (min-width: $desktop) {
.cve-id-input {
width: 470px;
}
}
.cve-btn-container {
background: unset !important;
color: $black !important;
}
.cve-btn-container:hover, .cve-inner-btn:hover {
cursor: pointer;
text-decoration: none !important;
}
</style>