forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-no-pairing-store-group-auth.mjs
More file actions
180 lines (162 loc) · 5.15 KB
/
check-no-pairing-store-group-auth.mjs
File metadata and controls
180 lines (162 loc) · 5.15 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
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
#!/usr/bin/env node
import ts from "typescript";
import { createPairingGuardContext } from "./lib/pairing-guard-context.mjs";
import {
collectFileViolations,
getPropertyNameText,
runAsScript,
toLine,
} from "./lib/ts-guard-utils.mjs";
const { repoRoot, sourceRoots, resolveFromRepo } = createPairingGuardContext(import.meta.url);
const allowedFiles = new Set([
resolveFromRepo("src/security/dm-policy-shared.ts"),
resolveFromRepo("src/channels/allow-from.ts"),
// Config migration/audit logic may intentionally reference store + group fields.
resolveFromRepo("src/security/fix.ts"),
resolveFromRepo("src/security/audit-channel.ts"),
]);
const storeIdentifierRe = /^(?:storeAllowFrom|storedAllowFrom|storeAllowList)$/i;
const groupNameRe =
/(?:groupAllowFrom|effectiveGroupAllowFrom|groupAllowed|groupAllow|groupAuth|groupSender)/i;
const storeSourceCallNames = new Set([
"readChannelAllowFromStore",
"readChannelAllowFromStoreSync",
"readStoreAllowFromForDmPolicy",
]);
const allowedResolverCallNames = new Set([
"resolveEffectiveAllowFromLists",
"resolveDmGroupAccessWithLists",
"resolveMattermostEffectiveAllowFromLists",
"resolveIrcEffectiveAllowlists",
]);
function getDeclarationNameText(name) {
if (ts.isIdentifier(name)) {
return name.text;
}
if (ts.isObjectBindingPattern(name) || ts.isArrayBindingPattern(name)) {
return name.getText();
}
return null;
}
function containsPairingStoreSource(node) {
let found = false;
const visit = (current) => {
if (found) {
return;
}
if (ts.isIdentifier(current) && storeIdentifierRe.test(current.text)) {
found = true;
return;
}
if (ts.isCallExpression(current)) {
const callName = getCallName(current);
if (callName && storeSourceCallNames.has(callName)) {
found = true;
return;
}
}
ts.forEachChild(current, visit);
};
visit(node);
return found;
}
function getCallName(node) {
if (!ts.isCallExpression(node)) {
return null;
}
if (ts.isIdentifier(node.expression)) {
return node.expression.text;
}
if (ts.isPropertyAccessExpression(node.expression)) {
return node.expression.name.text;
}
return null;
}
function isSuspiciousNormalizeWithStoreCall(node) {
if (!ts.isCallExpression(node)) {
return false;
}
if (!ts.isIdentifier(node.expression) || node.expression.text !== "normalizeAllowFromWithStore") {
return false;
}
const firstArg = node.arguments[0];
if (!firstArg || !ts.isObjectLiteralExpression(firstArg)) {
return false;
}
let hasStoreProp = false;
let hasGroupAllowProp = false;
for (const property of firstArg.properties) {
if (!ts.isPropertyAssignment(property)) {
continue;
}
const name = getPropertyNameText(property.name);
if (!name) {
continue;
}
if (name === "storeAllowFrom" && containsPairingStoreSource(property.initializer)) {
hasStoreProp = true;
}
if (name === "allowFrom" && groupNameRe.test(property.initializer.getText())) {
hasGroupAllowProp = true;
}
}
return hasStoreProp && hasGroupAllowProp;
}
function findViolations(content, filePath) {
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
const violations = [];
const visit = (node) => {
if (ts.isVariableDeclaration(node) && node.initializer) {
const name = getDeclarationNameText(node.name);
if (name && groupNameRe.test(name) && containsPairingStoreSource(node.initializer)) {
const callName = getCallName(node.initializer);
if (callName && allowedResolverCallNames.has(callName)) {
ts.forEachChild(node, visit);
return;
}
violations.push({
line: toLine(sourceFile, node),
reason: `group-scoped variable "${name}" references pairing-store identifiers`,
});
}
}
if (ts.isPropertyAssignment(node)) {
const propName = getPropertyNameText(node.name);
if (propName && groupNameRe.test(propName) && containsPairingStoreSource(node.initializer)) {
violations.push({
line: toLine(sourceFile, node),
reason: `group-scoped property "${propName}" references pairing-store identifiers`,
});
}
}
if (isSuspiciousNormalizeWithStoreCall(node)) {
violations.push({
line: toLine(sourceFile, node),
reason: "group allowlist uses normalizeAllowFromWithStore(...) with pairing-store entries",
});
}
ts.forEachChild(node, visit);
};
visit(sourceFile);
return violations;
}
async function main() {
const violations = await collectFileViolations({
sourceRoots,
repoRoot,
findViolations,
skipFile: (filePath) => allowedFiles.has(filePath),
});
if (violations.length === 0) {
return;
}
console.error("Found pairing-store identifiers referenced in group auth composition:");
for (const violation of violations) {
console.error(`- ${violation.path}:${violation.line} (${violation.reason})`);
}
console.error(
"Group auth must be composed via shared resolvers (resolveDmGroupAccessWithLists / resolveEffectiveAllowFromLists).",
);
process.exit(1);
}
runAsScript(import.meta.url, main);