Skip to content

Commit f8f6424

Browse files
author
antelle
committed
compare fs dates before starting from updated data
1 parent 1158e1b commit f8f6424

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

Diff for: Gruntfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ module.exports = function(grunt) {
300300
files: { 'tmp/desktop/app/index.html': 'dist/index.html' }
301301
},
302302
'desktop-public-key': {
303-
options: { replacements: [{ pattern: '\'PUBLIC_KEY_CONTENT\'', replacement:
303+
options: { replacements: [{ pattern: '\'@@PUBLIC_KEY_CONTENT\'', replacement:
304304
'`' + fs.readFileSync('app/resources/public-key.pem', {encoding: 'utf8'}).trim() + '`' }] },
305305
files: { 'tmp/desktop/app/main.js': 'desktop/main.js' }
306306
},

Diff for: desktop/main.js

+35-24
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,50 @@
88

99
const app = require('electron').app;
1010
const path = require('path');
11-
const fs = require('fs');
11+
const fs = require('original-fs');
1212

1313
const userDataDir = app.getPath('userData');
1414
const userDataAppArchivePath = path.join(userDataDir, 'app.asar');
1515
let entryPointDir = __dirname;
1616

17-
if (fs.existsSync(userDataAppArchivePath)) {
18-
let versionLocal = require('./package.json').version;
17+
try {
18+
const appFilePath = entryPointDir.endsWith('app.asar') ? entryPointDir : __filename;
19+
let userPackageStat;
1920
try {
20-
let versionUserData = require(path.join(userDataAppArchivePath, 'package.json')).version;
21-
versionLocal = versionLocal.split('.');
22-
versionUserData = versionUserData.split('.');
23-
for (let i = 0; i < versionLocal.length; i++) {
24-
if (+versionUserData[i] > +versionLocal[i]) {
25-
entryPointDir = userDataAppArchivePath;
26-
try {
27-
validateSignature(userDataDir);
28-
} catch (e) {
29-
exitWithError('Error validating signatures: ' + e);
21+
userPackageStat = fs.statSync(userDataAppArchivePath);
22+
} catch (e) {}
23+
if (userPackageStat) {
24+
const packageStat = fs.statSync(appFilePath);
25+
const userPackageStatTime = Math.max(userPackageStat.mtime.getTime(), userPackageStat.ctime.getTime());
26+
const packageStatTime = Math.max(packageStat.mtime.getTime(), packageStat.ctime.getTime());
27+
if (userPackageStatTime > packageStatTime) {
28+
let versionLocal = require('./package.json').version;
29+
let versionUserData = require(path.join(userDataAppArchivePath, 'package.json')).version;
30+
versionLocal = versionLocal.split('.');
31+
versionUserData = versionUserData.split('.');
32+
for (let i = 0; i < versionLocal.length; i++) {
33+
if (+versionUserData[i] > +versionLocal[i]) {
34+
entryPointDir = userDataAppArchivePath;
35+
try {
36+
validateSignature(userDataDir);
37+
} catch (e) {
38+
exitWithError('Error validating signatures: ' + e);
39+
}
40+
break;
41+
}
42+
if (+versionUserData[i] < +versionLocal[i]) {
43+
break;
3044
}
31-
break;
32-
}
33-
if (+versionUserData[i] < +versionLocal[i]) {
34-
break;
3545
}
3646
}
37-
} catch (e) {
38-
console.error('Error reading user file version', e); // eslint-disable-line no-console
3947
}
48+
} catch (e) {
49+
console.error('Error reading user file version', e); // eslint-disable-line no-console
4050
}
51+
const entryPointFile = path.join(entryPointDir, 'app.js');
52+
require(entryPointFile);
4153

4254
function validateSignature(appPath) {
43-
const fs = require('original-fs');
4455
const signatures = JSON.parse(fs.readFileSync(path.join(appPath, 'signatures.json')));
4556
const selfSignature = signatures.kwResSelf;
4657
if (!selfSignature || !signatures['app.asar']) {
@@ -58,8 +69,11 @@ function validateSignature(appPath) {
5869

5970
function validateDataSignature(data, signature, name) {
6071
const crypto = require('crypto');
61-
const publicKey = 'PUBLIC_KEY_CONTENT';
6272
const verify = crypto.createVerify('RSA-SHA256');
73+
let publicKey = '@@PUBLIC_KEY_CONTENT';
74+
if (publicKey.startsWith('@@')) {
75+
publicKey = fs.readFileSync('app/resources/public-key.pem', {encoding: 'utf8'}).trim();
76+
}
6377
verify.write(data);
6478
verify.end();
6579
signature = Buffer.from(signature, 'base64');
@@ -72,6 +86,3 @@ function exitWithError(err) {
7286
console.error(err); // eslint-disable-line no-console
7387
process.exit(1);
7488
}
75-
76-
const entryPointFile = path.join(entryPointDir, 'app.js');
77-
require(entryPointFile);

Diff for: release-notes.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Release notes
22
-------------
33
##### v1.7.0 (WIP)
44
`-` fixed color flash on startup
5+
`+` downgrading desktop app
56

67
##### v1.6.1 (2017-12-03)
78
`-` fixed white screen on startup

0 commit comments

Comments
 (0)