Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fift injection #61

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { IpfsCodeStorageProvider } from "./ipfs-code-storage-provider";
import rateLimit from "express-rate-limit";
import { checkPrerequisites } from "./check-prerequisites";
import { FiftSourceVerifier } from "./source-verifier/fift-source-verifier";
import { FuncSourceVerifier } from "./source-verifier/func-source-verifier";
import { FuncSourceVerifier, specialCharsRegex } from "./source-verifier/func-source-verifier";
import { TactSourceVerifier, FileSystem } from "./source-verifier/tact-source-verifier";
import { TonReaderClientImpl } from "./ton-reader-client";
import { getLatestVerified } from "./latest-known-contracts";
Expand Down Expand Up @@ -61,6 +61,12 @@ const sourcesUpload = multer({
files: 50,
fileSize: 200 * 1024,
},
fileFilter(req, file, callback) {
callback(
null,
!specialCharsRegex().test(file.originalname) && !specialCharsRegex().test(file.fieldname),
);
},
});

const tactStagingUpload = multer({
Expand All @@ -84,7 +90,12 @@ const tactStagingUpload = multer({
fileSize: 200 * 1024,
},
fileFilter(req, file, callback) {
callback(null, !!file.originalname.match(/\.(boc|pkg)/));
callback(
null,
!!file.originalname.match(/\.(boc|pkg)/) &&
!specialCharsRegex().test(file.originalname) &&
!specialCharsRegex().test(file.fieldname),
);
},
});

Expand Down Expand Up @@ -142,7 +153,8 @@ app.get("/hc", (req, res) => {
);

// Not awaiting on purpose, otherwise this may take too much time.
getLatestVerified(process.env.VERIFIER_ID!, process.env.IPFS_PROVIDER!);
if (process.env.NODE_ENV === "production")
getLatestVerified(process.env.VERIFIER_ID!, process.env.IPFS_PROVIDER!);

app.post(
"/source",
Expand Down Expand Up @@ -217,6 +229,7 @@ app.get("/hc", (req, res) => {
app.use(function (err: any, req: any, res: any, next: any) {
console.error(err.message); // Log error message in our server's console
if (!err.statusCode) err.statusCode = 500; // If err has no specified error code, set error code to 'Internal Server Error (500)'
console.error(err.stack);
res.status(err.statusCode).send(err); // All HTTP requests must have a response, so let's send back an error with its status
});

Expand Down
5 changes: 5 additions & 0 deletions src/source-verifier/fift-source-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import path from "path";
import { Cell } from "ton";
import { FuncCompilerVersion } from "@ton-community/contract-verifier-sdk";
import { binaryPath } from "../binaries";
import { specialCharsRegex } from "./func-source-verifier";

export async function fiftToCodeCell(
funcVersion: FuncCompilerVersion,
Expand All @@ -23,6 +24,10 @@ boc>B "${b64OutFile}" B>file`;

const executable = path.join(process.cwd(), binaryPath, funcVersion, "fift");

if (specialCharsRegex().test(executable)) {
throw new Error("Unallowed special characters in command line");
}

process.env.FIFTPATH = path.join(process.cwd(), binaryPath, funcVersion, "fiftlib");

await execAsync(`${executable} -s ${tmpB64Fift}`);
Expand Down
4 changes: 3 additions & 1 deletion src/source-verifier/func-source-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import { binaryPath } from "../binaries";
const semverRegex = () =>
/^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-(0|[1-9A-Za-z-][0-9A-Za-z-]*)(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$/;

export const specialCharsRegex = () => /[;>\&`\|\$\(\)\[\]\{\}'"\\\#]/;

function prepareFuncCommand(
executable: string,
funcArgs: string,
fiftOutFile: string,
commandLine: string,
) {
if (/[;>\&`\|\$\(\)\[\]\{\}'"\\\#]/.test(commandLine)) {
if (specialCharsRegex().test(commandLine)) {
throw new Error("Unallowed special characters in command line");
}
const getPath = (_path: string) => _path;
Expand Down
Loading