-
Notifications
You must be signed in to change notification settings - Fork 40
feat(aap): In App WAF support #744
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
Draft
CarlesDD
wants to merge
5
commits into
main
Choose a base branch
from
ccapell/APPSEC-60752/in-app-waf-port
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+724
−1
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6476d5f
Remove unused native-appsec binaries + native-appsec version pinned i…
CarlesDD 512bae1
Extract event data and pass it over dc
CarlesDD 976a5e0
Fix linting issues
CarlesDD 5832672
Fix formatting issue
CarlesDD a9a47e9
Fix test mock
CarlesDD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,240 @@ | ||
| import { extractHTTPDataFromEvent } from "./event-data-extractor"; | ||
|
|
||
| describe("extractHTTPDataFromEvent", () => { | ||
| describe("non-HTTP events", () => { | ||
| it("should return undefined for SQS events", () => { | ||
| const event = { Records: [{ eventSource: "aws:sqs", body: "test" }] }; | ||
| expect(extractHTTPDataFromEvent(event)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should return undefined for S3 events", () => { | ||
| const event = { Records: [{ s3: { bucket: { name: "test" } } }] }; | ||
| expect(extractHTTPDataFromEvent(event)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should return undefined for empty events", () => { | ||
| expect(extractHTTPDataFromEvent({})).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("API Gateway v1", () => { | ||
| const baseEvent = { | ||
| httpMethod: "GET", | ||
| path: "/my/path", | ||
| resource: "/my/{param}", | ||
| headers: { Host: "example.com", Cookie: "session=abc; lang=en" }, | ||
| multiValueHeaders: null, | ||
| queryStringParameters: { foo: "bar" }, | ||
| multiValueQueryStringParameters: null, | ||
| pathParameters: { param: "123" }, | ||
| body: null, | ||
| isBase64Encoded: false, | ||
| requestContext: { | ||
| stage: "prod", | ||
| path: "/prod/my/path", | ||
| identity: { sourceIp: "1.2.3.4" }, | ||
| }, | ||
| }; | ||
|
|
||
| it("should extract HTTP data correctly", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result!.method).toBe("GET"); | ||
| expect(result!.path).toBe("/prod/my/path"); | ||
| expect(result!.clientIp).toBe("1.2.3.4"); | ||
| expect(result!.route).toBe("/my/{param}"); | ||
| expect(result!.pathParams).toEqual({ param: "123" }); | ||
| expect(result!.query).toEqual({ foo: "bar" }); | ||
| expect(result!.isBase64Encoded).toBe(false); | ||
| }); | ||
|
|
||
| it("should separate cookies from headers", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
|
|
||
| expect(result!.headers.cookie).toBeUndefined(); | ||
| expect(result!.cookies).toEqual({ session: "abc", lang: "en" }); | ||
| }); | ||
|
|
||
| it("should normalize header names to lowercase", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
|
|
||
| expect(result!.headers.host).toBe("example.com"); | ||
| }); | ||
|
|
||
| it("should decode base64 body", () => { | ||
| const event = { | ||
| ...baseEvent, | ||
| body: Buffer.from('{"key":"value"}').toString("base64"), | ||
| isBase64Encoded: true, | ||
| }; | ||
|
|
||
| const result = extractHTTPDataFromEvent(event); | ||
| expect(result!.body).toEqual({ key: "value" }); | ||
| expect(result!.isBase64Encoded).toBe(true); | ||
| }); | ||
|
|
||
| it("should parse JSON body when not base64 encoded", () => { | ||
| const event = { | ||
| ...baseEvent, | ||
| body: '{"key":"value"}', | ||
| }; | ||
|
|
||
| const result = extractHTTPDataFromEvent(event); | ||
| expect(result!.body).toEqual({ key: "value" }); | ||
| }); | ||
|
|
||
| it("should return raw string body when not JSON", () => { | ||
| const event = { | ||
| ...baseEvent, | ||
| body: "plain text body", | ||
| }; | ||
|
|
||
| const result = extractHTTPDataFromEvent(event); | ||
| expect(result!.body).toBe("plain text body"); | ||
| }); | ||
|
|
||
| it("should merge multi-value query params", () => { | ||
| const event = { | ||
| ...baseEvent, | ||
| queryStringParameters: { foo: "bar" }, | ||
| multiValueQueryStringParameters: { foo: ["bar", "baz"], single: ["one"] }, | ||
| }; | ||
|
|
||
| const result = extractHTTPDataFromEvent(event); | ||
| expect(result!.query).toEqual({ foo: ["bar", "baz"], single: "one" }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("API Gateway v2", () => { | ||
| const baseEvent = { | ||
| version: "2.0", | ||
| rawPath: "/my/path", | ||
| rawQueryString: "foo=bar", | ||
| headers: { host: "example.com" }, | ||
| queryStringParameters: { foo: "bar" }, | ||
| pathParameters: { id: "456" }, | ||
| body: null, | ||
| isBase64Encoded: false, | ||
| cookies: ["session=abc", "lang=en"], | ||
| routeKey: "GET /my/{id}", | ||
| requestContext: { | ||
| http: { | ||
| method: "POST", | ||
| path: "/my/path", | ||
| sourceIp: "5.6.7.8", | ||
| }, | ||
| domainName: "api.example.com", | ||
| apiId: "abc123", | ||
| stage: "$default", | ||
| }, | ||
| }; | ||
|
|
||
| it("should extract HTTP data correctly", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result!.method).toBe("POST"); | ||
| expect(result!.path).toBe("/my/path"); | ||
| expect(result!.clientIp).toBe("5.6.7.8"); | ||
| expect(result!.route).toBe("/my/{id}"); | ||
| expect(result!.pathParams).toEqual({ id: "456" }); | ||
| }); | ||
|
|
||
| it("should parse cookies from the cookies array", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
|
|
||
| expect(result!.cookies).toEqual({ session: "abc", lang: "en" }); | ||
| }); | ||
|
|
||
| it("should extract route from routeKey", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
| expect(result!.route).toBe("/my/{id}"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("ALB", () => { | ||
| const baseEvent = { | ||
| httpMethod: "GET", | ||
| path: "/alb/path", | ||
| headers: { | ||
| host: "example.com", | ||
| "x-forwarded-for": "9.8.7.6, 10.0.0.1", | ||
| cookie: "token=xyz", | ||
| }, | ||
| queryStringParameters: { key: "val" }, | ||
| body: null, | ||
| isBase64Encoded: false, | ||
| requestContext: { | ||
| elb: { | ||
| targetGroupArn: "arn:aws:elasticloadbalancing:us-east-1:123456789:targetgroup/my-tg/abc", | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| it("should extract HTTP data correctly", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result!.method).toBe("GET"); | ||
| expect(result!.path).toBe("/alb/path"); | ||
| }); | ||
|
|
||
| it("should extract client IP from x-forwarded-for", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
| expect(result!.clientIp).toBe("9.8.7.6"); | ||
| }); | ||
|
|
||
| it("should parse cookies from the cookie header", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
| expect(result!.cookies).toEqual({ token: "xyz" }); | ||
| expect(result!.headers.cookie).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should not have route or pathParams", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
| expect(result!.route).toBeUndefined(); | ||
| expect(result!.pathParams).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Lambda Function URL", () => { | ||
| const baseEvent = { | ||
| version: "2.0", | ||
| rawPath: "/url/path", | ||
| rawQueryString: "", | ||
| headers: { host: "abc123.lambda-url.us-east-1.on.aws" }, | ||
| queryStringParameters: null, | ||
| body: null, | ||
| isBase64Encoded: false, | ||
| cookies: ["token=xyz"], | ||
| requestContext: { | ||
| domainName: "abc123.lambda-url.us-east-1.on.aws", | ||
| http: { | ||
| method: "GET", | ||
| path: "/url/path", | ||
| sourceIp: "11.12.13.14", | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| it("should extract HTTP data correctly", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result!.method).toBe("GET"); | ||
| expect(result!.path).toBe("/url/path"); | ||
| expect(result!.clientIp).toBe("11.12.13.14"); | ||
| }); | ||
|
|
||
| it("should parse cookies from the cookies array", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
| expect(result!.cookies).toEqual({ token: "xyz" }); | ||
| }); | ||
|
|
||
| it("should not have route", () => { | ||
| const result = extractHTTPDataFromEvent(baseEvent); | ||
| expect(result!.route).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
moveDependency('@datadog/native-appsec')?