This is an automated email from the ASF dual-hosted git repository.
dengliming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu-dashboard.git
The following commit(s) were added to refs/heads/master by this push:
new af715c35 fix: redirect unauthorized users instead of rendering a dead
Route (#709)
af715c35 is described below
commit af715c35d3b39de8f9a70bdc801c53ac963eaa02
Author: Hussnain Ahmad <[email protected]>
AuthorDate: Sun Oct 11 10:31:50 2026 -0400
fix: redirect unauthorized users instead of rendering a dead Route (#709)
* fix: redirect unauthorized users instead of rendering a dead Route
In the unauthorized branch of AuthRoute.render, the code returned a
<Route> whose path was the redirect target with a null component. That
route never matches the current location, so nothing rendered and the
user was never sent to the 403 page.
Return <Redirect to={redirectPath} /> (dva/router) so unauthorized
URLs actually navigate to the redirect target.
Closes #604
* test(authroute): add regression test for denied-permission redirect (#604)
* style(authroute): fix import order in regression test (dva/router before
./AuthRoute)
---
src/utils/AuthRoute.js | 4 ++--
src/utils/AuthRoute.test.js | 41 ++++++++++++++++++++++++++++++++++++++++-
2 files changed, 42 insertions(+), 3 deletions(-)
diff --git a/src/utils/AuthRoute.js b/src/utils/AuthRoute.js
index d5c94404..02ff2b93 100644
--- a/src/utils/AuthRoute.js
+++ b/src/utils/AuthRoute.js
@@ -17,7 +17,7 @@
import React, { Component } from "react";
import { connect } from "dva";
-import { Route } from "dva/router";
+import { Redirect, Route } from "dva/router";
import { Spin } from "antd";
import { filterTree } from "./utils";
import { getIntlContent } from "./IntlUtils";
@@ -347,7 +347,7 @@ export default class AuthRoute extends Component {
if (checkMenuAuth(paths, permissions)) {
return <Route path={path} component={component} />;
} else {
- return <Route path={redirectPath} component={null} />;
+ return <Redirect to={redirectPath} />;
}
}
}
diff --git a/src/utils/AuthRoute.test.js b/src/utils/AuthRoute.test.js
index 9d137211..5f3f4ccc 100644
--- a/src/utils/AuthRoute.test.js
+++ b/src/utils/AuthRoute.test.js
@@ -15,7 +15,8 @@
* limitations under the License.
*/
-import {
+import { Redirect, Route } from "dva/router";
+import AuthRoute, {
checkMenuAuth,
getAuthMenus,
getBaseRedirectPath,
@@ -192,3 +193,41 @@ it("removes parent menus when none of their children are
authorized", () => {
expect(getAuthMenus([], menuTree, permissions, false)).toEqual([]);
});
+
+// Regression test for issue #604: unauthorized routes must redirect instead
+// of rendering a dead route (blank page).
+const WrappedAuthRoute = AuthRoute.WrappedComponent;
+
+function renderAuthRoute({ path, permissions, redirectPath }) {
+ resetAuthMenuCache();
+ const instance = new WrappedAuthRoute({
+ loading: false,
+ path,
+ component: () => null,
+ global: { permissions },
+ redirectPath,
+ location: { pathname: path },
+ });
+ return instance.render();
+}
+
+it("redirects to the redirect path when permissions are denied", () => {
+ const redirectPath = "/exception/403";
+ const rendered = renderAuthRoute({
+ path: "/system/role",
+ permissions: { menu: [{ url: "/system/user" }] },
+ redirectPath,
+ });
+ expect(rendered.type).toBe(Redirect);
+ expect(rendered.props.to).toBe(redirectPath);
+});
+
+it("renders the route when permissions are granted", () => {
+ const rendered = renderAuthRoute({
+ path: "/system/role",
+ permissions: { menu: [{ url: "/system/role" }] },
+ redirectPath: "/exception/403",
+ });
+ expect(rendered.type).toBe(Route);
+ expect(rendered.props.path).toBe("/system/role");
+});