aboutsummaryrefslogtreecommitdiff
path: root/extension/src/features/nip07allow-api.ts
blob: eff4ff8852c1665664e99cbe36ebc289ee97c101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (C) 2023 Vaughn Nugent
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

import { storage, tabs, type Tabs } from "webextension-polyfill";
import { Watchable, useSingleSlotStorage } from "./types";
import { defaultTo, filter, includes, isEqual } from "lodash";
import { BgRuntime, FeatureApi, IFeatureExport, exportForegroundApi, popupAndOptionsOnly } from "./framework";
import { AppSettings } from "./settings";
import { set, get, watchOnce, useToggle } from "@vueuse/core";
import { computed, ref } from "vue";

interface AllowedSites{
    origins: string[];
    enabled: boolean;
}
export interface AllowedOriginStatus{
    readonly allowedOrigins: string[];
    readonly enabled: boolean;
    readonly currentOrigin?: string;
    readonly isAllowed: boolean;
}

export interface InjectAllowlistApi extends FeatureApi, Watchable {
    addOrigin(origin?: string): Promise<void>;
    removeOrigin(origin?: string): Promise<void>;
    getStatus(): Promise<AllowedOriginStatus>;
    enable(value: boolean): Promise<void>;
}

export const useInjectAllowList = (): IFeatureExport<AppSettings, InjectAllowlistApi> => {
    return {
        background: ({ }: BgRuntime<AppSettings>) => {

            const store = useSingleSlotStorage<AllowedSites>(storage.local, 'nip07-allowlist', { origins: [], enabled: true });
            
            //watch current tab
            const allowedOrigins = ref<string[]>([])
            const protectionEnabled = ref<boolean>(true)
            const [manullyTriggered, trigger] = useToggle()

            const { currentOrigin, currentTab } = (() => {

                const currentTab = ref<Tabs.Tab | undefined>(undefined)
                const currentOrigin = computed(() => currentTab.value?.url ? new URL(currentTab.value.url).origin : undefined)

                //Watch for changes to the current tab
                tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
                    //If the url changed, update the current tab
                    if (changeInfo.url) {
                        currentTab.value = tab
                    }
                })

                tabs.onActivated.addListener(async ({ tabId }) => {
                    //Get the tab
                    const tab = await tabs.get(tabId)
                    //Update the current tab
                    currentTab.value = tab
                })
                return { currentTab, currentOrigin }
            })()

            const writeChanges = async () => {
                await store.set({ origins: [...get(allowedOrigins)], enabled: get(protectionEnabled) })
            }

            //Initial load
            store.get().then((data) => {
                allowedOrigins.value = data.origins
                protectionEnabled.value = data.enabled
            })

            const isOriginAllowed = (origin?: string): boolean => {
                //If protection is not enabled, allow all
                if(protectionEnabled.value == false){
                    return true;
                }
                //if no origin specified, use current origin
                origin = defaultTo(origin, currentOrigin.value)

                //If no origin, return false
                if (!origin) {
                    return false;
                }

                //Default to origin only
                const originOnly = new URL(origin).origin
                return includes(allowedOrigins.value, originOnly)
            }

            const addOrigin = async (origin?: string): Promise<void> => {
                //if no origin specified, use current origin
                const newOrigin = defaultTo(origin, currentOrigin.value)
                if (!newOrigin) {
                    return;
                }

                const originOnly = new URL(newOrigin).origin

                //See if origin is already in the list
                if (!includes(allowedOrigins.value, originOnly)) {
                    //Add to the list
                    allowedOrigins.value.push(originOnly);
                    trigger();

                    //Save changes
                    await writeChanges()

                    //If current tab was added, reload the tab
                    if (!origin) {
                        await tabs.reload(currentTab.value?.id)
                    }
                }
            }

            const removeOrigin = async (origin?: string): Promise<void> => {
                //Allow undefined to remove current origin
                const delOrigin = defaultTo(origin, currentOrigin.value)
                if (!delOrigin) {
                    return;
                }

                //Get origin part of url
                const delOriginOnly = new URL(delOrigin).origin
                const allowList = get(allowedOrigins)

                //Remove the origin
                allowedOrigins.value = filter(allowList, (o) => !isEqual(o, delOriginOnly));
                trigger();

                await writeChanges()

                //If current tab was removed, reload the tab
                if (!origin) {
                    await tabs.reload(currentTab.value?.id)
                }
            }
           

            return {
                addOrigin: popupAndOptionsOnly(addOrigin),
                removeOrigin: popupAndOptionsOnly(removeOrigin),
                enable: popupAndOptionsOnly(async (value: boolean): Promise<void> => {
                    set(protectionEnabled, value)
                    await writeChanges()
                }),
                async getStatus(): Promise<AllowedOriginStatus> {
                    return{
                        allowedOrigins: [...get(allowedOrigins)],
                        enabled: get(protectionEnabled),
                        currentOrigin: get(currentOrigin),
                        isAllowed: isOriginAllowed()
                    }
                },
                waitForChange:  () => {
                    //Wait for the trigger to change
                    return new Promise((resolve) => watchOnce([currentTab, protectionEnabled, manullyTriggered] as any, () => resolve()));
                },
            }
        },
        foreground: exportForegroundApi([
            'addOrigin',
            'removeOrigin',
            'getStatus',
            'enable',
            'waitForChange'
        ])
    }
}