Skip to content

Keep only ASCII from the hostname in Posit Assistant staging directory names - #19626

Merged
gtritchie merged 5 commits into
mainfrom
bugfix/pai-staging-ascii-hostname
Nov 14, 2026
Merged

gtritchie merged 5 commits into
mainfrom
bugfix/pai-staging-ascii-hostname

Conversation

@gtritchie

Copy link
Copy Markdown
Member

Fixes #19624.

Installing or updating Posit Assistant extracts into a staging directory named after the hostname. The hostname was sanitized byte by byte with std::isalnum(), which accepts some bytes above 0x7F in macOS UTF-8 locales and Windows code-page locales. A hostname with an accented letter then produced an invalid UTF-8 directory name: macOS refused to create it ("Illegal byte sequence"), and on Windows with R < 4.2, R's unzip() misread it and extracted elsewhere.

stagingDirName() now keeps only ASCII letters, digits, - and ., checked explicitly, independent of the locale; every other byte becomes _. prepareStagingDir() takes the hostname as a parameter so tests can pass a non-ASCII one; installPackage() passes core::system::getHostname().

Tests

Two new ChatSlots tests stage an install with a UTF-8 hostname (jos\xC3\xA9-MacBook) and a code-page-1252 one (jos\xE9-PC), in a locale where the old check let high bytes through (.1252 on Windows, en_US.UTF-8 on macOS), and check that the directory is created with an ASCII name. On macOS both failed before the change and pass after it. On Linux they pass either way, because glibc's isalnum() rejects all bytes above 0x7F.

The paths to R's unzip() and the download are still passed in the native encoding, which breaks on Windows with R < 4.2 whenever the data directory path is non-ASCII; that is tracked separately in #19625.

The staging directory name sanitized the hostname with std::isalnum(),
which accepts some bytes above 0x7F in macOS UTF-8 and Windows code-page
locales. A hostname like "jose-MacBook" with an accented e became
"jos\xC3_-MacBook": invalid UTF-8, which APFS refuses with EILSEQ and
R < 4.2 on Windows misreads as a different directory. The sanitizer now
keeps only ASCII letters, digits, '-' and '.', whatever the locale.

prepareStagingDir() takes the hostname as a parameter so tests can pass
a non-ASCII one; getHostname() caches its first result.

Addresses #19624.
@gtritchie gtritchie added this to the 2026.12 (Parrot Balsam) milestone Nov 13, 2026
Comment on lines +53 to +57
for (const char* name : { ".1252", "en_US.UTF-8" })
{
if (std::setlocale(LC_CTYPE, name) != nullptr)
break;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If neither .1252 nor en_US.UTF-8 can be set, this falls through silently and the two tests that use it run in the current locale. There the old isalnum() check also rejects high bytes, so the tests pass with or without the fix and stop guarding the regression.

Unlikely on stock images (macOS has en_US.UTF-8, where isalnum(0xC3) and isalnum(0xE9) are true; Windows always has .1252), but it fails quietly when it does. Consider recording whether a locale was set and having the tests GTEST_SKIP() (or assert, as StringUtilsTests.cpp does with ASSERT_NE(nullptr, _wsetlocale(...))) when none was, except on Linux, where the comment already says they pass either way.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b52501c. The helper now records whether it set .1252 or en_US.UTF-8, and outside Linux reports a failure (ADD_FAILURE()) when neither could be set, so the two tests fail instead of passing vacuously. Checked by temporarily substituting nonexistent locale names: both tests failed with "Could not set LC_CTYPE to .1252 or en_US.UTF-8".

Without .1252 or en_US.UTF-8, the locale helper fell through to a locale
where the old isalnum() check was already safe, so the two staging-name
tests passed with or without the fix. Outside Linux, where glibc makes
the locale irrelevant, the helper now fails the test instead.

Addresses #19624.
const char* current = std::setlocale(LC_CTYPE, nullptr);
previous_ = current != nullptr ? current : "C";

bool set = false;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux the only read of set (inside #ifndef __linux__) is preprocessed out, so set is written but never read and the -Wall build (src/cpp/CMakeLists.txt:65) warns -Wunused-but-set-variable. Reproduced with clang -Wall on a copy with that block removed: "variable 'set' set but not used"; GCC's -Wall includes the same warning.

Either [[maybe_unused]] bool set = false; or moving the set bookkeeping inside the #ifndef avoids it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 64270b3. The helper no longer has the flag: it returns as soon as a locale is set, and reports the failure (outside Linux) after the loop. Compiled the helper with clang -Wall -Wextra -Werror with and without -D__linux__: no warnings either way. The two tests still fail with "Could not set LC_CTYPE to .1252 or en_US.UTF-8" when given nonexistent locale names.

On Linux the only read of the locale helper's "set" flag is preprocessed
out, so -Wall warned "variable 'set' set but not used". The helper now
returns as soon as a locale is set and reports the failure after the
loop, with no flag.

Addresses #19624.
@gtritchie
gtritchie requested a review from kevinushey November 13, 2026 21:33
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
5623 0 246 2
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 4 0 16 0
✅ desktop-linux 1406 0 47 1
✅ desktop-macos 1415 0 40 0
✅ desktop-windows 1402 0 57 1
✅ server-linux 1348 0 54 0
✅ setup 48 0 32 0

View report online


All platforms - commit 52f0f4f555bc1173f314e13eece73d7b08a0107f - Run #2283

@kevinushey kevinushey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I checked the premise locally on macOS: under en_US.UTF-8, isalnum() accepts 0xC3 and 0xE9 but not 0xA9, so the old sanitizer turns jos\xC3\xA9-MacBook into jos\xC3_-MacBook and mkdir rejects it with "Illegal byte sequence". The explicit ASCII ranges give jos__-MacBook and jos_-PC, as the tests expect. Nothing else calls prepareStagingDir() or reads the hostname back out of a staging directory name, so the change in sanitizing has no other consumers.

One non-blocking note: PR CI runs the C++ unit tests on Linux only (jenkins/Jenkinsfile.pull-request-build), where the two new tests pass with or without the fix. The .1252 branch of ScopedHighByteCType will first run in the Windows daily build (jenkins/Jenkinsfile.windows), so that is where a problem with it would show up.

@gtritchie
gtritchie merged commit 85e22ff into main Nov 14, 2026
13 of 15 checks passed
@gtritchie
gtritchie deleted the bugfix/pai-staging-ascii-hostname branch November 14, 2026 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Posit Assistant install fails on macOS, and on Windows with R < 4.2, when the hostname has non-ASCII characters

2 participants