-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.ts
220 lines (198 loc) · 5.89 KB
/
index.ts
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
import Component from '@glimmer/component';
import { service } from '@ember/service';
import { action } from '@ember/object';
import type IntlService from 'ember-intl/services/intl';
import type AnalysisModel from 'irene/models/analysis';
import {
indentHTML,
type VulnerableApiFinding,
} from 'irene/utils/parse-vulnerable-api-finding';
export interface FileDetailsVulnerabilityAnalysisDetailsFindingsVulnerableApiSignature {
Element: HTMLDivElement;
Args: {
analysis: AnalysisModel;
currentVulnerability?: VulnerableApiFinding;
};
}
interface FormattedResult {
value: string;
isJSON?: boolean;
isHTML?: boolean;
}
interface VulnerabilityDetails {
title: string;
value: string;
isEmpty: boolean;
copyIcon: boolean;
isKeyValuePair?: boolean;
whiteSpace?: 'pre-wrap' | 'pre-line';
}
export default class FileDetailsVulnerabilityAnalysisDetailsFindingsVulnerableApiComponent extends Component<FileDetailsVulnerabilityAnalysisDetailsFindingsVulnerableApiSignature> {
@service declare intl: IntlService;
get vulnerabilityResponse() {
return this.args.currentVulnerability?.response;
}
get vulnerabilityRequest() {
return this.args.currentVulnerability?.request;
}
get currentVulnerabilityDetails() {
const currentVulnerability = this.args.currentVulnerability || {};
if (Object.keys(currentVulnerability).length !== 0) {
return [
{
label: this.intl.t('url'),
value: this.url,
},
{
label: this.intl.t('method'),
value: this.vulnerabilityRequest?.method,
},
{
label: this.intl.t('severity'),
value: this.args.currentVulnerability?.severity,
},
{
label: this.intl.t('confidence'),
value: this.args.currentVulnerability?.confidence,
},
{
label: this.intl.t('issueDetails'),
value: this.args.currentVulnerability?.description,
},
];
} else {
return [];
}
}
get url() {
if (this.vulnerabilityResponse?.url) {
return this.vulnerabilityResponse?.url;
} else if (this.vulnerabilityRequest?.url) {
return this.vulnerabilityRequest?.url;
} else {
return this.args.currentVulnerability?.url;
}
}
@action getFormattedText(inputString: string | undefined): FormattedResult {
if (!inputString) {
return {
value: '',
};
}
const sanitizedString = inputString
.trim()
.replace(/(^['"])|(['"]$)/g, '')
.replace(/\\n/g, '\n');
// Check if it's HTML content
if (
sanitizedString.includes('<!DOCTYPE html') ||
sanitizedString.includes('<html') ||
sanitizedString.toLowerCase().includes('<!doctype html')
) {
return {
value: indentHTML(sanitizedString),
isJSON: false,
isHTML: true,
};
}
// Try to parse as JSON first
try {
const parsed = JSON.parse(sanitizedString);
return {
value: JSON.stringify(parsed, null, 2),
isJSON: true,
isHTML: false,
};
} catch {
// If JSON parsing fails, return the sanitized string
return {
value: sanitizedString,
isJSON: false,
isHTML: false,
};
}
}
getWhiteSpaceStyle(formattedBody: {
isJSON?: boolean;
isHTML?: boolean;
}): string {
return formattedBody.isJSON || formattedBody.isHTML
? 'pre-wrap'
: 'pre-line';
}
get vulnerabilityDetails() {
const request = this.args.currentVulnerability?.request;
const response = this.args.currentVulnerability?.response;
const formattedRequestBody = this.getFormattedText(request?.body);
const formattedResponseBody = this.getFormattedText(response?.text);
const reqBodyWhitespaceStyle =
this.getWhiteSpaceStyle(formattedRequestBody);
const resBodyWhitespaceStyle = this.getWhiteSpaceStyle(
formattedResponseBody
);
return [
{
title: this.intl.t('requestBody'),
value: formattedRequestBody.value,
isEmpty: !request?.body,
copyIcon: true,
whiteSpace: reqBodyWhitespaceStyle,
},
{
title: this.intl.t('requestHeaders'),
value: request?.headers,
isEmpty: !request?.headers,
copyIcon: true,
isKeyValuePair: true,
},
{
title: this.intl.t('requestParameters'),
value: request?.params,
isEmpty: !request?.params || Object.keys(request.params).length === 0,
copyIcon: true,
isKeyValuePair: true,
},
{
title: this.intl.t('requestCookies'),
value: request?.cookies,
isEmpty: !request?.cookies || Object.keys(request.cookies).length === 0,
copyIcon: true,
isKeyValuePair: true,
},
{
title: this.intl.t('response'),
value: response?.status_code,
isEmpty: !response?.status_code,
copyIcon: false,
},
{
title: this.intl.t('responseHeaders'),
value: response?.headers,
isEmpty:
!response?.headers || Object.keys(response.headers).length === 0,
copyIcon: true,
isKeyValuePair: true,
},
{
title: this.intl.t('responseCookies'),
value: response?.cookies,
isEmpty:
!response?.cookies || Object.keys(response.cookies).length === 0,
copyIcon: true,
isKeyValuePair: true,
},
{
title: this.intl.t('responseBody'),
value: formattedResponseBody.value,
isEmpty: !response?.text || response?.text === '""',
whiteSpace: resBodyWhitespaceStyle,
copyIcon: true,
},
] as VulnerabilityDetails[];
}
}
declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
'FileDetails::VulnerabilityAnalysisDetails::Findings::VulnerableApi': typeof FileDetailsVulnerabilityAnalysisDetailsFindingsVulnerableApiComponent;
}
}