Skip to content

Commit 268528b

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 268528b

3 files changed

Lines changed: 181 additions & 0 deletions

File tree

src/components/CorsConfigModal.vue

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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, type CorsConfig } 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+
in_file: [] as string[],
63+
needs_restart: false,
64+
} as CorsConfig,
65+
corsStr: '',
66+
corsRegexStr: '',
67+
corsStore: useCorsStore(),
68+
};
69+
},
70+
computed: {
71+
...mapState(useCorsStore, ['config', 'loading', 'error']),
72+
},
73+
watch: {
74+
config(newVal) {
75+
if (newVal) {
76+
this.editable = JSON.parse(JSON.stringify(newVal));
77+
this.corsStr = newVal.cors.join(', ');
78+
this.corsRegexStr = newVal.cors_regex.join(', ');
79+
}
80+
},
81+
},
82+
methods: {
83+
isFixed(field: string): boolean {
84+
return this.config?.in_file?.includes(field) || false;
85+
},
86+
async load() {
87+
await this.corsStore.load();
88+
},
89+
async save(bvModalEvt: any) {
90+
bvModalEvt.preventDefault();
91+
92+
// Parse comma-separated strings back to arrays
93+
this.editable.cors = this.corsStr.split(',').map(s => s.trim()).filter(s => s !== '');
94+
this.editable.cors_regex = this.corsRegexStr.split(',').map(s => s.trim()).filter(s => s !== '');
95+
96+
try {
97+
await this.corsStore.save(this.editable);
98+
(this as any).$bvModal.hide('cors-config-modal');
99+
alert('CORS configuration saved! Please restart the server to apply changes.');
100+
} catch (e: any) {
101+
const msg = e.response?.data?.message || e.message || 'Unknown error';
102+
alert('Failed to save: ' + msg);
103+
}
104+
},
105+
},
106+
};
107+
</script>

src/stores/cors.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
export type MutableCorsConfig = Pick<CorsConfig, 'cors' | 'cors_regex' | 'cors_allow_aw_chrome_extension' | 'cors_allow_all_mozilla_extension'>;
14+
15+
interface State {
16+
config: CorsConfig | null;
17+
loading: boolean;
18+
error: string | null;
19+
}
20+
21+
export const useCorsStore = defineStore('cors', {
22+
state: (): State => ({
23+
config: null,
24+
loading: false,
25+
error: null,
26+
}),
27+
actions: {
28+
async load() {
29+
this.loading = true;
30+
this.error = null;
31+
try {
32+
const client = getClient();
33+
const response = await client.req.get('/0/cors-config');
34+
this.config = response.data;
35+
} catch (e: any) {
36+
this.error = e.response?.data?.message || e.message || 'Failed to load CORS config';
37+
} finally {
38+
this.loading = false;
39+
}
40+
},
41+
async save(newConfig: MutableCorsConfig) {
42+
this.loading = true;
43+
this.error = null;
44+
try {
45+
const client = getClient();
46+
// Only send the mutable subset to the server
47+
const payload: MutableCorsConfig = {
48+
cors: newConfig.cors,
49+
cors_regex: newConfig.cors_regex,
50+
cors_allow_aw_chrome_extension: newConfig.cors_allow_aw_chrome_extension,
51+
cors_allow_all_mozilla_extension: newConfig.cors_allow_all_mozilla_extension,
52+
};
53+
await client.req.post('/0/cors-config', payload);
54+
55+
// Update local state if successful
56+
if (this.config) {
57+
this.config = { ...this.config, ...payload };
58+
}
59+
} catch (e: any) {
60+
this.error = e.response?.data?.message || e.message || 'Failed to save CORS config';
61+
throw e;
62+
} finally {
63+
this.loading = false;
64+
}
65+
}
66+
}
67+
});

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)