mirror of
https://github.com/movie-web/extension.git
synced 2025-09-13 11:33:25 +00:00
Add allowed var + fix default whitelist
This commit is contained in:
@@ -3,20 +3,21 @@ import type { PlasmoMessaging } from '@plasmohq/messaging';
|
||||
import { getVersion } from '~hooks/useVersion';
|
||||
import type { BaseRequest } from '~types/request';
|
||||
import type { BaseResponse } from '~types/response';
|
||||
import { assertDomainWhitelist } from '~utils/storage';
|
||||
import { isDomainWhitelisted } from '~utils/storage';
|
||||
|
||||
type Response = BaseResponse<{
|
||||
version: string;
|
||||
allowed: boolean;
|
||||
}>;
|
||||
|
||||
const handler: PlasmoMessaging.MessageHandler<BaseRequest, Response> = async (req, res) => {
|
||||
try {
|
||||
await assertDomainWhitelist(req.body.requestDomain);
|
||||
const version = getVersion();
|
||||
|
||||
res.send({
|
||||
success: true,
|
||||
version,
|
||||
allowed: await isDomainWhitelisted(req.body.requestDomain),
|
||||
});
|
||||
} catch (err) {
|
||||
res.send({
|
||||
|
@@ -3,7 +3,7 @@ import { useStorage } from '@plasmohq/storage/hook';
|
||||
|
||||
import { makeUrlIntoDomain } from '~utils/domains';
|
||||
|
||||
export const DEFAULT_DOMAIN_WHITELIST = ['https://movie-web.app', 'http://localhost:5173'];
|
||||
export const DEFAULT_DOMAIN_WHITELIST = ['movie-web.app', 'localhost:5173'];
|
||||
|
||||
export const storage = new Storage();
|
||||
|
||||
@@ -16,9 +16,13 @@ export function useDomainStorage() {
|
||||
return useStorage<string[]>('domainWhitelist', (v) => v ?? DEFAULT_DOMAIN_WHITELIST);
|
||||
}
|
||||
|
||||
export const assertDomainWhitelist = async (url: string) => {
|
||||
export const isDomainWhitelisted = async (url: string) => {
|
||||
const domain = makeUrlIntoDomain(url);
|
||||
if (!domain) throw new Error('Domain is from a normal tab');
|
||||
const isWhiteListed = await domainIsInWhitelist(domain);
|
||||
if (!domain) return false;
|
||||
return domainIsInWhitelist(domain);
|
||||
};
|
||||
|
||||
export const assertDomainWhitelist = async (url: string) => {
|
||||
const isWhiteListed = await isDomainWhitelisted(url);
|
||||
if (!isWhiteListed) throw new Error('Domain is not whitelisted');
|
||||
};
|
||||
|
Reference in New Issue
Block a user