-
Notifications
You must be signed in to change notification settings - Fork 40
t3657: address PR #1688 review feedback — path resolution, sqlite busy_timeout, shared token extraction #4706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -221,3 +221,42 @@ portable_timeout() { | |||||||||||||||||||||||||||||||||||||
| timeout_sec "$@" | ||||||||||||||||||||||||||||||||||||||
| return $? | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ####################################### | ||||||||||||||||||||||||||||||||||||||
| # Extract token counts from a worker log file (t1114) | ||||||||||||||||||||||||||||||||||||||
| # Supports camelCase (opencode JSON) and snake_case (claude CLI JSON) formats. | ||||||||||||||||||||||||||||||||||||||
| # Results are stored in module-level globals _EXTRACT_TOKENS_IN and | ||||||||||||||||||||||||||||||||||||||
| # _EXTRACT_TOKENS_OUT. Callers copy these into their own local variables. | ||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||
| # Usage: | ||||||||||||||||||||||||||||||||||||||
| # local tokens_in="" tokens_out="" | ||||||||||||||||||||||||||||||||||||||
| # extract_tokens_from_log "$log_file" | ||||||||||||||||||||||||||||||||||||||
| # tokens_in="$_EXTRACT_TOKENS_IN" | ||||||||||||||||||||||||||||||||||||||
| # tokens_out="$_EXTRACT_TOKENS_OUT" | ||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||
| # $1: log_file path (may be empty or non-existent — handled gracefully) | ||||||||||||||||||||||||||||||||||||||
| ####################################### | ||||||||||||||||||||||||||||||||||||||
| _EXTRACT_TOKENS_IN="" | ||||||||||||||||||||||||||||||||||||||
| _EXTRACT_TOKENS_OUT="" | ||||||||||||||||||||||||||||||||||||||
| extract_tokens_from_log() { | ||||||||||||||||||||||||||||||||||||||
| local log_file="$1" | ||||||||||||||||||||||||||||||||||||||
| _EXTRACT_TOKENS_IN="" | ||||||||||||||||||||||||||||||||||||||
| _EXTRACT_TOKENS_OUT="" | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if [[ -z "$log_file" || ! -f "$log_file" ]]; then | ||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| local raw_in raw_out | ||||||||||||||||||||||||||||||||||||||
| raw_in=$(grep -oE '"inputTokens":[0-9]+' "$log_file" 2>/dev/null | tail -1 | grep -oE '[0-9]+' || true) | ||||||||||||||||||||||||||||||||||||||
| raw_out=$(grep -oE '"outputTokens":[0-9]+' "$log_file" 2>/dev/null | tail -1 | grep -oE '[0-9]+' || true) | ||||||||||||||||||||||||||||||||||||||
| if [[ -z "$raw_in" ]]; then | ||||||||||||||||||||||||||||||||||||||
| raw_in=$(grep -oE '"input_tokens":[0-9]+' "$log_file" 2>/dev/null | tail -1 | grep -oE '[0-9]+' || true) | ||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||
| if [[ -z "$raw_out" ]]; then | ||||||||||||||||||||||||||||||||||||||
| raw_out=$(grep -oE '"output_tokens":[0-9]+' "$log_file" 2>/dev/null | tail -1 | grep -oE '[0-9]+' || true) | ||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+250
to
+258
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation for extracting token counts can be inefficient, as it may read the log file from disk up to four separate times. For large log files, this can lead to a noticeable performance degradation. You can significantly improve efficiency by reading the file only once to gather all potential matches. Furthermore, instead of repeated
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||
| [[ -n "$raw_in" ]] && _EXTRACT_TOKENS_IN="$raw_in" | ||||||||||||||||||||||||||||||||||||||
| [[ -n "$raw_out" ]] && _EXTRACT_TOKENS_OUT="$raw_out" | ||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is vulnerable to SQL injection. The variables
$mem_idand$sql_strategyare directly embedded into the SQL query string. If their values contain a single quote, an attacker could break out of the string and execute arbitrary SQL commands, which is a critical security risk.For
sqlite3in shell scripts, the most robust way to prevent SQL injection is to use parameterized queries with.param setwithin a helper function. This separates the SQL command from the data, eliminating injection risks. As an immediate fix for this line, you must escape single quotes within these variables before they are used in the query. The standard way to escape a single quote in SQL is to double it (''). You can achieve this in bash using parameter expansion:${variable//'/'/''}.References
--argforjq) at each point of use.sqlite3, create a helper function that uses.param setfor safe parameterized bindings instead of direct string interpolation.