Skip to content

Commit

Permalink
Obfuscate Windows usernames from troubleshooting info
Browse files Browse the repository at this point in the history
  • Loading branch information
anttimaki committed Jan 29, 2025
1 parent 1d33c88 commit bc47bc5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/store/modules/ProfileModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ManagerSettings from '../../r2mm/manager/ManagerSettings';
import * as PackageDb from '../../r2mm/manager/PackageDexieStore';
import ModListSort from '../../r2mm/mods/ModListSort';
import ProfileModList from '../../r2mm/mods/ProfileModList';
import FileUtils from '../../utils/FileUtils';
import SearchUtils from '../../utils/SearchUtils';

interface State {
Expand Down Expand Up @@ -301,9 +302,9 @@ export default {

async generateTroubleshootingString({dispatch, getters, rootGetters, rootState}): Promise<string> {
const steamDirectory = await GameDirectoryResolverProvider.instance.getSteamDirectory();
const steamPath = steamDirectory instanceof R2Error ? '-' : steamDirectory;
const steamPath = steamDirectory instanceof R2Error ? '-' : FileUtils.hideWindowsUsername(steamDirectory);
const gameDirectory = await GameDirectoryResolverProvider.instance.getDirectory(rootState.activeGame);
const gamePath = gameDirectory instanceof R2Error ? '-' : gameDirectory;
const gamePath = gameDirectory instanceof R2Error ? '-' : FileUtils.hideWindowsUsername(gameDirectory);
const packageCacheDate = await PackageDb.getLastPackageListUpdateTime(rootState.activeGame.internalFolderName);
const packageCacheSize = await PackageDb.getPackageCount(rootState.activeGame.internalFolderName);
const packageVuexState: string = await dispatch('tsMods/generateTroubleshootingString', null, {root: true});
Expand All @@ -313,7 +314,7 @@ export default {
Game: ${rootState.activeGame.displayName} (${rootState.activeGame.activePlatform.storePlatform})
Steam path: ${steamPath}
Game path: ${gamePath}
Profile path: ${getters.activeProfile.getProfilePath()}
Profile path: ${FileUtils.hideWindowsUsername(getters.activeProfile.getProfilePath())}
Custom launch arguments: ${rootGetters.settings.getContext().gameSpecific.launchParameters || '-'}
Mod list in cache: ${packageCacheSize} mods, hash updated ${packageCacheDate || 'never'}
Mod list in memory: ${packageVuexState}
Expand Down
9 changes: 9 additions & 0 deletions src/utils/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export default class FileUtils {
return Promise.resolve();
}

// Obfuscates the Windows username if it's part of the path.
public static hideWindowsUsername(dir: string) {
const separator = dir.includes('/') ? '/' : '\\';
return dir.replace(
/([A-Za-z]:)[\\\/]Users[\\\/][^\\\/]+[\\\/]/,
`$1${separator}Users${separator}***${separator}`
);
}

public static humanReadableSize(bytes: number) {
// NumberFormat renders GBs as BBs ("billion bytes") when using "byte" unit type.
if (bytes > 999999999 && bytes < 1000000000000) {
Expand Down
69 changes: 69 additions & 0 deletions test/jest/__tests__/utils/utils.FileUtils.ts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import FileUtils from "../../../../src/utils/FileUtils";

describe("FileUtils.hideWindowsUsername", () => {
it("Doesn't change slashes", () => {
expect(
FileUtils.hideWindowsUsername('C:\\Users\\Alice\\appData')
).toStrictEqual('C:\\Users\\***\\appData');

expect(
FileUtils.hideWindowsUsername('C:/Users/Bob/appData')
).toStrictEqual('C:/Users/***/appData');
});

it("Doesn't change drive letters", () => {
expect(
FileUtils.hideWindowsUsername('C:\\Users\\Charlie\\appData')
).toStrictEqual('C:\\Users\\***\\appData');

expect(
FileUtils.hideWindowsUsername('x:\\Users\\David\\appData')
).toStrictEqual('x:\\Users\\***\\appData');
});

it("Doesn't change the rest of the path", () => {
expect(
FileUtils.hideWindowsUsername('C:\\Users\\Eve\\')
).toStrictEqual('C:\\Users\\***\\');

expect(
FileUtils.hideWindowsUsername('C:\\Users\\Frank\\Desktop')
).toStrictEqual('C:\\Users\\***\\Desktop');

expect(
FileUtils.hideWindowsUsername('C:\\Users\\Grace\\Desktop\\')
).toStrictEqual('C:\\Users\\***\\Desktop\\');

expect(
FileUtils.hideWindowsUsername('C:\\Users\\Heidi\\Desktop\\file.txt')
).toStrictEqual('C:\\Users\\***\\Desktop\\file.txt');
});

it("Doesn't affect other paths", () => {
expect(
FileUtils.hideWindowsUsername('C:\\LUsers\\Ivan\\')
).toStrictEqual('C:\\LUsers\\Ivan\\');

expect(
FileUtils.hideWindowsUsername('C:\\temp\\Users\\Judy\\')
).toStrictEqual('C:\\temp\\Users\\Judy\\');
});

it("Isn't tricked by odd usernames", () => {
expect(
FileUtils.hideWindowsUsername('C:\\Users\\123\\')
).toStrictEqual('C:\\Users\\***\\');

expect(
FileUtils.hideWindowsUsername('C:\\Users\\@admin\\')
).toStrictEqual('C:\\Users\\***\\');

expect(
FileUtils.hideWindowsUsername('C:\\Users\\_\\')
).toStrictEqual('C:\\Users\\***\\');

expect(
FileUtils.hideWindowsUsername('C:\\Users\\***\\')
).toStrictEqual('C:\\Users\\***\\');
});
});

0 comments on commit bc47bc5

Please sign in to comment.