forked from hoppscotch/hoppscotch
/
header.inspector.ts
80 lines (66 loc) 路 2.34 KB
/
header.inspector.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
import { Service } from "dioc"
import { InspectionService, Inspector, InspectorResult } from ".."
import { getI18n } from "~/modules/i18n"
import { HoppRESTRequest } from "@hoppscotch/data"
import { Ref, computed, markRaw } from "vue"
import IconAlertTriangle from "~icons/lucide/alert-triangle"
import { InterceptorService } from "~/services/interceptor.service"
/**
* This inspector is responsible for inspecting the header of a request.
* It checks if the header contains cookies.
*
* NOTE: Initializing this service registers it as a inspector with the Inspection Service.
*/
export class HeaderInspectorService extends Service implements Inspector {
public static readonly ID = "HEADER_INSPECTOR_SERVICE"
private t = getI18n()
public readonly inspectorID = "header"
private readonly inspection = this.bind(InspectionService)
private readonly interceptorService = this.bind(InterceptorService)
constructor() {
super()
this.inspection.registerInspector(this)
}
private cookiesCheck(headerKey: string) {
const cookieKeywords = ["Cookie", "Set-Cookie", "Cookie2", "Set-Cookie2"]
return cookieKeywords.includes(headerKey)
}
getInspections(req: Readonly<Ref<HoppRESTRequest>>) {
return computed(() => {
const results: InspectorResult[] = []
const headers = req.value.headers
const headerKeys = Object.values(headers).map((header) => header.key)
const isContainCookies = headerKeys.includes("Cookie")
if (
isContainCookies &&
!this.interceptorService.currentInterceptor.value?.supportsCookies
) {
headerKeys.forEach((headerKey, index) => {
if (this.cookiesCheck(headerKey)) {
results.push({
id: "header",
icon: markRaw(IconAlertTriangle),
text: {
type: "text",
text: this.t("inspections.header.cookie"),
},
severity: 2,
isApplicable: true,
locations: {
type: "header",
position: "key",
key: headerKey,
index: index,
},
doc: {
text: this.t("action.download_here"),
link: "https://hoppscotch.com/download",
},
})
}
})
}
return results
})
}
}