Skip to content

Commit 4187c01

Browse files
committed
feat(settings): show error when web crypto api is not available
1 parent aec2cd5 commit 4187c01

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { Stack, Text } from '@mantine/core'
22

3-
export const SwitchLabel = (title: string, desrc: string) => (
3+
export const SwitchLabel = (title: string, desrc: string, error?: null | string) => (
44
<Stack gap={1}>
55
<Text size="md" fw={500}>
66
{title}
77
</Text>
88
<Text size="xs" c="dimmed">
99
{desrc}
1010
</Text>
11+
{error && (
12+
<Text size="xs" c="alert" fw={500}>
13+
{error}
14+
</Text>
15+
)}
1116
</Stack>
1217
)

src/GZCTF/ClientApp/src/locales/en-US/admin.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@
298298
"platform": {
299299
"api_encryption": {
300300
"description": "Encrypt sensitive data in some APIs",
301-
"label": "Enable API Encryption"
301+
"label": "Enable API Encryption",
302+
"not_available": "Web Crypto API is not available"
302303
},
303304
"color": {
304305
"description": "Custom color for the platform",

src/GZCTF/ClientApp/src/locales/zh-CN/admin.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@
298298
"platform": {
299299
"api_encryption": {
300300
"description": "加密部分敏感数据,如用户密码、flag",
301-
"label": "启用 API 数据加密"
301+
"label": "启用 API 数据加密",
302+
"not_available": "当前环境不支持 Web Crypto API"
302303
},
303304
"color": {
304305
"description": "自定义平台颜色,用于生成主题色盘",

src/GZCTF/ClientApp/src/pages/admin/Settings.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { LogoBox } from '@Components/LogoBox'
2727
import { AdminPage } from '@Components/admin/AdminPage'
2828
import { SwitchLabel } from '@Components/admin/SwitchLabel'
2929
import { showErrorNotification } from '@Utils/ApiHelper'
30+
import { isWebCryptoAvailable } from '@Utils/Crypto'
3031
import { IMAGE_MIME_TYPES } from '@Utils/Shared'
3132
import { OnceSWRConfig, useCaptchaConfig, useConfig } from '@Hooks/useConfig'
3233
import api, { AccountPolicy, ConfigEditModel, ContainerPolicy, GlobalConfig } from '@Api'
@@ -95,6 +96,7 @@ const Configs: FC = () => {
9596
}
9697

9798
const colors = color && /^#[0-9A-F]{6}$/i.test(color) ? generateColors(color) : theme.colors.brand
99+
const webCryptoAvailable = isWebCryptoAvailable()
98100

99101
return (
100102
<AdminPage isLoading={!configs}>
@@ -239,10 +241,12 @@ const Configs: FC = () => {
239241
<Grid.Col span={1} className={misc.alignCenter}>
240242
<Switch
241243
checked={globalConfig?.apiEncryption ?? false}
242-
disabled={disabled}
244+
disabled={disabled || !webCryptoAvailable}
245+
readOnly
243246
label={SwitchLabel(
244247
t('admin.content.settings.platform.api_encryption.label'),
245-
t('admin.content.settings.platform.api_encryption.description')
248+
t('admin.content.settings.platform.api_encryption.description'),
249+
webCryptoAvailable ? null : t('admin.content.settings.platform.api_encryption.not_available')
246250
)}
247251
onChange={(e) =>
248252
setGlobalConfig({

src/GZCTF/ClientApp/src/utils/Crypto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async function encryptData(plainTextBytes: Uint8Array, recipientPublicKeyBase64:
6767
return result
6868
}
6969

70-
function isWebCryptoAvailable(): boolean {
70+
export function isWebCryptoAvailable(): boolean {
7171
return typeof crypto !== 'undefined' && typeof crypto.subtle !== 'undefined'
7272
}
7373

0 commit comments

Comments
 (0)