A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
fix(guard.isIgnoredPath): Avoid incorrect matches when using `indexOf`
haitaoo378b6e6b2d86
diff --git a/ui/src/pages/UserCenter/Auth/components/WeCom/index.tsx b/ui/src/pages/UserCenter/Auth/components/WeCom/index.tsxindex 21970f55..70e4a018 100644--- a/ui/src/pages/UserCenter/Auth/components/WeCom/index.tsx+++ b/ui/src/pages/UserCenter/Auth/components/WeCom/index.tsx@@ -65,12 +65,13 @@ const Index: FC = () => {clearTimeout(checkTimer);};}, []);+if (getUaType() !== USER_AGENT_NAMES.WeCom) {return (<Card className="text-center"><Card.Body><Card.Title as="h3" className="mb-3">- {ucAgent?.agent_info.display_name} {t('login')}+ {ucAgent?.agent_info?.display_name} {t('login')}</Card.Title>{qrcodeDataUrl ? (<>@@ -82,7 +83,7 @@ const Index: FC = () => {/><div className="text-secondary mt-3">{t('qrcode_login_tip', {- agentName: ucAgent?.agent_info.display_name,+ agentName: ucAgent?.agent_info?.display_name,})}</div></>diff --git a/ui/src/pages/UserCenter/Auth/index.tsx b/ui/src/pages/UserCenter/Auth/index.tsxindex 9387daea..f7a4f86c 100644--- a/ui/src/pages/UserCenter/Auth/index.tsx+++ b/ui/src/pages/UserCenter/Auth/index.tsx@@ -13,7 +13,7 @@ const Index = () => {const { t } = useTranslation('translation');const [searchParam] = useSearchParams();const { agent: ucAgent } = userCenterStore();- let agentName = ucAgent?.agent_info.name || '';+ let agentName = ucAgent?.agent_info?.name || '';if (searchParam.get('agent_name')) {agentName = searchParam.get('agent_name') || '';}diff --git a/ui/src/pages/UserCenter/AuthFailed/components/WeCom.tsx b/ui/src/pages/UserCenter/AuthFailed/components/WeCom.tsxindex c277a558..b69561b4 100644--- a/ui/src/pages/UserCenter/AuthFailed/components/WeCom.tsx+++ b/ui/src/pages/UserCenter/AuthFailed/components/WeCom.tsx@@ -35,7 +35,7 @@ const Index: FC = () => {<Card><Card.Body><h3 className="text-center pt-3 mb-3">- {ucAgent?.agent_info.display_name} {t('login')}+ {ucAgent?.agent_info?.display_name} {t('login')}</h3><p className="text-danger text-center">{t('login_failed_email_tip')}diff --git a/ui/src/pages/UserCenter/AuthFailed/index.tsx b/ui/src/pages/UserCenter/AuthFailed/index.tsxindex 05c9c78d..9ab9cc72 100644--- a/ui/src/pages/UserCenter/AuthFailed/index.tsx+++ b/ui/src/pages/UserCenter/AuthFailed/index.tsx@@ -13,7 +13,7 @@ const Index = () => {const { t } = useTranslation('translation');const [searchParam] = useSearchParams();const { agent: ucAgent } = userCenterStore();- let agentName = ucAgent?.agent_info.name || '';+ let agentName = ucAgent?.agent_info?.name || '';if (searchParam.get('agent_name')) {agentName = searchParam.get('agent_name') || '';}diff --git a/ui/src/utils/floppyNavigation.ts b/ui/src/utils/floppyNavigation.tsindex 6cdb1544..7ed3a77b 100644--- a/ui/src/utils/floppyNavigation.ts+++ b/ui/src/utils/floppyNavigation.ts@@ -10,6 +10,47 @@ const equalToCurrentHref = (target: string, base?: string) => {const targetUrl = new URL(target, base);return targetUrl.toString() === window.location.href;};+const matchToCurrentHref = (target: string) => {+ target = (target || '').trim();+ // Empty string or `/` can match any path+ if (!target || target === '/') {+ return true;+ }+ const { pathname, search, hash } = window.location;+ const tPart = target.split('?');++ /**+ * With the current requirements, `hash` and `search` can simply be matched+ * Later extended to field-by-field matching if necessary+ */+ if (tPart[1]) {+ const tChip = tPart[1].split('#');+ const tSearch = tChip[0] || '';+ const tHash = tChip[1] || '';+ if (tHash && hash.indexOf(tHash) === -1) {+ return false;+ }+ if (tSearch && search.indexOf(tSearch) === -1) {+ return false;+ }+ }++ /**+ * As determination above, `tPart[0]` must be a valid string+ */+ let pathMatch = true;+ const tPath = tPart[0].split('/').filter((_) => !!_);+ const lPath = pathname.split('/').filter((_) => !!_);++ tPath.forEach((p, i) => {+ const lp = lPath[i];+ if (p !== lp) {+ pathMatch = false;+ }+ });++ return pathMatch;+};const storageLoginRedirect = () => {const { pathname } = window.location;@@ -136,5 +177,6 @@ export const floppyNavigation = {isRoutableLink,handleRouteLinkClick,equalToCurrentHref,+ matchToCurrentHref,storageLoginRedirect,};diff --git a/ui/src/utils/guard.ts b/ui/src/utils/guard.tsindex c9c34a2d..0b92f081 100644--- a/ui/src/utils/guard.ts+++ b/ui/src/utils/guard.ts@@ -94,13 +94,15 @@ export const IGNORE_PATH_LIST = ['/user-center/',];-export const isIgnoredPath = (ignoredPath: string | string[]) => {+export const isIgnoredPath = (ignoredPath?: string | string[]) => {+ if (!ignoredPath) {+ ignoredPath = IGNORE_PATH_LIST;+ }if (!Array.isArray(ignoredPath)) {ignoredPath = [ignoredPath];}- const { pathname } = window.location;- const matchingPath = ignoredPath.find((_) => {- return pathname.indexOf(_) !== -1;+ const matchingPath = ignoredPath.find((p) => {+ return floppyNavigation.matchToCurrentHref(p);});return !!matchingPath;};