Skip to content

Commit 5279a87

Browse files
committed
[SEC] restrict CORS to authorized extension IDs
Fixes a security issue where any Firefox extension (moz-extension://.*) could access the ActivityWatch server without any restriction. Previously, the CORS configuration included a wildcard for all Mozilla extensions by default. This commit removes that blanket permission and introduces granular control through both static configuration and the Web UI. We've added 2 new fields to the file configuration (allow_aw_chrome_extension and allow_all_mozilla_extension) and 4 new settings to the Web UI (Fixed origins, Regex origins, and extension-specific shortcuts). The server now merges these settings to determine the final set of authorized origins, ensuring a more secure and flexible configuration. Dependent on: ActivityWatch/aw-server-rust#581 edited according to the last changes
1 parent cd5aafe commit 5279a87

3 files changed

Lines changed: 165 additions & 0 deletions

File tree

src/components/CorsConfigModal.vue

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<template lang="pug">
2+
b-modal(id="cors-config-modal" title="Security & CORS" @show="load" @ok="save" :ok-disabled="loading || !config" size="lg")
3+
div(v-if="config")
4+
b-alert(v-if="config.needs_restart" show variant="warning" class="mb-4")
5+
h5.alert-heading ⚠️ Server Restart Required
6+
p.mb-0
7+
| CORS settings are only applied once at startup. You must <b>stop and restart the server</b> for any changes made here to take effect.
8+
9+
b-form-group(label="Fixed CORS origins" label-cols-md=4 description="Configure general CORS origins with exact matches (e.g. http://localhost:8080). Comma-separated.")
10+
b-input(v-model="corsStr" type="text" :disabled="isFixed('cors')")
11+
small.text-warning(v-if="isFixed('cors')")
12+
| ⚠️ Fixed in <code>config.toml</code>. Settings in the configuration file take precedence and cannot be changed here.
13+
14+
b-form-group(label="Regex CORS origins" label-cols-md=4 description="Configure CORS origins with regular expressions. Useful for browser extensions (e.g. chrome-extension://.* or moz-extension://.*). Comma-separated.")
15+
b-input(v-model="corsRegexStr" type="text" :disabled="isFixed('cors_regex')")
16+
small.text-warning(v-if="isFixed('cors_regex')")
17+
| ⚠️ Fixed in <code>config.toml</code>. Settings in the configuration file take precedence and cannot be changed here.
18+
19+
h5.mt-4 Extensions Shortcuts
20+
b-form-group(label-cols-md=4)
21+
b-form-checkbox(v-model="editable.cors_allow_aw_chrome_extension" :disabled="isFixed('cors_allow_aw_chrome_extension')") Allow ActivityWatch extension (Chrome)
22+
template(#description)
23+
div Chrome extensions use a stable, persistent ID, so the official extension is reliably supported.
24+
small.text-warning(v-if="isFixed('cors_allow_aw_chrome_extension')")
25+
| ⚠️ Fixed in <code>config.toml</code>. Settings in the configuration file take precedence and cannot be changed here.
26+
27+
b-form-group(label-cols-md=4)
28+
b-form-checkbox(v-model="editable.cors_allow_all_mozilla_extension" :disabled="isFixed('cors_allow_all_mozilla_extension')") Allow all Firefox extensions (DANGEROUS)
29+
template(#description)
30+
div Every version of a Mozilla extension has its own ID to avoid fingerprinting. This is why you must either allow all extensions or manually configure your specific ID.
31+
small.text-warning.mb-2.d-block(v-if="isFixed('cors_allow_all_mozilla_extension')")
32+
| ⚠️ Fixed in <code>config.toml</code>. Settings in the configuration file take precedence and cannot be changed here.
33+
div.mt-2.text-danger(v-if="editable.cors_allow_all_mozilla_extension")
34+
| ⚠️ DANGEROUS: Not recommended for security. If enabled, any installed extension can access your ActivityWatch data. Use this only if you know what extensions you have and assume full responsibility.
35+
div(v-else)
36+
| Recommended for security. To allow a specific extension safely:
37+
ol.mt-2.mb-1
38+
li Go to <code>about:debugging#/runtime/this-firefox</code> in your browser.
39+
li Look for your extension and copy the <b>Manifest URL</b> (e.g. <code>moz-extension://4b931c07dededdedff152/manifest.json</code>).
40+
li Remove <code>manifest.json</code> from the end (to get <code>moz-extension://4b931c07dededdedff152</code>).
41+
li Paste it into the <b>Regex CORS origins</b> field above (use a comma to separate if not empty).
42+
43+
div(v-else-if="loading")
44+
p Loading...
45+
div(v-else-if="error")
46+
b-alert(show variant="danger") Failed to load CORS configuration: {{ error }}
47+
</template>
48+
49+
<script lang="ts">
50+
import { useCorsStore } from '~/stores/cors';
51+
import { mapState } from 'pinia';
52+
53+
export default {
54+
name: 'CorsConfigModal',
55+
data() {
56+
return {
57+
editable: {
58+
cors: [] as string[],
59+
cors_regex: [] as string[],
60+
cors_allow_aw_chrome_extension: false,
61+
cors_allow_all_mozilla_extension: false,
62+
},
63+
corsStr: '',
64+
corsRegexStr: '',
65+
corsStore: useCorsStore(),
66+
};
67+
},
68+
computed: {
69+
...mapState(useCorsStore, ['config', 'loading', 'error']),
70+
},
71+
watch: {
72+
config(newVal) {
73+
if (newVal) {
74+
this.editable = JSON.parse(JSON.stringify(newVal));
75+
this.corsStr = newVal.cors.join(', ');
76+
this.corsRegexStr = newVal.cors_regex.join(', ');
77+
}
78+
},
79+
},
80+
methods: {
81+
isFixed(field: string): boolean {
82+
return this.config?.in_file?.includes(field) || false;
83+
},
84+
async load() {
85+
await this.corsStore.load();
86+
},
87+
async save(bvModalEvt: any) {
88+
bvModalEvt.preventDefault();
89+
90+
// Parse comma-separated strings back to arrays
91+
this.editable.cors = this.corsStr.split(',').map(s => s.trim()).filter(s => s !== '');
92+
this.editable.cors_regex = this.corsRegexStr.split(',').map(s => s.trim()).filter(s => s !== '');
93+
94+
try {
95+
await this.corsStore.save(this.editable);
96+
(this as any).$bvModal.hide('cors-config-modal');
97+
alert('CORS configuration saved! Please restart the server to apply changes.');
98+
} catch (e: any) {
99+
alert('Failed to save: ' + e.message);
100+
}
101+
},
102+
},
103+
};
104+
</script>

src/stores/cors.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { defineStore } from 'pinia';
2+
import { getClient } from '~/util/awclient';
3+
4+
export interface CorsConfig {
5+
cors: string[];
6+
cors_regex: string[];
7+
cors_allow_aw_chrome_extension: boolean;
8+
cors_allow_all_mozilla_extension: boolean;
9+
in_file: string[];
10+
needs_restart: boolean;
11+
}
12+
13+
interface State {
14+
config: CorsConfig | null;
15+
loading: boolean;
16+
error: string | null;
17+
}
18+
19+
export const useCorsStore = defineStore('cors', {
20+
state: (): State => ({
21+
config: null,
22+
loading: false,
23+
error: null,
24+
}),
25+
actions: {
26+
async load() {
27+
this.loading = true;
28+
this.error = null;
29+
try {
30+
const client = getClient();
31+
const response = await client.req.get('/0/cors-config');
32+
this.config = response.data;
33+
} catch (e: any) {
34+
this.error = e.message || 'Failed to load CORS config';
35+
} finally {
36+
this.loading = false;
37+
}
38+
},
39+
async save(newConfig: CorsConfig) {
40+
this.loading = true;
41+
this.error = null;
42+
try {
43+
const client = getClient();
44+
await client.req.post('/0/cors-config', newConfig);
45+
this.config = newConfig;
46+
} catch (e: any) {
47+
this.error = e.message || 'Failed to save CORS config';
48+
throw e;
49+
} finally {
50+
this.loading = false;
51+
}
52+
}
53+
}
54+
});

src/views/settings/Settings.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ div
3737
hr
3838

3939
DeveloperSettings
40+
div.mt-2
41+
b-btn(v-b-modal.cors-config-modal, variant="outline-primary", size="sm")
42+
| Configure CORS
43+
44+
CorsConfigModal
4045
</template>
4146

4247
<script lang="ts">
@@ -49,6 +54,7 @@ import ReleaseNotificationSettings from '~/views/settings/ReleaseNotificationSet
4954
import CategorizationSettings from '~/views/settings/CategorizationSettings.vue';
5055
import LandingPageSettings from '~/views/settings/LandingPageSettings.vue';
5156
import DeveloperSettings from '~/views/settings/DeveloperSettings.vue';
57+
import CorsConfigModal from '~/components/CorsConfigModal.vue';
5258
import Theme from '~/views/settings/Theme.vue';
5359
import ColorSettings from '~/views/settings/ColorSettings.vue';
5460
import ActivePatternSettings from '~/views/settings/ActivePatternSettings.vue';
@@ -65,6 +71,7 @@ export default {
6571
ColorSettings,
6672
DeveloperSettings,
6773
ActivePatternSettings,
74+
CorsConfigModal,
6875
},
6976
beforeRouteLeave(to, from, next) {
7077
const categoryStore = useCategoryStore();

0 commit comments

Comments
 (0)