Skip to content

Commit

Permalink
fix(bld): set product_string property in manifest to rename MacOS Hel…
Browse files Browse the repository at this point in the history
…per apps (#1290)

* Replace special and control characters iff options.app.name is a
string

Fixes: #1286
  • Loading branch information
ayushmanchhabra authored Nov 1, 2024
1 parent a173040 commit b1caad7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ nwbuild({

### Chores

- chore: add Linux, MacOS and Windows fixtures
- chore(docs): don't store JSDoc definitions in `typedef`s - get's hard to understand during development.
- chore: annotate file paths as `fs.PathLike` instead of `string`.
- chore(bld): factor out core build step
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"test": "vitest run --coverage",
"test:cov": "vitest --coverage.enabled true",
"demo:bld": "node ./tests/fixtures/demo.js",
"demo:exe": "./tests/fixtures/out/nwapp.app/Contents/MacOS/nwapp",
"demo:cli": "nwbuild --mode=run --version=0.92.0 --flavor=sdk --glob=false --cacheDir=./node_modules/nw ./tests/fixtures/app"
"demo:exe": "./tests/fixtures/out/Demo.app/Contents/MacOS/Demo",
"demo:cli": "nwbuild --mode=run --flavor=sdk --glob=false --cacheDir=./node_modules/nw ./tests/fixtures/app"
},
"devDependencies": {
"@eslint/js": "^9.12.0",
Expand Down
10 changes: 8 additions & 2 deletions src/bld.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ async function bld({
await fs.promises.cp(nwDir, outDir, { recursive: true, verbatimSymlinks: true });

const files = await util.globFiles({ srcDir, glob });
const manifest = await util.getNodeManifest({ srcDir, glob });
let manifest = await util.getNodeManifest({ srcDir, glob });

/* Set `product_string` in manifest for MacOS. This is used in renaming the Helper apps. */
if (platform === 'osx') {
manifest.json.product_string = app.name;
await fs.promises.writeFile(manifest.path, JSON.stringify(manifest.json));
}

if (glob) {
for (let file of files) {
Expand Down Expand Up @@ -169,7 +175,7 @@ async function bld({
typeof managedManifest === 'object' ||
typeof managedManifest === 'string'
) {
await manageManifest({ nwPkg: manifest, managedManifest, outDir, platform });
await manageManifest({ nwPkg: manifest.json, managedManifest, outDir, platform });
}

if (platform === 'linux') {
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ import util from './util.js';
async function nwbuild(options) {
let built;
let releaseInfo = {};
let manifest = {};
let manifest = {
path: '',
json: undefined,
};

try {
// Parse options
options = await util.parse(options, manifest);

manifest = await util.getNodeManifest({ srcDir: options.srcDir, glob: options.glob });
if (typeof manifest?.nwbuild === 'object') {
options = manifest.nwbuild;
if (typeof manifest.json?.nwbuild === 'object') {
options = manifest.json.nwbuild;
}

options = await util.parse(options, manifest);
options = await util.parse(options, manifest.json);

//TODO: impl logging

Expand Down
22 changes: 15 additions & 7 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,30 @@ async function globFiles({
* @param {object} options - node manifest options
* @param {string | string []} options.srcDir - src dir
* @param {boolean} options.glob - glob flag
* @returns {object} - Node manifest
* @returns {Promise.<{path: string, json: object}>} - Node manifest
*/
async function getNodeManifest({
srcDir, glob
}) {
let manifest;
let manifest = {
path: '',
json: undefined,
};
let files;
if (glob) {
files = await globFiles({ srcDir, glob });
for (const file of files) {
if (path.basename(file) === 'package.json' && manifest === undefined) {
manifest = JSON.parse(await fs.promises.readFile(file));
manifest.path = file;
manifest.json = JSON.parse(await fs.promises.readFile(file));
}
}
} else {
manifest = JSON.parse(await fs.promises.readFile(path.resolve(srcDir, 'package.json')));
manifest.path = path.resolve(srcDir, 'package.json');
manifest.json = JSON.parse(await fs.promises.readFile(path.resolve(srcDir, 'package.json')));
}

if (manifest === undefined) {
if (manifest.json === undefined) {
throw new Error('package.json not found in srcDir file glob patterns.');
}

Expand Down Expand Up @@ -220,8 +225,11 @@ export const parse = async (options, pkg) => {

options.app = options.app ?? {};
options.app.name = options.app.name ?? pkg.name;
/* Remove special and control characters from app.name to mitigate potential path traversal. */
options.app.name = options.app.name.replace(/[<>:"/\\|?*\u0000-\u001F]/g, '');
/* Since the `parse` function is called twice, the first time `pkg` is `{}` and `options.app.name` is `undefined`. */
if (options.app.name) {
/* Remove special and control characters from app.name to mitigate potential path traversal. */
options.app.name = options.app.name.replace(/[<>:"/\\|?*\u0000-\u001F]/g, '');
}
options.app.icon = options.app.icon ?? undefined;

// TODO(#737): move this out
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ await nwbuild({
outDir: './tests/fixtures/out',
glob: false,
app: {
name: 'nwapp',
name: 'Demo',
// MacOS options
LSApplicationCategoryType: 'public.app-category.utilities',
CFBundleIdentifier: 'io.nwutils.demo',
Expand Down

0 comments on commit b1caad7

Please sign in to comment.