Fix paste ownership, input bounds, and IPC validation
Supersedes #5745, #5746, and #5747.
This commit is contained in:
@@ -4,6 +4,7 @@ import { existsSync, statSync } from 'fs'
|
||||
import { basename } from 'path'
|
||||
import { gitExecFileSync, gitExecFileAsync } from './runner'
|
||||
import type { BaseRefSearchResult } from '../../shared/types'
|
||||
import { parseGitRevListAheadBehindCounts } from '../../shared/git-rev-list-output'
|
||||
import {
|
||||
buildHostedRemoteCommitUrl,
|
||||
buildHostedRemoteFileUrl,
|
||||
@@ -363,13 +364,11 @@ export function getRemoteDrift(
|
||||
['rev-list', '--left-right', '--count', `${localRef}...${remoteRef}`],
|
||||
gitExecOptions(repoPath, options)
|
||||
)
|
||||
const [aheadStr, behindStr] = stdout.trim().split(/\s+/)
|
||||
const ahead = Number(aheadStr)
|
||||
const behind = Number(behindStr)
|
||||
if (!Number.isFinite(ahead) || !Number.isFinite(behind)) {
|
||||
const counts = parseGitRevListAheadBehindCounts(stdout)
|
||||
if (counts.status !== 'ok') {
|
||||
return null
|
||||
}
|
||||
return { ahead, behind }
|
||||
return { ahead: counts.ahead, behind: counts.behind }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
// Why: covers two recent classifier fixes — Retry-After honoring on 429
|
||||
// (transient detection must propagate, not silently retry on 250ms cadence)
|
||||
// and stderr extraction from execFile rejections (err.message is unreliable).
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { extractExecError, isTransientGhError, parseRetryAfterMs } from './runner'
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('parseRetryAfterMs', () => {
|
||||
it('returns null when no Retry-After is present', () => {
|
||||
expect(parseRetryAfterMs('HTTP 429 Too Many Requests')).toBeNull()
|
||||
@@ -17,6 +21,20 @@ describe('parseRetryAfterMs', () => {
|
||||
expect(parseRetryAfterMs(' retry-after: 12 \n')).toBe(12_000)
|
||||
})
|
||||
|
||||
it('parses large stderr output without full-string retry-after matching', () => {
|
||||
const matchSpy = vi.spyOn(String.prototype, 'match')
|
||||
const stderr = `${'noise\n'.repeat(10_000)}Retry-After: 12\n`
|
||||
|
||||
expect(parseRetryAfterMs(stderr)).toBe(12_000)
|
||||
const usedRetryAfterMatch = matchSpy.mock.calls.some(
|
||||
([pattern]) =>
|
||||
pattern instanceof RegExp &&
|
||||
pattern.source.startsWith('retry-after:') &&
|
||||
pattern.source.includes('[^\\r\\n]')
|
||||
)
|
||||
expect(usedRetryAfterMatch).toBe(false)
|
||||
})
|
||||
|
||||
it('returns null for malformed values', () => {
|
||||
expect(parseRetryAfterMs('Retry-After: not-a-date')).toBeNull()
|
||||
})
|
||||
|
||||
+46
-3
@@ -1154,11 +1154,10 @@ export function extractExecError(err: unknown): { stderr: string; stdout: string
|
||||
* and burns the retry budget. Also supports HTTP-date Retry-After values.
|
||||
*/
|
||||
export function parseRetryAfterMs(stderr: string): number | null {
|
||||
const m = stderr.match(/retry-after:\s*([^\r\n]+)/i)
|
||||
if (!m) {
|
||||
const raw = findRetryAfterHeaderValue(stderr)
|
||||
if (raw === null) {
|
||||
return null
|
||||
}
|
||||
const raw = m[1].trim()
|
||||
if (/^\d+$/.test(raw)) {
|
||||
const seconds = Number(raw)
|
||||
return Number.isFinite(seconds) ? seconds * 1000 : null
|
||||
@@ -1170,6 +1169,50 @@ export function parseRetryAfterMs(stderr: string): number | null {
|
||||
return Math.max(0, ts - Date.now())
|
||||
}
|
||||
|
||||
function findRetryAfterHeaderValue(stderr: string): string | null {
|
||||
const headerIndex = indexOfAsciiIgnoreCase(stderr, 'retry-after:', 0)
|
||||
if (headerIndex === -1) {
|
||||
return null
|
||||
}
|
||||
let valueStart = headerIndex + 'retry-after:'.length
|
||||
while (valueStart < stderr.length) {
|
||||
const code = stderr.charCodeAt(valueStart)
|
||||
if (code !== 9 && code !== 32) {
|
||||
break
|
||||
}
|
||||
valueStart++
|
||||
}
|
||||
let valueEnd = valueStart
|
||||
while (valueEnd < stderr.length) {
|
||||
const code = stderr.charCodeAt(valueEnd)
|
||||
if (code === 10 || code === 13) {
|
||||
break
|
||||
}
|
||||
valueEnd++
|
||||
}
|
||||
const value = stderr.slice(valueStart, valueEnd).trim()
|
||||
return value.length > 0 ? value : null
|
||||
}
|
||||
|
||||
function indexOfAsciiIgnoreCase(value: string, search: string, fromIndex: number): number {
|
||||
const lastStart = value.length - search.length
|
||||
for (let index = Math.max(0, fromIndex); index <= lastStart; index++) {
|
||||
let matches = true
|
||||
for (let offset = 0; offset < search.length; offset++) {
|
||||
const code = value.charCodeAt(index + offset)
|
||||
const normalizedCode = code >= 65 && code <= 90 ? code + 32 : code
|
||||
if (normalizedCode !== search.charCodeAt(offset)) {
|
||||
matches = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if (matches) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Classify whether a gh execFile rejection is worth retrying.
|
||||
*
|
||||
|
||||
@@ -47,6 +47,7 @@ import { hasWorktreeBaseCommitRef } from './worktree-base-ref-probe'
|
||||
import { getLargeDiffRenderLimit } from '../../shared/large-diff-render-limit'
|
||||
import type { GitRuntimeOptions } from './git-runtime-options'
|
||||
import { gitOptionsForWorktree } from './git-runtime-options'
|
||||
import { parseGitRevListFirstParentOid } from '../../shared/git-rev-list-output'
|
||||
|
||||
const MAX_GIT_SHOW_BYTES = 10 * 1024 * 1024
|
||||
const MAX_STAGED_COMMIT_CONTEXT_BYTES = MAX_GIT_SHOW_BYTES
|
||||
@@ -774,8 +775,8 @@ export async function getCommitCompare(
|
||||
['rev-list', '--parents', '-n', '1', commitOid],
|
||||
gitOptionsForWorktree(worktreePath, options)
|
||||
)
|
||||
const [, firstParent] = stdout.trim().split(/\s+/)
|
||||
summary.parentOid = firstParent ?? null
|
||||
const firstParent = parseGitRevListFirstParentOid(stdout)
|
||||
summary.parentOid = firstParent
|
||||
summary.baseRef = firstParent ? firstParent.slice(0, 7) : 'empty tree'
|
||||
|
||||
const entries = await loadCommitChanges(worktreePath, summary.parentOid, commitOid, options)
|
||||
|
||||
@@ -13,6 +13,7 @@ import type {
|
||||
LocalBaseRefUpdateSuggestion,
|
||||
RemoveWorktreeResult
|
||||
} from '../../shared/types'
|
||||
import { parseGitRevListAheadBehindCounts } from '../../shared/git-rev-list-output'
|
||||
import { gitExecFileAsync, translateWslOutputPaths } from './runner'
|
||||
import { resolveGitDir } from './status'
|
||||
import { hasWorktreeBaseCommitRef } from './worktree-base-ref-probe'
|
||||
@@ -148,13 +149,8 @@ function parseRemoteTrackingLocalBaseRef(
|
||||
}
|
||||
|
||||
function parseRevListDrift(output: string): { ahead: number; behind: number } | null {
|
||||
const [aheadStr, behindStr] = output.trim().split(/\s+/)
|
||||
const ahead = Number(aheadStr)
|
||||
const behind = Number(behindStr)
|
||||
if (!Number.isFinite(ahead) || !Number.isFinite(behind) || ahead < 0 || behind < 0) {
|
||||
return null
|
||||
}
|
||||
return { ahead, behind }
|
||||
const counts = parseGitRevListAheadBehindCounts(output)
|
||||
return counts.status === 'ok' ? { ahead: counts.ahead, behind: counts.behind } : null
|
||||
}
|
||||
|
||||
async function evaluateLocalBaseRefRefreshability(
|
||||
|
||||
Reference in New Issue
Block a user