-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.ts
More file actions
121 lines (104 loc) · 3.25 KB
/
Copy pathindex.ts
File metadata and controls
121 lines (104 loc) · 3.25 KB
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
import { defineCheck, done, Severity } from "engine";
import { Tags } from "../../types";
import { keyStrategy } from "../../utils/key";
type FindingDetails = {
type: "__schema" | "__type";
};
const detectFromJson = (body: string): FindingDetails | undefined => {
try {
const parsed = JSON.parse(body) as Record<string, unknown>;
if (parsed === null || typeof parsed !== "object") {
return undefined;
}
const data = parsed.data;
if (data === null || data === undefined) {
return undefined;
}
if (typeof data === "object") {
if ("__schema" in (data as Record<string, unknown>)) {
return { type: "__schema" };
}
if ("__type" in (data as Record<string, unknown>)) {
return { type: "__type" };
}
}
} catch {
// Ignore JSON parse errors
}
return undefined;
};
const FALLBACK_REGEX = /"__schema"\s*:/;
const buildDescription = (details: FindingDetails): string => {
const subject = details.type === "__schema" ? "`__schema`" : "`__type`";
return [
"The GraphQL endpoint responded to an introspection query.",
"",
`The response includes the ${subject} field, indicating that schema introspection is enabled.`,
"",
"Exposed schema metadata can significantly aid attackers in enumerating operations and crafting targeted attacks. Disable introspection on production environments or protect the endpoint behind authentication.",
].join("\n");
};
export default defineCheck<Record<never, never>>(({ step }) => {
step("detectIntrospection", (state, context) => {
const { response } = context.target;
if (response === undefined) {
return done({ state });
}
const bodyText = response.getBody()?.toText();
if (bodyText === undefined || bodyText.length === 0) {
return done({ state });
}
const details = detectFromJson(bodyText);
if (details !== undefined) {
return done({
state,
findings: [
{
name: "GraphQL introspection enabled",
description: buildDescription(details),
severity: Severity.MEDIUM,
correlation: {
requestID: context.target.request.getId(),
locations: [],
},
},
],
});
}
if (FALLBACK_REGEX.test(bodyText)) {
return done({
state,
findings: [
{
name: "GraphQL introspection enabled",
description: buildDescription({ type: "__schema" }),
severity: Severity.MEDIUM,
correlation: {
requestID: context.target.request.getId(),
locations: [],
},
},
],
});
}
return done({ state });
});
return {
metadata: {
id: "graphql-introspection-enabled",
name: "GraphQL introspection enabled",
description:
"Detects GraphQL responses that disclose schema metadata via introspection.",
type: "passive",
tags: [Tags.INFORMATION_DISCLOSURE],
severities: [Severity.MEDIUM],
aggressivity: {
minRequests: 0,
maxRequests: 0,
},
},
initState: () => ({}),
dedupeKey: keyStrategy().withHost().withPath().build(),
when: (target) => target.response !== undefined,
};
});