Skip to content

数据库访问迁移#1714

Merged
konpoku merged 1 commit intomasterfrom
dev
Mar 9, 2026
Merged

数据库访问迁移#1714
konpoku merged 1 commit intomasterfrom
dev

Conversation

@nozomizo1314
Copy link
Contributor

No description provided.

return res.status(422).send("422 Unprocessable Entity: Missing credentials");
router.post(
"/update",
authenticate(["counselor", "root"]),

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.

Copilot Autofix

AI 5 days ago

In general, to fix missing rate limiting on sensitive or expensive endpoints, you introduce a rate-limiting middleware (e.g., via express-rate-limit) and apply it to those routes (or the entire router). This middleware tracks requests per client (commonly by IP) over a fixed time window and rejects requests that exceed a configured threshold, thus preventing a single client from overwhelming the server.

For this specific file, the least intrusive fix that doesn’t change existing functionality is:

  • Import express-rate-limit.
  • Create a limiter instance configured for authenticated, admin-like operations (e.g., a modest number of requests per minute/hour).
  • Apply this limiter to the three POST routes that interact with noticeFunc (/update, /add, /delete) by inserting it into the middleware chain just after authenticate([...]). This preserves current auth behavior and route logic, and only adds rate limiting on top. We restrict the change strictly to src/routes/notice.ts and avoid assuming anything about the rest of the app.

Concretely:

  • At the top of src/routes/notice.ts, add an import for express-rate-limit.
  • After creating the router, define a noticeLimiter using rateLimit({ windowMs: ..., max: ... }). Reasonable defaults could be e.g. windowMs: 15 * 60 * 1000 (15 minutes) and max: 100.
  • Update each router.post for /update, /add, and /delete so that the middleware chain becomes authenticate(["counselor", "root"]), noticeLimiter, async (req, res) => { ... }, leaving the handler bodies unchanged (except for any necessary line-number shifts).
Suggested changeset 2
src/routes/notice.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/routes/notice.ts b/src/routes/notice.ts
--- a/src/routes/notice.ts
+++ b/src/routes/notice.ts
@@ -1,11 +1,19 @@
 import express from "express";
 import authenticate from "../middlewares/authenticate";
 import * as noticeFunc from "../hasura/notice";
+import rateLimit from "express-rate-limit";
 const router = express.Router();
+
+const noticeLimiter = rateLimit({
+  windowMs: 15 * 60 * 1000, // 15 minutes
+  max: 100, // limit each IP to 100 requests per windowMs
+});
+
 //TODO: Optional parameters should be implemented.
 router.post(
   "/update",
   authenticate(["counselor", "root"]),
+  noticeLimiter,
   async (req, res) => {
     try {
       const title: string = req.body.title;
@@ -53,33 +56,39 @@
 //});
 
 //newly added
-router.post("/add", authenticate(["counselor", "root"]), async (req, res) => {
-  try {
-    const title: string = req.body.title;
-    const content: string = req.body.content;
-    const files: string = req.body.files;
-    const notice_type: string = req.body.notice_type;
+router.post(
+  "/add",
+  authenticate(["counselor", "root"]),
+  noticeLimiter,
+  async (req, res) => {
+    try {
+      const title: string = req.body.title;
+      const content: string = req.body.content;
+      const files: string = req.body.files;
+      const notice_type: string = req.body.notice_type;
 
-    if (!title || !content || !files || !notice_type) {
-      return res
-        .status(422)
-        .send("422 Unprocessable Entity: Missing credentials");
+      if (!title || !content || !files || !notice_type) {
+        return res
+          .status(422)
+          .send("422 Unprocessable Entity: Missing credentials");
+      }
+      const notice_id: string = await noticeFunc.add_notice(
+        title,
+        content,
+        files,
+        notice_type,
+      );
+      return res.status(200).send(notice_id);
+    } catch (err) {
+      return res.status(500).send("Internal Server Error");
     }
-    const notice_id: string = await noticeFunc.add_notice(
-      title,
-      content,
-      files,
-      notice_type,
-    );
-    return res.status(200).send(notice_id);
-  } catch (err) {
-    return res.status(500).send("Internal Server Error");
-  }
-});
+  },
+);
 
 router.post(
   "/delete",
   authenticate(["counselor", "root"]),
+  noticeLimiter,
   async (req, res) => {
     try {
       const id: string = req.body.id;
EOF
package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/package.json b/package.json
--- a/package.json
+++ b/package.json
@@ -36,7 +36,8 @@
     "openai": "6.22.0",
     "qcloud-cos-sts": "3.1.3",
     "unisms": "0.0.6",
-    "web-push": "3.6.7"
+    "web-push": "3.6.7",
+    "express-rate-limit": "^8.3.0"
   },
   "devDependencies": {
     "@types/bcrypt": "6.0.0",
EOF
@@ -36,7 +36,8 @@
"openai": "6.22.0",
"qcloud-cos-sts": "3.1.3",
"unisms": "0.0.6",
"web-push": "3.6.7"
"web-push": "3.6.7",
"express-rate-limit": "^8.3.0"
},
"devDependencies": {
"@types/bcrypt": "6.0.0",
This fix introduces these dependencies
Package Version Security advisories
express-rate-limit (npm) 8.3.0 None
Copilot is powered by AI and may make mistakes. Always verify output.

router.post("/delete", authenticate(["counselor","root"]), async (req, res) => {
//newly added
router.post("/add", authenticate(["counselor", "root"]), async (req, res) => {

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.

Copilot Autofix

AI 5 days ago

In general, the issue should be fixed by introducing a rate-limiting middleware (e.g., via express-rate-limit) and applying it to the sensitive routes that perform database or other expensive operations. This middleware should restrict the number of requests per IP (or per user) over a time window, returning an HTTP 429 status when the limit is exceeded.

The best fix here, without changing existing functionality, is to add a rate limiter to src/routes/notice.ts and apply it to the /add route (and, ideally, the other modification routes as well). We can safely import express-rate-limit, define a limiter instance in this file with reasonable defaults (e.g., 100 requests per 15 minutes per IP), and insert it into the middleware chain for the /add route. That preserves all existing logic, validation, and return values while simply enforcing a maximum request rate. Concretely:

  • Add import rateLimit from "express-rate-limit"; near the top.
  • Define const noticeRateLimiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 100 }); after the router is created.
  • Update the /add route definition so its middleware chain becomes authenticate([...]) followed by noticeRateLimiter and then the async handler: router.post("/add", authenticate([...]), noticeRateLimiter, async (req, res) => { ... });.
    All changes are confined to src/routes/notice.ts.
Suggested changeset 2
src/routes/notice.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/routes/notice.ts b/src/routes/notice.ts
--- a/src/routes/notice.ts
+++ b/src/routes/notice.ts
@@ -1,7 +1,14 @@
 import express from "express";
 import authenticate from "../middlewares/authenticate";
 import * as noticeFunc from "../hasura/notice";
+import rateLimit from "express-rate-limit";
 const router = express.Router();
+
+const noticeRateLimiter = rateLimit({
+  windowMs: 15 * 60 * 1000, // 15 minutes
+  max: 100, // limit each IP to 100 requests per window
+});
+
 //TODO: Optional parameters should be implemented.
 router.post(
   "/update",
@@ -53,29 +59,34 @@
 //});
 
 //newly added
-router.post("/add", authenticate(["counselor", "root"]), async (req, res) => {
-  try {
-    const title: string = req.body.title;
-    const content: string = req.body.content;
-    const files: string = req.body.files;
-    const notice_type: string = req.body.notice_type;
+router.post(
+  "/add",
+  authenticate(["counselor", "root"]),
+  noticeRateLimiter,
+  async (req, res) => {
+    try {
+      const title: string = req.body.title;
+      const content: string = req.body.content;
+      const files: string = req.body.files;
+      const notice_type: string = req.body.notice_type;
 
-    if (!title || !content || !files || !notice_type) {
-      return res
-        .status(422)
-        .send("422 Unprocessable Entity: Missing credentials");
+      if (!title || !content || !files || !notice_type) {
+        return res
+          .status(422)
+          .send("422 Unprocessable Entity: Missing credentials");
+      }
+      const notice_id: string = await noticeFunc.add_notice(
+        title,
+        content,
+        files,
+        notice_type,
+      );
+      return res.status(200).send(notice_id);
+    } catch (err) {
+      return res.status(500).send("Internal Server Error");
     }
-    const notice_id: string = await noticeFunc.add_notice(
-      title,
-      content,
-      files,
-      notice_type,
-    );
-    return res.status(200).send(notice_id);
-  } catch (err) {
-    return res.status(500).send("Internal Server Error");
-  }
-});
+  },
+);
 
 router.post(
   "/delete",
EOF
package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/package.json b/package.json
--- a/package.json
+++ b/package.json
@@ -36,7 +36,8 @@
     "openai": "6.22.0",
     "qcloud-cos-sts": "3.1.3",
     "unisms": "0.0.6",
-    "web-push": "3.6.7"
+    "web-push": "3.6.7",
+    "express-rate-limit": "^8.3.0"
   },
   "devDependencies": {
     "@types/bcrypt": "6.0.0",
EOF
@@ -36,7 +36,8 @@
"openai": "6.22.0",
"qcloud-cos-sts": "3.1.3",
"unisms": "0.0.6",
"web-push": "3.6.7"
"web-push": "3.6.7",
"express-rate-limit": "^8.3.0"
},
"devDependencies": {
"@types/bcrypt": "6.0.0",
This fix introduces these dependencies
Package Version Security advisories
express-rate-limit (npm) 8.3.0 None
Copilot is powered by AI and may make mistakes. Always verify output.

export default router; No newline at end of file
router.post(
"/delete",

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.

Copilot Autofix

AI 5 days ago

In general, to fix missing rate limiting in Express routes, add a rate‑limiting middleware (commonly from express-rate-limit) and apply it to the expensive routes or to the entire router. This middleware will cap how many requests a client (typically by IP) can make in a given time window, reducing the impact of abusive or automated request floods while leaving normal usage unaffected.

For this file, the best low‑impact fix is:

  • Import express-rate-limit.
  • Define a limiter specifically for these notice‑management routes, with reasonable defaults (e.g., 100 requests per 15 minutes per IP).
  • Apply this limiter to the router so it covers /update, /add, and /delete without changing their existing logic or signatures.

Concretely:

  • At the top of src/routes/notice.ts, after the existing imports, add an import for express-rate-limit.
  • Immediately after const router = express.Router();, define a noticeRateLimiter using rateLimit({ windowMs: 15 * 60 * 1000, max: 100 }).
  • Add router.use(noticeRateLimiter); so all routes in this router are rate‑limited.
  • No changes are needed to the route handlers themselves, preserving existing behavior aside from introducing throttling when limits are exceeded.
Suggested changeset 2
src/routes/notice.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/routes/notice.ts b/src/routes/notice.ts
--- a/src/routes/notice.ts
+++ b/src/routes/notice.ts
@@ -1,7 +1,15 @@
 import express from "express";
 import authenticate from "../middlewares/authenticate";
 import * as noticeFunc from "../hasura/notice";
+import rateLimit from "express-rate-limit";
 const router = express.Router();
+
+const noticeRateLimiter = rateLimit({
+  windowMs: 15 * 60 * 1000, // 15 minutes
+  max: 100, // limit each IP to 100 requests per windowMs
+});
+
+router.use(noticeRateLimiter);
 //TODO: Optional parameters should be implemented.
 router.post(
   "/update",
EOF
@@ -1,7 +1,15 @@
import express from "express";
import authenticate from "../middlewares/authenticate";
import * as noticeFunc from "../hasura/notice";
import rateLimit from "express-rate-limit";
const router = express.Router();

const noticeRateLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});

router.use(noticeRateLimiter);
//TODO: Optional parameters should be implemented.
router.post(
"/update",
package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/package.json b/package.json
--- a/package.json
+++ b/package.json
@@ -36,7 +36,8 @@
     "openai": "6.22.0",
     "qcloud-cos-sts": "3.1.3",
     "unisms": "0.0.6",
-    "web-push": "3.6.7"
+    "web-push": "3.6.7",
+    "express-rate-limit": "^8.3.0"
   },
   "devDependencies": {
     "@types/bcrypt": "6.0.0",
EOF
@@ -36,7 +36,8 @@
"openai": "6.22.0",
"qcloud-cos-sts": "3.1.3",
"unisms": "0.0.6",
"web-push": "3.6.7"
"web-push": "3.6.7",
"express-rate-limit": "^8.3.0"
},
"devDependencies": {
"@types/bcrypt": "6.0.0",
This fix introduces these dependencies
Package Version Security advisories
express-rate-limit (npm) 8.3.0 None
Copilot is powered by AI and may make mistakes. Always verify output.
@konpoku konpoku merged commit dbc8f05 into master Mar 9, 2026
5 of 6 checks passed
@konpoku konpoku deleted the dev branch March 9, 2026 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants