Repository navigation
Description
Summary
#7113's fix changed the chat-store identity from sanitizeFilename(username) to a SHA-256 hex digest,
which is right. The lazy migration it added to keep old history reachable reintroduces the same
cross-user access the issue was about, in two ways.
One awards the shared directory to whichever colliding user is looked up first, giving them read and
delete access to the other's chats. The other lets a user whose name is 64 lowercase hex characters
move another user's live chat store into their own.
Where (HEAD bb0a526)
server/src/main/java/com/arcadedb/server/ai/ChatStorage.java:203-216:
private void migrateLegacyDirectoryIfPresent(final String username, final File hashedDir) {
if (hashedDir.exists())
return;
final File legacyDir = Paths.get(rootPath, "chats", sanitizeFilename(username)).toFile();
if (!legacyDir.exists() || legacyDir.equals(hashedDir))
return;
try {
Files.move(legacyDir.toPath(), hashedDir.toPath());with :224-228:
static String sanitizeFilename(final String input) {
if (input == null || input.isEmpty())
return "default";
return input.replaceAll("[^a-zA-Z0-9_\\-]", "_");
}and :243-252, which emits lowercase hex via HexFormat.of().formatHex(hash).
1. The collision is awarded, not resolved
Two usernames that sanitised to the same legacy name shared one directory - the javadoc's own example
is user@corp.com, user.corp.com, user_corp_com. That shared directory contains both users'
chats.
After the upgrade, whichever of them is looked up first runs the migration and Files.moves the whole
shared directory into its own hashed path. That user now owns, can read and can delete every chat the
other ever wrote. The second user gets a fresh empty store.
The javadoc at :200-202 states only half of this:
empty hashed directory - it can no longer see the first user's history.
The half it omits is that the first user gains the second's history. That is the cross-user read
#7113 reported, made permanent and one-directional rather than fixed.
2. A 64-hex-character username selects another user's live directory
sanitizeFilename only rewrites characters outside [a-zA-Z0-9_-], so a 64-character lowercase hex
string passes through byte-identical. hashUsername emits lowercase hex of exactly that length.
So for a user whose username equals another user's SHA-256 digest, sanitizeFilename(username) names
that other user's current, live hashed directory. On first access, with no hashed directory of
their own yet, the migration moves the victim's live store into the attacker's.
This does not require the legacy scheme to have ever been used. It is reachable on a fresh install.
Suggested fix
The migration needs to stop trusting a name derived from user-controlled text:
- Do not move a directory that is a valid hash. Refuse the migration when
sanitizeFilename(username)
matches^[0-9a-f]{64}$, which closes item 2 outright. - Do not move a directory that more than one user could have produced. For item 1, either copy
rather than move and leave the legacy directory in place, or refuse the migration when the legacy
name is ambiguous and surface it to an operator. Silently awarding one user another's data is worse
than losing the history, and the issue this fixes was specifically about cross-user access. - Consider making the migration an explicit, audited, one-off operation rather than a lazy side effect
of a read. A data move that happens on first access is hard to reason about and hard to undo.
Also worth correcting the javadoc, which currently describes the outcome from only the losing user's
side.
Scope / confidence
Confidence high on both items; the migration, sanitizeFilename's character class and hashUsername's
lowercase output were each read in the tree, and item 2 follows from those three facts alone.
Both are new in this window, introduced by the #7113 fix.
Activity
fix(#7620): the legacy chat-directory migration never claims a direct…