Skip to content

Commit

Permalink
Add error handling for possible malformed body in lambda request
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30Alt committed Sep 4, 2024
1 parent df2ac22 commit 2f2fb62
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/build/framework/awsLambda/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ const response_1 = require("../response");
const utils_2 = require("../utils");
const constants_1 = require("../constants");
const supertokens_1 = __importDefault(require("../../supertokens"));
const error_1 = __importDefault(require("../../error"));
class AWSRequest extends request_1.BaseRequest {
constructor(event) {
super();
this.getFormDataFromRequestBody = async () => {
if (this.event.body === null || this.event.body === undefined) {
return {};
} else {
const parsedUrlEncodedFormData = Object.fromEntries(new URLSearchParams(this.event.body).entries());
return parsedUrlEncodedFormData === undefined ? {} : parsedUrlEncodedFormData;
try {
const parsedUrlEncodedFormData = Object.fromEntries(new URLSearchParams(this.event.body).entries());
return parsedUrlEncodedFormData === undefined ? {} : parsedUrlEncodedFormData;
} catch (err) {
throw new error_1.default({
type: error_1.default.BAD_INPUT_ERROR,
message: "API input error: Please make sure to pass valid url encoded form in the request body",
});
}
}
};
this.getJSONFromRequestBody = async () => {
Expand Down
12 changes: 10 additions & 2 deletions lib/ts/framework/awsLambda/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { COOKIE_HEADER } from "../constants";
import { SessionContainerInterface } from "../../recipe/session/types";
import SuperTokens from "../../supertokens";
import { Framework } from "../types";
import STError from "../../error";

export class AWSRequest extends BaseRequest {
private event: APIGatewayProxyEventV2 | APIGatewayProxyEvent;
Expand All @@ -44,8 +45,15 @@ export class AWSRequest extends BaseRequest {
if (this.event.body === null || this.event.body === undefined) {
return {};
} else {
const parsedUrlEncodedFormData = Object.fromEntries(new URLSearchParams(this.event.body).entries());
return parsedUrlEncodedFormData === undefined ? {} : parsedUrlEncodedFormData;
try {
const parsedUrlEncodedFormData = Object.fromEntries(new URLSearchParams(this.event.body).entries());
return parsedUrlEncodedFormData === undefined ? {} : parsedUrlEncodedFormData;
} catch (err) {
throw new STError({
type: STError.BAD_INPUT_ERROR,
message: "API input error: Please make sure to pass valid url encoded form in the request body",
});
}
}
};

Expand Down

0 comments on commit 2f2fb62

Please sign in to comment.