Skip to content

Commit 0ca5f6b

Browse files
committed
feat: enforce JSON-only content type for mutation methods in ExpressServer
1 parent 8c45a64 commit 0ca5f6b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

adminforth/servers/express.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ class ExpressServer implements IExpressHttpServer {
269269
const fullPath = `${this.adminforth.config.baseUrl}/adminapi/v1${path}`;
270270

271271
const expressHandler = async (req, res) => {
272+
// Enforce JSON-only for mutation HTTP methods
273+
// AdminForth API endpoints accept only application/json for POST, PUT, PATCH, DELETE
274+
// If you need other content types, use a custom server endpoint.
275+
const method = (req.method || '').toUpperCase();
276+
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
277+
const contentTypeHeader = (req.headers?.['content-type'] || '').toString();
278+
const isJson = contentTypeHeader.toLowerCase().startsWith('application/json');
279+
if (!isJson) {
280+
const passed = contentTypeHeader || 'undefined';
281+
res.status(415).send(`AdminForth API endpoints support only requests with Content/Type: application/json, when you passed: ${passed}. Please use custom server endpoint if you really need this content type`);
282+
return;
283+
}
284+
}
272285
let body = req.body || {};
273286
if (typeof body === 'string') {
274287
try {

adminforth/spa/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function callApi({path, method, body=undefined}: {
5050

5151
export async function callAdminForthApi({ path, method, body=undefined, headers=undefined }: {
5252
path: string,
53-
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
53+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH',
5454
body?: any,
5555
headers?: Record<string, string>
5656
}): Promise<any> {

0 commit comments

Comments
 (0)