Add allowed var + fix default whitelist

This commit is contained in:
mrjvs
2024-01-10 22:11:19 +01:00
parent 136020f741
commit db9f1eb926
2 changed files with 11 additions and 6 deletions

View File

@@ -3,20 +3,21 @@ import type { PlasmoMessaging } from '@plasmohq/messaging';
import { getVersion } from '~hooks/useVersion'; import { getVersion } from '~hooks/useVersion';
import type { BaseRequest } from '~types/request'; import type { BaseRequest } from '~types/request';
import type { BaseResponse } from '~types/response'; import type { BaseResponse } from '~types/response';
import { assertDomainWhitelist } from '~utils/storage'; import { isDomainWhitelisted } from '~utils/storage';
type Response = BaseResponse<{ type Response = BaseResponse<{
version: string; version: string;
allowed: boolean;
}>; }>;
const handler: PlasmoMessaging.MessageHandler<BaseRequest, Response> = async (req, res) => { const handler: PlasmoMessaging.MessageHandler<BaseRequest, Response> = async (req, res) => {
try { try {
await assertDomainWhitelist(req.body.requestDomain);
const version = getVersion(); const version = getVersion();
res.send({ res.send({
success: true, success: true,
version, version,
allowed: await isDomainWhitelisted(req.body.requestDomain),
}); });
} catch (err) { } catch (err) {
res.send({ res.send({

View File

@@ -3,7 +3,7 @@ import { useStorage } from '@plasmohq/storage/hook';
import { makeUrlIntoDomain } from '~utils/domains'; 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(); export const storage = new Storage();
@@ -16,9 +16,13 @@ export function useDomainStorage() {
return useStorage<string[]>('domainWhitelist', (v) => v ?? DEFAULT_DOMAIN_WHITELIST); 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); const domain = makeUrlIntoDomain(url);
if (!domain) throw new Error('Domain is from a normal tab'); if (!domain) return false;
const isWhiteListed = await domainIsInWhitelist(domain); return domainIsInWhitelist(domain);
};
export const assertDomainWhitelist = async (url: string) => {
const isWhiteListed = await isDomainWhitelisted(url);
if (!isWhiteListed) throw new Error('Domain is not whitelisted'); if (!isWhiteListed) throw new Error('Domain is not whitelisted');
}; };