* Parse a Spring config file (YAML or .properties) and emit one `constant` * node per LEAF key, with `qualifiedName` = the dotted path. Leaf keys are * what `@Value("${k}")` references hit; intermediate keys aren't bound by * Spring's `@Value` (a `@ConfigurationProperties` class binds a SUBTREE,
( filePath: string, content: string, )
| 347 | * those references are resolved at lookup time by prefix-suffix matching). |
| 348 | */ |
| 349 | function extractSpringConfig( |
| 350 | filePath: string, |
| 351 | content: string, |
| 352 | ): { nodes: Node[]; references: UnresolvedRef[] } { |
| 353 | const nodes: Node[] = []; |
| 354 | const isProperties = /\.properties$/i.test(filePath); |
| 355 | const lang = isProperties ? 'properties' : 'yaml'; |
| 356 | const now = Date.now(); |
| 357 | |
| 358 | const emitLeaf = (dottedKey: string, line: number, valueText: string) => { |
| 359 | if (!dottedKey) return; |
| 360 | nodes.push({ |
| 361 | id: `spring-config:${filePath}:${line}:${dottedKey}`, |
| 362 | kind: 'constant', |
| 363 | name: dottedKey.split('.').pop() ?? dottedKey, |
| 364 | qualifiedName: dottedKey, |
| 365 | filePath, |
| 366 | startLine: line, |
| 367 | endLine: line, |
| 368 | startColumn: 0, |
| 369 | endColumn: valueText.length, |
| 370 | language: lang, |
| 371 | signature: dottedKey, |
| 372 | // SECURITY (#383): store the KEY only, never the value. Config files |
| 373 | // routinely hold secrets (DB passwords, API keys, JDBC URLs with embedded |
| 374 | // credentials), and surfacing the value here pushes it into agent context |
| 375 | // unbidden (it lands in codegraph_node/explore output via the docstring). |
| 376 | // The key is all `@Value`/`@ConfigurationProperties` resolution needs; an |
| 377 | // agent that genuinely needs a value can read the file directly. |
| 378 | updatedAt: now, |
| 379 | }); |
| 380 | }; |
| 381 | |
| 382 | if (isProperties) { |
| 383 | // Properties format: `k1.k2.k3 = value` (or `:` separator, or no value). |
| 384 | // Lines starting with `#`/`!` are comments. Backslash continuations are |
| 385 | // valid but rare; we don't try to join them (a continued value is still |
| 386 | // a value of the same key). |
| 387 | const lines = content.split(/\r?\n/); |
| 388 | for (let i = 0; i < lines.length; i++) { |
| 389 | const raw = lines[i] ?? ''; |
| 390 | const trimmed = raw.trim(); |
| 391 | if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('!')) continue; |
| 392 | const sep = (() => { |
| 393 | for (let j = 0; j < raw.length; j++) { |
| 394 | const ch = raw[j]; |
| 395 | if (ch === '=' || ch === ':') return j; |
| 396 | if (ch === '\\' && raw[j + 1]) { j++; continue; } |
| 397 | } |
| 398 | return -1; |
| 399 | })(); |
| 400 | if (sep < 0) continue; |
| 401 | const key = raw.slice(0, sep).trim(); |
| 402 | const val = raw.slice(sep + 1).trim(); |
| 403 | emitLeaf(key, i + 1, val); |
| 404 | } |
| 405 | return { nodes, references: [] }; |
| 406 | } |