Skip to content

Commit

Permalink
feat: 后端检查特殊字符
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Nov 10, 2024
1 parent dfb319e commit 884872f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export const FileListSchema = avsc.Type.forSchema({
},
});

const bannedCharacters = /[&<>\"'\r\n]/g;

export class Utilities {
public static isRunningInDocker(): boolean {
return process.env.IS_IN_DOCKER === 'true';
Expand Down Expand Up @@ -548,4 +550,13 @@ export class Utilities {
return new Date(Date.now() + after * 365 * 24 * 60 * 60 * 1000);
}
}

public static checkName(name: string | null): boolean {
if (!name) return true;
return !bannedCharacters.test(name);
};

public static checkNameRule(name: string): boolean | string {
return bannedCharacters.test(name) ? '名称不能包含特殊字符' : true;
}
}
11 changes: 11 additions & 0 deletions src/routes/ApiClusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ export class ApiClusters {
const bandwidth = Number(req.body.bandwidth || 0);
if (Number.isNaN(bandwidth) || bandwidth <= 10 || bandwidth > 500) {
res.status(400).json({ error: "Invalid bandwidth" });
return;
}
if (name.length < 1 || name.length > 20 || name === "") {
res.status(400).json({ error: "Invalid name" });
return;
}
if (!Utilities.checkName(name)) {
res.status(400).json({ error: "Name cannot contain special characters" });
return;
}

let cluster = new ClusterEntity();
Expand Down Expand Up @@ -129,6 +135,11 @@ export class ApiClusters {
const isProxy = Boolean(req.body.isProxy) || false;
const isMasterStats = Boolean(req.body.isMasterStats) || false;

if (!Utilities.checkName(clusterName) || !Utilities.checkName(sponsor) || !Utilities.checkName(sponsorUrl) || !Utilities.checkName(sponsorBanner)) {
res.status(400).json({ error: "Cannot contain special characters" });
return;
}

const cluster = inst.clusters.find(c => c.clusterId === clusterId);
if (!cluster) {
res.status(404).send(); // 集群不存在
Expand Down
5 changes: 5 additions & 0 deletions src/routes/ApiUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export class ApiUser {
const sponsorUrl = req.body.sponsorUrl as string || null;
const sponsorBanner = req.body.sponsorBanner as string || null;

if (!Utilities.checkName(name) || !Utilities.checkName(sponsor) || !Utilities.checkName(sponsorUrl) || !Utilities.checkName(sponsorBanner)) {
res.status(400).json({ error: "Cannot contain special characters" });
return;
}

if (bandwidth !== null && (Number.isNaN(bandwidth) || bandwidth < 10 || bandwidth > 500)) {
res.status(400).send({ message: 'Invalid bandwidth' });
return;
Expand Down

0 comments on commit 884872f

Please sign in to comment.