Skip to content

Commit a7911c6

Browse files
authored
fix: serve exported desktop routes through a local app protocol (#396)
1 parent 2968e96 commit a7911c6

4 files changed

Lines changed: 123 additions & 12 deletions

File tree

‎desktopPaths.js‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
const fs = require('fs');
19+
const path = require('path');
20+
21+
const getExportedFile = (url, exportRoot) => {
22+
let request;
23+
let pathname;
24+
try {
25+
request = new URL(url);
26+
pathname = decodeURIComponent(request.pathname);
27+
} catch {
28+
return null;
29+
}
30+
if (request.protocol !== 'app:' || request.host !== 'editor' || request.username || request.password) {
31+
return null;
32+
}
33+
34+
if (pathname.includes('\\') || pathname.includes('\0') || pathname.split('/').includes('..')) {
35+
return null;
36+
}
37+
38+
const file = path.resolve(exportRoot, `.${pathname}`);
39+
const relative = path.relative(exportRoot, file);
40+
if (relative.startsWith('..') || path.isAbsolute(relative)) {
41+
return null;
42+
}
43+
44+
return (
45+
[file, `${file}.html`, path.join(file, 'index.html')].find((candidate) => {
46+
return fs.existsSync(candidate) && fs.statSync(candidate).isFile();
47+
}) || null
48+
);
49+
};
50+
51+
module.exports = { getExportedFile };

‎main.js‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
const { app, BrowserWindow, protocol } = require('electron');
18+
const { app, BrowserWindow, protocol, net } = require('electron');
1919
const path = require('path');
20+
const { pathToFileURL } = require('url');
21+
const { getExportedFile } = require('./desktopPaths');
2022

21-
protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true, standard: true } }]);
23+
protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true, standard: true, supportFetchAPI: true } }]);
2224

2325
const createWindow = () => {
2426
const win = new BrowserWindow({
@@ -29,22 +31,20 @@ const createWindow = () => {
2931
},
3032
});
3133

32-
win.loadFile(path.join(__dirname, 'out/index.html')).catch((err) => {
34+
win.loadURL('app://editor/').catch((err) => {
3335
console.error('Failed to load local file:', err);
3436
win.loadURL('https://editor.casbin.org/');
3537
});
36-
37-
win.webContents.on('did-fail-load', async () => {
38-
try {
39-
await win.loadFile(path.join(__dirname, 'out/index.html'));
40-
} catch (err) {
41-
console.error('Retry loading local file failed:', err);
42-
win.loadURL('https://editor.casbin.org/');
43-
}
44-
});
4538
};
4639

4740
app.whenReady().then(() => {
41+
protocol.handle('app', (request) => {
42+
const file = getExportedFile(request.url, path.join(__dirname, 'out'));
43+
if (!file) {
44+
return new Response('Not found', { status: 404 });
45+
}
46+
return net.fetch(pathToFileURL(file).toString());
47+
});
4848
createWindow();
4949
});
5050

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"files": [
3333
"main.js",
34+
"desktopPaths.js",
3435
"preload.js",
3536
"out/**/*",
3637
"!node_modules/**/*",

‎test/desktopPaths.test.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
const fs = require('fs');
19+
const os = require('os');
20+
const path = require('path');
21+
const { getExportedFile } = require('../desktopPaths');
22+
23+
describe('desktop static export paths', () => {
24+
const exportRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'casbin-editor-paths-'));
25+
26+
beforeAll(() => {
27+
fs.mkdirSync(path.join(exportRoot, '_next'));
28+
fs.writeFileSync(path.join(exportRoot, 'index.html'), 'editor');
29+
fs.writeFileSync(path.join(exportRoot, 'gallery.html'), 'gallery');
30+
fs.writeFileSync(path.join(exportRoot, 'github.svg'), '<svg/>');
31+
fs.writeFileSync(path.join(exportRoot, '_next', 'chunk.js'), 'chunk');
32+
});
33+
34+
afterAll(() => {
35+
fs.rmSync(exportRoot, { recursive: true });
36+
});
37+
38+
test.each([
39+
['app://editor/', 'index.html'],
40+
['app://editor/gallery', 'gallery.html'],
41+
['app://editor/github.svg', 'github.svg'],
42+
['app://editor/_next/chunk.js?v=1', '_next/chunk.js'],
43+
])('serves %s from the exported bundle', (url, file) => {
44+
expect(getExportedFile(url, exportRoot)).toBe(path.join(exportRoot, file));
45+
});
46+
47+
test.each([
48+
'https://editor/github.svg',
49+
'app://other/github.svg',
50+
'app://editor/%2e%2e%2foutside.txt',
51+
'app://editor/%5coutside.txt',
52+
'app://editor/%00outside.txt',
53+
'app://editor/%E0%A4',
54+
'invalid url',
55+
'app://editor/missing.js',
56+
])('does not serve %s', (url) => {
57+
expect(getExportedFile(url, exportRoot)).toBeNull();
58+
});
59+
});

0 commit comments

Comments
 (0)