codex icon indicating copy to clipboard operation
codex copied to clipboard

Windows fs/readDirectory maps distinct filenames to the same name

Open dovvnloading opened this issue 2 weeks ago • 1 comments

Summary

On Windows, fs/readDirectory can return identical names for distinct files when one filename contains an unpaired UTF-16 surrogate. A client that uses either returned name to read a file can access the other file instead.

Reproduction

On Windows with Python and codex on PATH, run this isolated fixture:

import base64, json, os, pathlib, subprocess, tempfile

with tempfile.TemporaryDirectory(dir=os.getcwd()) as tmp:
    root = pathlib.Path(tmp)
    home, folder = root / 'home', root / 'folder'
    home.mkdir(); folder.mkdir()
    (home / 'config.toml').write_text('[features]\nplugins = false\n', encoding='utf8')
    name = 'bad\ud800.txt'
    (folder / name).write_bytes(b'test')
    (folder / 'bad\ufffd.txt').write_bytes(b'different file')
    proc = subprocess.Popen(['codex', 'app-server', '--disable', 'plugins'], stdin=subprocess.PIPE,
        stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, encoding='utf8',
        env={**os.environ, 'CODEX_HOME': str(home), 'CODEX_APP_SERVER_DISABLE_MANAGED_CONFIG': '1'})
    def call(i, method, params):
        proc.stdin.write(json.dumps({'id':i, 'method':method, 'params':params})+'\n'); proc.stdin.flush()
        while True:
            line = proc.stdout.readline()
            assert line
            msg = json.loads(line)
            if msg.get('id') == i: return msg
    try:
        assert 'result' in call(1, 'initialize', {'clientInfo': {'name':'repro','version':'1'}})
        proc.stdin.write('{"method":"initialized","params":{}}\n'); proc.stdin.flush()
        result = call(2, 'fs/readDirectory', {'path':str(folder)})['result']['entries']
        print('actual:', ascii(os.listdir(folder)))
        print('returned:', ascii(result))
        names = [item['fileName'] for item in result]
        print('returned distinct names:', len(set(names)))
        assert names.count('bad\ufffd.txt') == 2
        read = call(3, 'fs/readFile', {'path': str(folder / names[0])})
        contents = base64.b64decode(read['result']['dataBase64'])
        print('contents from either returned path:', contents)
        assert contents == b'different file'
    finally:
        proc.stdin.close()
        try: proc.wait(timeout=10)
        except subprocess.TimeoutExpired: proc.kill(); proc.wait()

With codex-cli 0.167.0-alpha.3.2, the directory contained two distinct names, bad\ud800.txt and bad\ufffd.txt, with different contents. The RPC returned two entries both named bad\ufffd.txt; reading either returned name yielded the latter file's contents. This was reproduced independently.

Expected behavior and possible fix

A directory listing should not silently give two files the same addressable name. Provide a lossless native-path representation through the listing and subsequent file APIs, or explicitly report an unrepresentable filename rather than returning a misleading name. Add Windows tests for an unpaired surrogate and an actual replacement-character filename together, as well as normal Unicode names.

The local filesystem adapter uses to_string_lossy() on entry names. Relevant source matches main at c95b72ed6f00348c6da5ab95b740398094023ce8; testing used the installed CLI rather than a main build. Searches for fs/readDirectory, unpaired surrogates and lossy filename conversion found no equivalent report.

dovvnloading avatar Nov 11 '26 23:11 dovvnloading

I reproduced this with the official standalone App Server 0.169.0 and the desktop-bundled 0.167.0-alpha.4.1 backend on Windows 11 x64 (build 26200, NTFS). The test used local stdio calls to fs/readDirectory and fs/readFile.

Files on disk (escaped names) Directory result Read using the returned name
bad\ufffd.txt only bad\ufffd.txt Reads the expected bytes
bad\ud800.txt only bad\ufffd.txt RPC error -32603
Both, with distinct contents Two bad\ufffd.txt entries Both reads return the U+FFFD file's bytes

The first case confirms that a real U+FFFD name works. The second shows that even a one-entry listing can return a name that cannot be opened. On 0.169.0, ten additional controls with ASCII, spaces, Chinese and accented characters all returned the exact name and expected bytes. The controls varied the parent directory and the filename separately. Release · Name conversion in 0.169.0

A Windows regression test can cover all three rows. The valid U+FFFD name should remain addressable, and a filename that the string-based API cannot represent should produce an explicit error or be omitted from the list. Directory entry schema

CQ880 avatar Nov 13 '26 14:11 CQ880