Fix logging out users on upgrade / app recreate with same URL (fix #1176) (PR #1179)

This is a follow-up of https://github.com/nativefier/nativefier/pull/1162#discussion_r623766247

PR #1162 introduced a new `generateRandomSuffix` helper function,
used it for its needs (avoiding collisions of injected js/css).

But it also replaced existing appname normalizing logic with it,
introducing randomness in a place that used to be deterministic.

As a result, starting with dd6e15fb5 / v43.1.0, re-creating an app would cause
the app to use a different appName, thus a different appData folder, thus
losing user data including cookies.

This PR leaves the `--inject` fixes of #1176, but reverts the appName logic
to the pre-#1176 code.
This commit is contained in:
Ronan Jouchet 2021-05-03 11:16:50 -04:00 committed by GitHub
parent beae6e872b
commit 331fe8db39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 6 deletions

View File

@ -0,0 +1,11 @@
import { normalizeAppName } from './prepareElectronApp';
describe('normalizeAppName', () => {
test('it is stable', () => {
// Non-determinism / unstability would cause using a different appName
// at each app regen, thus a different appData folder, which would cause
// losing user state, including login state through cookies.
const normalizedTrello = normalizeAppName('Trello', 'https://trello.com');
expect(normalizedTrello).toBe('trello-nativefier-679e8e');
});
});

View File

@ -1,3 +1,4 @@
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
@ -127,9 +128,16 @@ async function maybeCopyScripts(srcs: string[], dest: string): Promise<void> {
}
}
function normalizeAppName(appName: string): string {
// use a simple random string to prevent collisions
const postFixHash = generateRandomSuffix();
/**
* Use a basic 6-character hash to prevent collisions. The hash is deterministic url & name,
* so that an upgrade (same URL) of an app keeps using the same appData folder.
* Warning! Changing this normalizing & hashing will change the way appNames are generated,
* changing appData folder, and users will get logged out of their apps after an upgrade.
*/
export function normalizeAppName(appName: string, url: string): string {
const hash = crypto.createHash('md5');
hash.update(url);
const postFixHash = hash.digest('hex').substring(0, 6);
const normalized = appName
.toLowerCase()
.replace(/[,:.]/g, '')
@ -137,10 +145,14 @@ function normalizeAppName(appName: string): string {
return `${normalized}-nativefier-${postFixHash}`;
}
function changeAppPackageJsonName(appPath: string, name: string): void {
function changeAppPackageJsonName(
appPath: string,
name: string,
url: string,
): void {
const packageJsonPath = path.join(appPath, '/package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
const normalizedAppName = normalizeAppName(name);
const normalizedAppName = normalizeAppName(name, url);
packageJson.name = normalizedAppName;
log.debug(`Updating ${packageJsonPath} 'name' field to ${normalizedAppName}`);
@ -184,5 +196,9 @@ export async function prepareElectronApp(
} catch (err) {
log.error('Error copying injection files.', err);
}
changeAppPackageJsonName(dest, options.packager.name);
changeAppPackageJsonName(
dest,
options.packager.name,
options.packager.targetUrl,
);
}