Skip to content

Commit 646425a

Browse files
jonassorgenfreishubham-padia
authored andcommitted
link_utils: exposing whitelisted protocols to user settings
Fixes #1284. Adding config option to set protocols in the config that are whitelisted to be opened directly. The behaviour is documented in docs\howto\customize-link-protocols.md.
1 parent 096652b commit 646425a

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

app/common/config-schemata.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const configSchemata = {
3636
useManualProxy: z.boolean(),
3737
useProxy: z.boolean(),
3838
useSystemProxy: z.boolean(),
39+
whitelistedProtocols: z.string().array(),
3940
};
4041
export type ConfigSchemata = typeof configSchemata;
4142

app/common/config-util.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ export function getConfigItem<Key extends keyof Config>(
4949
}
5050
}
5151

52+
// The function getConfigItem stores the default in settings.json which means that
53+
// if the defaults change in the future, that change will not be propagated
54+
// to anyone having a pre-installed desktop app. We should phase out getConfigItem
55+
// in favour of this function and rename this to getConfigItem when it is done
56+
// for all existing occurences.
57+
export function getConfigItemWithoutSettingDefault<Key extends keyof Config>(
58+
key: Key,
59+
defaultValue: Config[Key],
60+
): z.output<ConfigSchemata[Key]> {
61+
if (isConfigItemExists(key)) {
62+
const typedSchemata: {
63+
[Key in keyof Config]: z.ZodType<
64+
z.output<ConfigSchemata[Key]>,
65+
z.input<ConfigSchemata[Key]>
66+
>;
67+
} = configSchemata; // https://github.com/colinhacks/zod/issues/5154
68+
return typedSchemata[key].parse(database.getObject<unknown>(`/${key}`));
69+
}
70+
71+
return defaultValue;
72+
}
73+
5274
// This function returns whether a key exists in the configuration file (settings.json)
5375
export function isConfigItemExists(key: string): boolean {
5476
try {
@@ -86,6 +108,7 @@ function reloadDatabase(): void {
86108
app.getPath("userData"),
87109
"/config/settings.json",
88110
);
111+
console.log(settingsJsonPath);
89112
try {
90113
const file = fs.readFileSync(settingsJsonPath, "utf8");
91114
JSON.parse(file);

app/common/link-util.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import fs from "node:fs";
33
import os from "node:os";
44
import path from "node:path";
55

6+
import * as ConfigUtil from "./config-util.ts";
67
import {Html, html} from "./html.ts";
78
import * as t from "./translation-util.ts";
89

10+
const whitelistedProtocols = ConfigUtil.getConfigItemWithoutSettingDefault(
11+
"whitelistedProtocols",
12+
["http:", "https:", "mailto:", "tel:", "sip:"],
13+
);
14+
915
export async function openBrowser(url: URL): Promise<void> {
10-
if (["http:", "https:", "mailto:"].includes(url.protocol)) {
16+
if (whitelistedProtocols.includes(url.protocol)) {
1117
await shell.openExternal(url.href);
1218
} else {
1319
// For security, indirect links to non-whitelisted protocols
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Customizing link protocols
2+
3+
The Zulip app supports opening certain link protocols directly in their associated system applications. These are known as **whitelisted protocols**.
4+
5+
## Default whitelisted protocols
6+
7+
By default, the following protocols are whitelisted:
8+
9+
```
10+
http, https, mailto, tel, sip
11+
```
12+
13+
Links using these protocols are opened directly by the system.
14+
15+
All other protocols are considered potentially unsafe and are therefore opened indirectly—through a local HTML file in your default web browser.
16+
17+
## Extending the whitelisted protocols
18+
19+
It is possible to customize the list of whitelisted protocols by editing the `settings.json` file located at: `userdata/Zulip/config/settings.json`
20+
21+
To modify the list, the `whitelistedProtocols` key can be updated. For example:
22+
23+
```json
24+
{
25+
...
26+
"whitelistedProtocols": [
27+
"http:",
28+
"https:",
29+
"mailto:"
30+
]
31+
...
32+
}
33+
```
34+
35+
Note: Each protocol should include the trailing (:), e.g., "mailto:" instead of "mailto".

0 commit comments

Comments
 (0)