-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
259 additions
and
112 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,52 @@ | ||
import type { FastifyInstance } from "fastify"; | ||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; | ||
import { env } from "../../shared/utils/env"; | ||
import { StatusCodes } from "http-status-codes"; | ||
|
||
export function withEnforceEngineMode(server: FastifyInstance) { | ||
if (env.ENGINE_MODE === "sandbox") { | ||
server.addHook("onRequest", async (request, reply) => { | ||
if (request.method !== "GET") { | ||
return reply.status(405).send({ | ||
statusCode: 405, | ||
error: "Engine is in read-only mode. Only GET requests are allowed.", | ||
message: | ||
"Engine is in read-only mode. Only GET requests are allowed.", | ||
}); | ||
switch (env.ENGINE_MODE) { | ||
case "lite": | ||
server.addHook("onRequest", enforceLiteMode); | ||
break; | ||
case "sandbox": | ||
server.addHook("onRequest", enforceSandboxMode); | ||
break; | ||
} | ||
} | ||
|
||
const ALLOWED_LITE_MODE_PATHS_GET = new Set(["/backend-wallet/lite/:teamId"]); | ||
const ALLOWED_LITE_MODE_PATHS_POST = new Set([ | ||
"/backend-wallet/lite/:teamId", | ||
"/backend-wallet/sign-message", | ||
]); | ||
async function enforceLiteMode(request: FastifyRequest, reply: FastifyReply) { | ||
if (request.routeOptions.url) { | ||
if (request.method === "GET") { | ||
if (ALLOWED_LITE_MODE_PATHS_GET.has(request.routeOptions.url)) { | ||
return; | ||
} | ||
} else if (request.method === "POST") { | ||
if (ALLOWED_LITE_MODE_PATHS_POST.has(request.routeOptions.url)) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
return reply.status(StatusCodes.FORBIDDEN).send({ | ||
statusCode: StatusCodes.FORBIDDEN, | ||
message: "Engine is in lite mode. Only limited endpoints are allowed.", | ||
error: "ENGINE_MODE_FORBIDDEN", | ||
}); | ||
} | ||
|
||
async function enforceSandboxMode( | ||
request: FastifyRequest, | ||
reply: FastifyReply, | ||
) { | ||
if (request.method !== "GET") { | ||
return reply.status(StatusCodes.FORBIDDEN).send({ | ||
statusCode: StatusCodes.FORBIDDEN, | ||
message: "Engine is in sandbox mode. Only GET requests are allowed.", | ||
error: "ENGINE_MODE_FORBIDDEN", | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { FastifyRequest } from "fastify"; | ||
import type { AuthenticationType } from "../middleware/auth"; | ||
import { createCustomError } from "../middleware/error"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { env } from "node:process"; | ||
|
||
export function assertAuthenticationType<T extends AuthenticationType>( | ||
req: FastifyRequest, | ||
types: T[], | ||
): asserts req is FastifyRequest & { | ||
authentication: { type: T }; | ||
} { | ||
if (!types.includes(req.authentication.type as T)) { | ||
throw createCustomError( | ||
`This endpoint requires authentication type: ${types.join(", ")}`, | ||
StatusCodes.FORBIDDEN, | ||
"FORBIDDEN_AUTHENTICATION_TYPE", | ||
); | ||
} | ||
} | ||
|
||
export function getDecryptionPassword(req: FastifyRequest) { | ||
if (env.ENGINE_MODE === "lite" && req.authentication.type === "lite") { | ||
return req.authentication.litePassword; | ||
} | ||
return env.ENCRYPTION_PASSWORD; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters