From 73ed553668f3280fa507f1b2c171ac1194263d8c Mon Sep 17 00:00:00 2001 From: devinxl Date: Tue, 9 Apr 2024 17:35:26 +0800 Subject: [PATCH] fix(dcellar-web-ui): create folder error --- apps/dcellar-web-ui/package.json | 2 +- apps/dcellar-web-ui/src/facade/common.ts | 8 ++++++++ apps/dcellar-web-ui/src/facade/object.ts | 4 ++-- .../object/components/CreateFolderOperation.tsx | 2 +- .../src/pages/api/bill_monthly/[[...slug]].ts | 4 ++-- .../src/pages/api/bill_realtime/[[...slug]].ts | 4 ++-- .../src/pages/api/chart/[[...slug]].ts | 4 ++-- .../src/pages/api/policies/[[...slug]].ts | 4 ++-- .../src/pages/api/total_cost/[[...slug]].ts | 4 ++-- apps/dcellar-web-ui/src/utils/req.ts | 16 ++++++++++------ common/config/rush/pnpm-lock.yaml | 8 ++++---- 11 files changed, 36 insertions(+), 24 deletions(-) diff --git a/apps/dcellar-web-ui/package.json b/apps/dcellar-web-ui/package.json index f503d537..dd05e497 100644 --- a/apps/dcellar-web-ui/package.json +++ b/apps/dcellar-web-ui/package.json @@ -19,7 +19,7 @@ "antd": "5.11.0", "ahooks": "3.7.7", "hash-wasm": "4.10.0", - "@bnb-chain/greenfield-js-sdk": "2.0.0-alpha.5", + "@bnb-chain/greenfield-js-sdk": "2.0.0-alpha.6", "@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.31", "@emotion/react": "^11.10.5", "@emotion/styled": "^11.10.5", diff --git a/apps/dcellar-web-ui/src/facade/common.ts b/apps/dcellar-web-ui/src/facade/common.ts index d79302ff..8aff4a81 100644 --- a/apps/dcellar-web-ui/src/facade/common.ts +++ b/apps/dcellar-web-ui/src/facade/common.ts @@ -18,6 +18,14 @@ import { TempAccountEntity } from '@/store/slices/accounts'; export const resolve = (r: R): [R, null] => [r, null]; +export const resolveSpRequest = (r: SpResponse) => { + console.log('r', r); + if (r.code !== 0) { + return [null, r.message || UNKNOWN_ERROR]; + } + return [r.body, null]; +}; + export type DeliverTxResponse = Awaited>; export const getObjectInfoAndBucketQuota = async ({ diff --git a/apps/dcellar-web-ui/src/facade/object.ts b/apps/dcellar-web-ui/src/facade/object.ts index 0362d9c4..15d46e18 100644 --- a/apps/dcellar-web-ui/src/facade/object.ts +++ b/apps/dcellar-web-ui/src/facade/object.ts @@ -1,6 +1,6 @@ import { GROUP_ID } from '@/constants/legacy'; import { quotaRemains } from '@/facade/bucket'; -import { getObjectInfoAndBucketQuota, resolve } from '@/facade/common'; +import { getObjectInfoAndBucketQuota, resolve, resolveSpRequest } from '@/facade/common'; import { E_NOT_FOUND, E_NO_QUOTA, @@ -692,5 +692,5 @@ export const delegateCreateFolder = async ( ) => { const client = await getClient(); - return client.object.delegateCreateFolder(request, auth).then(resolve, commonFault); + return client.object.delegateCreateFolder(request, auth).then(resolveSpRequest, commonFault); }; diff --git a/apps/dcellar-web-ui/src/modules/object/components/CreateFolderOperation.tsx b/apps/dcellar-web-ui/src/modules/object/components/CreateFolderOperation.tsx index 580702fc..3e195b34 100644 --- a/apps/dcellar-web-ui/src/modules/object/components/CreateFolderOperation.tsx +++ b/apps/dcellar-web-ui/src/modules/object/components/CreateFolderOperation.tsx @@ -330,7 +330,7 @@ export const CreateFolderOperation = memo(function C if (error1) { setLoading(false); dispatch(setSignatureAction({})); - return setFormErrors([error1 || UNKNOWN_ERROR]); + return setFormErrors([error1]); } else { onClose(); setLoading(false); diff --git a/apps/dcellar-web-ui/src/pages/api/bill_monthly/[[...slug]].ts b/apps/dcellar-web-ui/src/pages/api/bill_monthly/[[...slug]].ts index 2291b486..545a7ff1 100644 --- a/apps/dcellar-web-ui/src/pages/api/bill_monthly/[[...slug]].ts +++ b/apps/dcellar-web-ui/src/pages/api/bill_monthly/[[...slug]].ts @@ -1,11 +1,11 @@ import { ALLOWED_DOMAINS, BILLING_API_URL } from '@/base/env'; -import { validateReferer } from '@/utils/req'; +import { isRefererAllowed } from '@/utils/req'; import axios from 'axios'; import { NextApiRequest, NextApiResponse } from 'next'; import qs from 'query-string'; const handler = async (req: NextApiRequest, res: NextApiResponse) => { - if (!validateReferer(req.headers.referer || '', ALLOWED_DOMAINS)) { + if (!isRefererAllowed(req.headers.referer || '', ALLOWED_DOMAINS)) { res.status(403).json({ message: 'Forbidden' }); } const { slug, ...query } = req.query; diff --git a/apps/dcellar-web-ui/src/pages/api/bill_realtime/[[...slug]].ts b/apps/dcellar-web-ui/src/pages/api/bill_realtime/[[...slug]].ts index 5fbd88c3..2d3368f9 100644 --- a/apps/dcellar-web-ui/src/pages/api/bill_realtime/[[...slug]].ts +++ b/apps/dcellar-web-ui/src/pages/api/bill_realtime/[[...slug]].ts @@ -1,11 +1,11 @@ import { ALLOWED_DOMAINS, BILLING_API_URL } from '@/base/env'; -import { validateReferer } from '@/utils/req'; +import { isRefererAllowed } from '@/utils/req'; import axios from 'axios'; import { NextApiRequest, NextApiResponse } from 'next'; import qs from 'query-string'; const handler = async (req: NextApiRequest, res: NextApiResponse) => { - if (!validateReferer(req.headers.referer || '', ALLOWED_DOMAINS)) { + if (!isRefererAllowed(req.headers.referer || '', ALLOWED_DOMAINS)) { res.status(403).json({ message: 'Forbidden' }); } const { slug, ...query } = req.query; diff --git a/apps/dcellar-web-ui/src/pages/api/chart/[[...slug]].ts b/apps/dcellar-web-ui/src/pages/api/chart/[[...slug]].ts index cbc418b6..8f4cdff4 100644 --- a/apps/dcellar-web-ui/src/pages/api/chart/[[...slug]].ts +++ b/apps/dcellar-web-ui/src/pages/api/chart/[[...slug]].ts @@ -2,10 +2,10 @@ import axios from 'axios'; import qs from 'query-string'; import { NextApiRequest, NextApiResponse } from 'next'; import { ALLOWED_DOMAINS, EXPLORER_API_URL } from '@/base/env'; -import { validateReferer } from '@/utils/req'; +import { isRefererAllowed } from '@/utils/req'; const handler = async (req: NextApiRequest, res: NextApiResponse) => { - if (!validateReferer(req.headers.referer || '', ALLOWED_DOMAINS)) { + if (!isRefererAllowed(req.headers.referer || '', ALLOWED_DOMAINS)) { res.status(403).json({ message: 'Forbidden' }); } const { slug, ...query } = req.query; diff --git a/apps/dcellar-web-ui/src/pages/api/policies/[[...slug]].ts b/apps/dcellar-web-ui/src/pages/api/policies/[[...slug]].ts index 192328e7..7e198d37 100644 --- a/apps/dcellar-web-ui/src/pages/api/policies/[[...slug]].ts +++ b/apps/dcellar-web-ui/src/pages/api/policies/[[...slug]].ts @@ -1,10 +1,10 @@ import { ALLOWED_DOMAINS, EXPLORER_API_URL } from '@/base/env'; -import { validateReferer } from '@/utils/req'; +import { isRefererAllowed } from '@/utils/req'; import axios from 'axios'; import { NextApiRequest, NextApiResponse } from 'next'; const handler = async (req: NextApiRequest, res: NextApiResponse) => { - if (!validateReferer(req.headers.referer || '', ALLOWED_DOMAINS)) { + if (!isRefererAllowed(req.headers.referer || '', ALLOWED_DOMAINS)) { res.status(403).json({ message: 'Forbidden' }); } const { slug } = req.query; diff --git a/apps/dcellar-web-ui/src/pages/api/total_cost/[[...slug]].ts b/apps/dcellar-web-ui/src/pages/api/total_cost/[[...slug]].ts index d070de8a..3577ae46 100644 --- a/apps/dcellar-web-ui/src/pages/api/total_cost/[[...slug]].ts +++ b/apps/dcellar-web-ui/src/pages/api/total_cost/[[...slug]].ts @@ -1,10 +1,10 @@ import { ALLOWED_DOMAINS, BILLING_API_URL } from '@/base/env'; -import { validateReferer } from '@/utils/req'; +import { isRefererAllowed } from '@/utils/req'; import axios from 'axios'; import { NextApiRequest, NextApiResponse } from 'next'; const handler = async (req: NextApiRequest, res: NextApiResponse) => { - if (!validateReferer(req.headers.referer || '', ALLOWED_DOMAINS)) { + if (!isRefererAllowed(req.headers.referer || '', ALLOWED_DOMAINS)) { res.status(403).json({ message: 'Forbidden' }); } const { slug } = req.query; diff --git a/apps/dcellar-web-ui/src/utils/req.ts b/apps/dcellar-web-ui/src/utils/req.ts index 3ca5f83b..2e206700 100644 --- a/apps/dcellar-web-ui/src/utils/req.ts +++ b/apps/dcellar-web-ui/src/utils/req.ts @@ -1,11 +1,15 @@ -const localhostDomains = ['localhost', '127.0.0.1', '::1']; +const validLocalhostDomains: readonly string[] = ['localhost', '127.0.0.1', '::1']; -export function validateReferer(referrer: string, allowedDomains: string) { - if (!referrer) { +export function isRefererAllowed(referrerURL: string, allowedDomainList: string): boolean { + if (!referrerURL) { return false; } - const domain = new URL(referrer).hostname; - const domains = allowedDomains.split(',').concat(localhostDomains); - return domains.includes(domain); + const domain = new URL(referrerURL).hostname; + const domains = allowedDomainList + .split(',') + .map((domain) => domain.trim()) + .concat(validLocalhostDomains); + + return domains.some((allowedDomain) => domain.endsWith(allowedDomain)); } diff --git a/common/config/rush/pnpm-lock.yaml b/common/config/rush/pnpm-lock.yaml index 7825a5ee..9a7d2366 100644 --- a/common/config/rush/pnpm-lock.yaml +++ b/common/config/rush/pnpm-lock.yaml @@ -8,7 +8,7 @@ importers: ../../apps/dcellar-web-ui: specifiers: '@bnb-chain/greenfield-cosmos-types': 0.4.0-alpha.31 - '@bnb-chain/greenfield-js-sdk': 2.0.0-alpha.5 + '@bnb-chain/greenfield-js-sdk': 2.0.0-alpha.6 '@commitlint/cli': ^17.4.3 '@commitlint/config-conventional': ^17.4.3 '@emotion/react': ^11.10.5 @@ -75,7 +75,7 @@ importers: wagmi: ~1.4.10 dependencies: '@bnb-chain/greenfield-cosmos-types': 0.4.0-alpha.31 - '@bnb-chain/greenfield-js-sdk': 2.0.0-alpha.5 + '@bnb-chain/greenfield-js-sdk': 2.0.0-alpha.6 '@emotion/react': 11.11.3_mj3jo2baq3jslihcop7oivercy '@emotion/styled': 11.11.0_44o7ug6fvmx5wru7ifqtcwoy2i '@next/bundle-analyzer': 13.5.6 @@ -1497,8 +1497,8 @@ packages: protobufjs: 6.11.4 dev: false - /@bnb-chain/greenfield-js-sdk/2.0.0-alpha.5: - resolution: {integrity: sha512-Lrc9LRLW8XDmvVJbGrOKysdsDOtosXtejO1qTuzut/Zhh8taL3b08/6QRh5JjHvmyWbnIWRNUtiz7BT1l73yDg==} + /@bnb-chain/greenfield-js-sdk/2.0.0-alpha.6: + resolution: {integrity: sha512-tET1UIqqxGptyVBxKPVAbXUY40ul3nZwrcl+IeEziuMM2TdRosG4s5HCGZ792pv5ima50yDmo1Y6dOTa2WkD8Q==} dependencies: '@bnb-chain/greenfield-cosmos-types': 0.4.0-alpha.31 '@cosmjs/proto-signing': 0.32.2