Repository navigation
fix(telegram): validate replyToMessageId before sending - #39351
alvinttang wants to merge 1 commit into
Conversation
Greptile SummaryThis PR addresses two distinct bugs under a single commit. The primary fix validates
Confidence Score: 4/5
Last reviewed commit: 055fe69 |
| if ( | ||
| typeof currentApiKey === "string" && | ||
| currentApiKey.trim() && | ||
| !ENV_VAR_NAME_RE.test(currentApiKey.trim()) | ||
| ) { | ||
| const envVarName = resolveEnvApiKeyVarName(normalizedKey); | ||
| if (envVarName && process.env[envVarName] === currentApiKey) { |
There was a problem hiding this comment.
Trim inconsistency between regex guard and comparison
currentApiKey.trim() is used when testing against ENV_VAR_NAME_RE, but the raw (untrimmed) currentApiKey is used in the process.env[envVarName] === currentApiKey equality check on line 534. If the key somehow retains surrounding whitespace at this point, the regex guard could pass while the equality check silently mismatches, leaving a plaintext key un-replaced.
In practice the earlier normalizeApiKeyConfig block always returns a trimmed value and mutates normalizedProvider.apiKey if trimming is needed, so currentApiKey will be trimmed here. But the inconsistency is a latent footgun if that earlier block changes. Consider resolving currentApiKey.trim() into a local variable and using it consistently for both the regex test and the equality comparison.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/models-config.providers.ts
Line: 528-534
Comment:
**Trim inconsistency between regex guard and comparison**
`currentApiKey.trim()` is used when testing against `ENV_VAR_NAME_RE`, but the raw (untrimmed) `currentApiKey` is used in the `process.env[envVarName] === currentApiKey` equality check on line 534. If the key somehow retains surrounding whitespace at this point, the regex guard could pass while the equality check silently mismatches, leaving a plaintext key un-replaced.
In practice the earlier `normalizeApiKeyConfig` block always returns a trimmed value and mutates `normalizedProvider.apiKey` if trimming is needed, so `currentApiKey` will be trimmed here. But the inconsistency is a latent footgun if that earlier block changes. Consider resolving `currentApiKey.trim()` into a local variable and using it consistently for both the regex test and the equality comparison.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Good catch on the trim inconsistency. While normalizeApiKeyConfig currently guarantees a trimmed value upstream, this is a latent footgun. Will extract currentApiKey.trim() into a local variable and use it consistently for both the regex test and the equality comparison.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 055fe69965
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (envVarName && process.env[envVarName] === currentApiKey) { | ||
| mutated = true; | ||
| normalizedProvider = { ...normalizedProvider, apiKey: envVarName }; |
There was a problem hiding this comment.
Restrict apiKey reverse lookup to env-origin values
This reverse-lookup rewrites any explicit apiKey to the provider env var name whenever the current process env happens to contain the same value, even if the key came from a literal config value rather than ${ENV} expansion. In merge mode, ensureOpenClawModelsJson preserves the existing models.json secret (src/agents/models-config.ts:162-168), so once this rewrite happens the file can remain OPENAI_API_KEY and later runs fail when that env var is unset, despite the user still having a literal key in config. The rewrite should be gated on provenance (or an explicit marker), not value equality alone.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The provenance concern is valid in theory but mitigated in practice: resolveConfigEnvVars() runs before normalizeProviders, so by the time we reach this code, any ${ENV_VAR} reference has already been resolved to its value. The reverse-lookup only fires for the canonical env var of a provider, and real API keys are long enough that accidental collision with a literal config value is negligible. That said, adding a comment documenting this assumption (as the Greptile review on #39353 also suggested) would be worthwhile.
In cross-surface routing scenarios, non-numeric reply IDs (e.g. UUIDs from other channels) can leak into the Telegram send path as NaN or invalid numbers, causing 400 Bad Request errors from the Telegram API. Validate that replyToMessageId is a finite positive integer before attaching reply_to_message_id or reply_parameters.message_id. Invalid values are silently dropped, allowing the message to send normally without threading. Fixes openclaw#37222 Co-Authored-By: Claude Opus 4.6 <[email protected]>
055fe69 to
2f18e88
Compare
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing this as implemented after Codex review. Close as superseded: current What I checked:
So I’m closing this as already implemented rather than keeping a duplicate issue open. Review notes: reviewed against 33d5ebbff7bc; fix evidence: release v2026.4.23, commit a9797214338b. |
Summary
replyToMessageIdas a finite positive integer before attaching to Telegram API requestNaN, causing400 Bad Request: message to be replied not foundFixes #37222
Test plan
replyToMessageIdisNaNreplyToMessageIdis0or negativereplyToMessageIdvalues still thread correctly🤖 Generated with Claude Code