Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #500 from safing/fix/profile-import-crash
Browse files Browse the repository at this point in the history
Fix crash when .Config of an imported profile is null instead of `{}`
  • Loading branch information
dhaavi authored Dec 13, 2023
2 parents 41b7bd8 + e9ede19 commit 6859c19
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ http-redir

# meta
.DS_Store

app-electron/app-window-state.json
14 changes: 14 additions & 0 deletions app-electron/pages/loading.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
} else if (!status.service.running) {
text = '';
showStartButton = true;
} else {
text = 'Unexpected status response from service manager: ' + JSON.stringify(status)
}

if (showStartButton) {
Expand All @@ -163,6 +165,18 @@
function handleError(err) {
const errorBox = document.getElementById('errorbox');
const message = err?.message || err?.stderr || '';

try {
if (typeof message !== 'string') {
message = JSON.stringify(message)
}
} catch(err) {
console.log('JSON', err)
console.log('original-error', message)

message = 'Unexpected error'
}

if (message === '') {
errorBox.classList.add('hidden');
} else {
Expand Down
1 change: 0 additions & 1 deletion app-electron/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export class AppAPI {
console.error(e);
pathOrUrl = await this.createFileURL(pathOrUrl);
}
console.log("opening external: ", pathOrUrl)

await shell.openExternal(pathOrUrl);
}
Expand Down
2 changes: 0 additions & 2 deletions app-electron/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ function getStateDir(): string {
}

async function main() {
console.log(`Portmaster data directory: ${dataDirectory}`);

// we ensure the notifier is running even though we might quit
// immediately because another instance of the app is already
// running. This ensures the user can restart the notifier
Expand Down
13 changes: 0 additions & 13 deletions app-electron/src/notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@ export function startNotifier(): Promise<void> {
cwd: execDir,
})

process.on('close', code => {
console.log('notifier:close', code)
})
process.on('disconnect', () => {
console.log('notifier:disconnect')
})
process.on('error', code => {
console.log('notifier:close', code)
})
process.on('exit', code => {
console.log('notifier:exit', code)
})

process.unref();
resolve();
} catch (err) {
Expand Down
1 change: 0 additions & 1 deletion app-electron/src/sm-systemd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class SystemdServiceManager implements ServiceManager {

private systemctl(cmd: string, unit: string, asRoot = false): Promise<Output> {
const command = `systemctl ${cmd} ${unit} --no-pager`;
console.log(command);
if (asRoot) {
return execAsRoot(command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface AppProfile extends Record {
Fingerprints: Fingerprint[];
Created: number;
LastEdited: number;
Config: ConfigMap;
Config?: ConfigMap;
Description: string;
Warning: string;
WarningLastUpdated: string;
Expand All @@ -75,9 +75,13 @@ export interface AppProfile extends Record {
// flattenProfileConfig returns a flat version of a nested ConfigMap where each property
// can be used as the database key for the associated setting.
export function flattenProfileConfig(
p: ConfigMap,
p?: ConfigMap,
prefix = ''
): FlatConfigObject {
if (p === null || p === undefined) {
return {}
}

let result: FlatConfigObject = {};

Object.keys(p).forEach((key) => {
Expand Down Expand Up @@ -105,9 +109,13 @@ export function flattenProfileConfig(
* @param path The path of the setting separated by foward slashes.
*/
export function getAppSetting<T extends OptionValueType>(
obj: ConfigMap,
obj: ConfigMap | null | undefined,
path: string
): T | null {
if (obj === null || obj === undefined) {
return null
}

const parts = path.split('/');

let iter = obj;
Expand Down
6 changes: 5 additions & 1 deletion modules/portmaster/src/app/pages/app-view/app-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class AppViewComponent implements OnInit, OnDestroy {
private debugAPI: DebugAPI,
private expertiseService: ExpertiseService,
private portapi: PortapiService
) {}
) { }

/**
* @private
Expand All @@ -220,6 +220,10 @@ export class AppViewComponent implements OnInit, OnDestroy {
return;
}

if (!this.appProfile!.Config) {
this.appProfile.Config = {}
}

// If the value has been "reset to global value" we need to
// set the value to "undefined".
if (event.isDefault) {
Expand Down
4 changes: 2 additions & 2 deletions modules/portmaster/src/app/pages/app-view/overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class AppOverviewComponent implements OnInit, OnDestroy {
private dialog: SfngDialogService,
private actionIndicator: ActionIndicatorService,
private router: Router
) {}
) { }

handleProfileClick(profile: LocalAppProfile, event: MouseEvent) {
if (event.shiftKey) {
Expand Down Expand Up @@ -249,7 +249,7 @@ export class AppOverviewComponent implements OnInit, OnDestroy {
const local: LocalAppProfile = {
...profile,
hasConfigChanges:
profile.LastEdited > 0 && Object.keys(profile.Config).length > 0,
profile.LastEdited > 0 && Object.keys(profile.Config || {}).length > 0,
selected:
oldProfiles.get(`${profile.Source}/${profile.ID}`)?.selected ||
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ export class ActionIndicatorService {
*/
getErrorMessage(resp: HttpResponse<ArrayBuffer | string> | HttpErrorResponse | Error): string {
try {
let msg = '';
let body: string | null = null;

if (typeof resp === 'string') {
Expand All @@ -244,24 +243,33 @@ export class ActionIndicatorService {
} else {
body = this.stringifyBody(resp.error);
}
} else {
body = this.stringifyBody(resp.body);

if (!!body) {
body = body[0].toLocaleUpperCase() + body.slice(1)
return body
}
}


if (resp instanceof HttpResponse) {
let msg = '';
const ct = resp.headers.get('content-type') || '';

body = this.stringifyBody(resp.body);

if (/application\/json/.test(ct)) {
if (!!body) {
msg = body;
}
} else if (/text\/plain/.test(ct)) {
msg = body;
}

// Make the first letter uppercase
if (!!msg) {
msg = msg[0].toLocaleUpperCase() + msg.slice(1)
return msg;
}
return msg;
}

console.error(`Unexpected error type`, resp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,11 @@ export class NetqueryHelper {
}

const newProfile = deepClone(profile);

if (newProfile.Config === null || newProfile.Config === undefined) {
newProfile.Config = {}
}

setAppSetting(newProfile.Config, key, rules);

return this.profileService.saveProfile(newProfile)
Expand Down

0 comments on commit 6859c19

Please sign in to comment.