libddwaf 1.15.0.0.2 → 1.18.0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/bug.md +32 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +17 -0
  4. data/.github/actions/docker-build-ruby/Dockerfile +5 -0
  5. data/.github/actions/docker-build-ruby/Dockerfile.alpine +7 -0
  6. data/.github/actions/docker-build-ruby/Dockerfile.jruby +6 -0
  7. data/.github/actions/docker-build-ruby/action.yml +57 -0
  8. data/.github/workflows/lint.yml +34 -0
  9. data/.github/workflows/package.yml +134 -0
  10. data/.github/workflows/test.yml +118 -0
  11. data/.gitignore +10 -0
  12. data/.steepignore +4 -0
  13. data/CONTRIBUTING.md +84 -0
  14. data/Steepfile +21 -0
  15. data/lib/datadog/appsec/waf/context.rb +122 -0
  16. data/lib/datadog/appsec/waf/converter.rb +172 -0
  17. data/lib/datadog/appsec/waf/handle.rb +108 -0
  18. data/lib/datadog/appsec/waf/lib_ddwaf.rb +307 -0
  19. data/lib/datadog/appsec/waf/result.rb +33 -0
  20. data/lib/datadog/appsec/waf/version.rb +2 -2
  21. data/lib/datadog/appsec/waf.rb +17 -682
  22. data/libddwaf.gemspec +26 -29
  23. data/shell.nix +35 -0
  24. data/sig/datadog/appsec/waf/context.rbs +39 -0
  25. data/sig/datadog/appsec/waf/converter.rbs +11 -0
  26. data/sig/datadog/appsec/waf/handle.rbs +42 -0
  27. data/sig/datadog/appsec/waf/lib_ddwaf.rbs +156 -0
  28. data/sig/datadog/appsec/waf/result.rbs +33 -0
  29. data/sig/datadog/appsec/waf.rbs +1 -213
  30. metadata +27 -7
  31. data/vendor/libddwaf/libddwaf-1.15.0-darwin-arm64/lib/libddwaf.dylib +0 -0
  32. data/vendor/libddwaf/libddwaf-1.15.0-darwin-x86_64/lib/libddwaf.dylib +0 -0
  33. data/vendor/libddwaf/libddwaf-1.15.0-linux-aarch64/lib/libddwaf.so +0 -0
  34. data/vendor/libddwaf/libddwaf-1.15.0-linux-x86_64/lib/libddwaf.so +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66ca5ed402ac186fc8a1a0be677af6edfb2e93f8848232d6ea26895f1828ac14
4
- data.tar.gz: 27e819869d4042edb1dd218324c9363a94dc4a1f77fb0185ef0449b07d20c8e1
3
+ metadata.gz: 2cea26919b7498876af6b3517e509939b1dfc2bfac538dccf9d4f7d6ba02bb84
4
+ data.tar.gz: ec604e73b223f59abcc8d001a08f46ea7d130b2ecc9e4f672313b812e91e5016
5
5
  SHA512:
6
- metadata.gz: 4ea8d77ff20dc6347eb6580f0e230bfee3c98d3a7e448379fd6e992812dc386c93f505efb14d8c098843fc589489b2e48e91bebc4c0b89a96a2c258a661c6e90
7
- data.tar.gz: 84a18f48f1cd2be8c5bcb7af4b25aa8b7704d82f3d80f2cf10d1ace2c7a9c5c20981698be22a191b3e999b9080d2d2479c86110124d8e4fec6dc03e4cc267c19
6
+ metadata.gz: 706ca9b34d702e244d74b46f2ef5633de09f37b8b8cfa3591e4421417592a9034eeabde5358879a00b113acad38c04a2827418edf8b936f8af3302d8b902bd01
7
+ data.tar.gz: c4c0f294b8e4d9e173ed775d6b60ecf89ebeb97360dfd67bb6c8049582f5f9d3e5cd7bc3cf179ac319518bef11b38b1bfcb7e740e18281b14c22510ee763dfcc
@@ -0,0 +1,32 @@
1
+ ---
2
+ name: Bug report
3
+ about: File a bug report
4
+ title: ''
5
+ labels: bug
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Current behaviour**
11
+ <!-- What is be happening. -->
12
+
13
+ **Expected behaviour**
14
+ <!-- What should be happening. -->
15
+
16
+ **Steps to reproduce**
17
+ <!--
18
+ How can we reproduce this issue in order to diagnose it?
19
+ Code snippets, log messages, screenshots and sample apps are encouraged!
20
+ -->
21
+
22
+ **How does `libddwaf` help you?**
23
+ <!-- Optionally, tell us why and how you're using ddtrace, and what your overall experience with it is! -->
24
+
25
+ **Environment**
26
+
27
+ * **libddwaf version:**
28
+ * **libddwaf gem platform:**
29
+ * **Ruby version:**
30
+ * **Ruby platform:**
31
+ * **Operating system:**
32
+
@@ -0,0 +1,17 @@
1
+ **What does this PR do?**
2
+ <!-- A brief description of the change being made with this pull request. -->
3
+
4
+ **Motivation**
5
+ <!-- What inspired you to submit this pull request? -->
6
+
7
+ **Additional Notes**
8
+ <!-- Anything else we should know when reviewing? -->
9
+
10
+ **How to test the change?**
11
+ <!--
12
+ Describe here how the change can be validated.
13
+ You are strongly encouraged to provide automated tests for this PR.
14
+ If this change cannot be feasibly tested, please explain why,
15
+ unless the change does not modify code (e.g. only modifies docs, comments).
16
+ -->
17
+
@@ -0,0 +1,5 @@
1
+ ARG RUBY_VERSION
2
+
3
+ FROM ruby:${RUBY_VERSION:-2.5}
4
+
5
+ RUN gem update --system 3.3.27
@@ -0,0 +1,7 @@
1
+ ARG RUBY_VERSION
2
+
3
+ FROM ruby:${RUBY_VERSION:-2.5}-alpine
4
+
5
+ RUN apk add --no-cache build-base git
6
+
7
+ RUN gem update --system 3.3.27
@@ -0,0 +1,6 @@
1
+ ARG RUBY_VERSION
2
+
3
+ FROM jruby:${RUBY_VERSION:-9.2}
4
+
5
+ RUN apt-get update
6
+ RUN apt-get install -y build-essential git
@@ -0,0 +1,57 @@
1
+ name: Build docker image
2
+ description:
3
+ Github Actions does not support setting the platform for the container yet.
4
+ This action builds the image for the specified architecture and libc.
5
+
6
+ inputs:
7
+ ruby-version:
8
+ description: Ruby version
9
+ required: true
10
+
11
+ arch:
12
+ description: Build architecture
13
+ required: true
14
+
15
+ libc:
16
+ description: Which libc is used
17
+ required: true
18
+
19
+ jruby:
20
+ description: Whether to use JRuby
21
+ required: false
22
+ default: "false"
23
+
24
+ outputs:
25
+ run-cmd:
26
+ description: Command to run the container
27
+ value: ${{ steps.set-run-cmd.outputs.run-cmd }}
28
+
29
+ runs:
30
+ using: "composite"
31
+ steps:
32
+ - name: Set up QEMU
33
+ uses: docker/setup-qemu-action@v3
34
+ with:
35
+ platforms: ${{ inputs.arch }}
36
+
37
+ - name: Set up Docker Buildx
38
+ uses: docker/setup-buildx-action@v3
39
+
40
+ - name: Build image
41
+ uses: docker/build-push-action@v6
42
+ with:
43
+ file: .github/actions/docker-build-ruby/Dockerfile${{ inputs.libc == 'musl' && '.alpine' || '' }}${{ inputs.jruby == 'true' && '.jruby' || '' }}
44
+ build-args: |
45
+ "RUBY_VERSION=${{ inputs.ruby-version }}"
46
+ push: false
47
+ load: true
48
+ tags: libddwaf-rb-test:latest
49
+ cache-from: type=gha
50
+ cache-to: type=gha,mode=max
51
+ platforms: linux/${{ inputs.arch }}
52
+
53
+ - name: Set run-cmd output
54
+ id: set-run-cmd
55
+ shell: bash
56
+ run: |
57
+ echo "run-cmd=docker run --platform linux/${{ inputs.arch }} -v gems:/usr/local/bundle -v ${{ github.workspace }}:/libddwaf-rb -w /libddwaf-rb libddwaf-rb-test:latest" >> "$GITHUB_OUTPUT"
@@ -0,0 +1,34 @@
1
+ name: Lint
2
+ on:
3
+ - push
4
+
5
+ jobs:
6
+ style-check:
7
+ name: Style check
8
+ runs-on: ubuntu-24.04
9
+ container:
10
+ image: ruby:3.3
11
+ steps:
12
+ - name: Checkout
13
+ uses: actions/checkout@v2
14
+
15
+ - name: Bundle
16
+ run: bundle install
17
+
18
+ - name: Run Rubocop
19
+ run: bundle exec rubocop -D
20
+
21
+ type-check:
22
+ name: Type check
23
+ runs-on: ubuntu-24.04
24
+ container:
25
+ image: ruby:3.3
26
+ steps:
27
+ - name: Checkout
28
+ uses: actions/checkout@v2
29
+
30
+ - name: Bundle
31
+ run: bundle install
32
+
33
+ - name: Run Steep
34
+ run: bundle exec rake steep:check
@@ -0,0 +1,134 @@
1
+ name: Package
2
+ on:
3
+ - push
4
+
5
+ jobs:
6
+ test-rake-build:
7
+ strategy:
8
+ fail-fast: false
9
+ matrix:
10
+ os: [ubuntu-24.04]
11
+ ruby: ["3.3"]
12
+ arch: [amd64, arm64]
13
+ libc: [gnu]
14
+ include:
15
+ - arch: amd64
16
+ platform: x86_64-linux
17
+ - arch: arm64
18
+ platform: aarch64-linux
19
+
20
+ name: Test build without fetching libddwaf (Ruby ${{ matrix.ruby }}, ${{ matrix.arch }}, ${{ matrix.libc }})
21
+ runs-on: ${{ matrix.os }}
22
+
23
+ steps:
24
+ - name: Checkout
25
+ uses: actions/checkout@v4
26
+
27
+ - name: Build docker image
28
+ id: build-image
29
+ uses: ./.github/actions/docker-build-ruby
30
+ with:
31
+ ruby-version: ${{ matrix.ruby }}
32
+ arch: ${{ matrix.arch }}
33
+ libc: ${{ matrix.libc }}
34
+
35
+ - name: Bundle install
36
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle install
37
+
38
+ - name: Build gem
39
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle exec rake build
40
+
41
+ - name: List artifact files
42
+ run: find .
43
+ working-directory: pkg
44
+
45
+ - name: Install gem
46
+ run: ${{ steps.build-image.outputs.run-cmd }} gem install --verbose pkg/*.gem
47
+
48
+ - name: Run smoke test
49
+ run: ${{ steps.build-image.outputs.run-cmd }} ruby -e 'begin require "libddwaf"; rescue LoadError => e; puts e.message; else fail "loaded when it should not"; end'
50
+
51
+ test-rake-binary:
52
+ strategy:
53
+ fail-fast: false
54
+ matrix:
55
+ os: [ubuntu-24.04]
56
+ ruby: ["3.3", "9.4"]
57
+ arch: [amd64, arm64]
58
+ libc: [gnu, musl]
59
+ include:
60
+ - arch: amd64
61
+ platform: x86_64-linux:llvm
62
+ - arch: arm64
63
+ platform: aarch64-linux:llvm
64
+ - ruby: 3.3
65
+ jruby: false
66
+ - ruby: 9.4
67
+ jruby: true
68
+ exclude:
69
+ - ruby: 9.4
70
+ libc: musl
71
+
72
+ name: Test gem build (${{ matrix.jruby == true && 'Jruby' || 'Ruby'}} ${{ matrix.ruby }}, ${{ matrix.arch }}, ${{ matrix.libc }})
73
+ runs-on: ${{ matrix.os }}
74
+
75
+ steps:
76
+ - name: Checkout
77
+ uses: actions/checkout@v4
78
+
79
+ - name: Build docker image
80
+ id: build-image
81
+ uses: ./.github/actions/docker-build-ruby
82
+ with:
83
+ ruby-version: ${{ matrix.ruby }}
84
+ arch: ${{ matrix.arch }}
85
+ libc: ${{ matrix.libc }}
86
+ jruby: ${{ matrix.jruby }}
87
+
88
+ - name: Bundle install
89
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle install
90
+
91
+ - name: Build binary gem
92
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle exec rake binary[${{ matrix.platform }}]
93
+
94
+ - name: List artifact files
95
+ run: find .
96
+ working-directory: pkg
97
+
98
+ - name: Install gem
99
+ run: ${{ steps.build-image.outputs.run-cmd }} gem install --verbose pkg/*.gem
100
+
101
+ - name: Run smoke test
102
+ run: ${{ steps.build-image.outputs.run-cmd }} ruby -r 'libddwaf' -e 'p Datadog::AppSec::WAF::LibDDWAF.ddwaf_get_version'
103
+
104
+ test-rake-binary-on-darwin:
105
+ strategy:
106
+ fail-fast: false
107
+ matrix:
108
+ os: [macos-15, macos-15-large]
109
+ include:
110
+ - os: macos-15
111
+ platform: arm64-darwin
112
+ - os: macos-15-large
113
+ platform: x86_64-darwin
114
+
115
+ name: Test gem build (${{ matrix.os }})
116
+ runs-on: ${{ matrix.os }}
117
+ env:
118
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
119
+
120
+ steps:
121
+ - name: Checkout
122
+ uses: actions/checkout@v4
123
+
124
+ - name: Bundle
125
+ run: bundle install
126
+
127
+ - name: Build binary gem
128
+ run: bundle exec rake binary[${{ matrix.platform }}]
129
+
130
+ - name: Install gem
131
+ run: gem install --verbose pkg/*.gem
132
+
133
+ - name: Run smoke test
134
+ run: ruby -r 'libddwaf' -e 'p Datadog::AppSec::WAF::LibDDWAF.ddwaf_get_version'
@@ -0,0 +1,118 @@
1
+ name: Test
2
+ on:
3
+ - push
4
+
5
+ jobs:
6
+ test-cruby-linux:
7
+ strategy:
8
+ fail-fast: false
9
+ matrix:
10
+ os: [ubuntu-24.04]
11
+ ruby: ["2.5", "2.6", "2.7", "3.0", "3.1", "3.2", "3.3"]
12
+ arch: [amd64, arm64]
13
+ libc: [gnu, musl]
14
+ include:
15
+ - arch: amd64
16
+ platform: x86_64-linux
17
+ - arch: arm64
18
+ platform: aarch64-linux
19
+
20
+ name: Test (Ruby ${{ matrix.ruby }}, ${{ matrix.arch }}, ${{ matrix.libc }})
21
+ runs-on: ${{ matrix.os }}
22
+
23
+ steps:
24
+ - name: Checkout
25
+ uses: actions/checkout@v4
26
+
27
+ - name: Build docker image
28
+ id: build-image
29
+ uses: ./.github/actions/docker-build-ruby
30
+ with:
31
+ ruby-version: ${{ matrix.ruby }}
32
+ arch: ${{ matrix.arch }}
33
+ libc: ${{ matrix.libc }}
34
+
35
+ - name: Bundle install
36
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle install
37
+
38
+ - name: Fetch binary library
39
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle exec rake fetch[${{ matrix.platform }}]
40
+
41
+ - name: Extract binary library
42
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle exec rake extract[${{ matrix.platform }}]
43
+
44
+ - name: Run specs
45
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle exec rake spec
46
+
47
+ test-jruby-linux:
48
+ strategy:
49
+ fail-fast: false
50
+ matrix:
51
+ os: [ubuntu-24.04]
52
+ jruby: ["9.3", "9.4"]
53
+ arch: [amd64, arm64]
54
+ include:
55
+ - arch: amd64
56
+ platform: x86_64-linux
57
+ - arch: arm64
58
+ platform: aarch64-linux
59
+
60
+ name: Test (Jruby ${{ matrix.jruby }}, ${{ matrix.arch }})
61
+ runs-on: ${{ matrix.os }}
62
+
63
+ steps:
64
+ - name: Checkout
65
+ uses: actions/checkout@v4
66
+
67
+ - name: Build docker image
68
+ id: build-image
69
+ uses: ./.github/actions/docker-build-ruby
70
+ with:
71
+ ruby-version: ${{ matrix.jruby }}
72
+ jruby: true
73
+ arch: ${{ matrix.arch }}
74
+ libc: gnu
75
+
76
+ - name: Bundle install
77
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle install
78
+
79
+ - name: Fetch binary library
80
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle exec rake fetch[${{ matrix.platform }}]
81
+
82
+ - name: Extract binary library
83
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle exec rake extract[${{ matrix.platform }}]
84
+
85
+ - name: Run specs
86
+ run: ${{ steps.build-image.outputs.run-cmd }} bundle exec rake spec
87
+
88
+ test-darwin:
89
+ strategy:
90
+ fail-fast: false
91
+ matrix:
92
+ os: [macos-15, macos-15-large]
93
+ include:
94
+ - os: macos-15
95
+ platform: arm64-darwin
96
+ - os: macos-15-large
97
+ platform: x86_64-darwin
98
+
99
+ name: Test (${{ matrix.os }})
100
+ runs-on: ${{ matrix.os }}
101
+ env:
102
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
103
+
104
+ steps:
105
+ - name: Checkout
106
+ uses: actions/checkout@v4
107
+
108
+ - name: Bundle
109
+ run: bundle install
110
+
111
+ - name: Fetch binary library
112
+ run: bundle exec rake fetch[${{ matrix.platform }}]
113
+
114
+ - name: Extract binary library
115
+ run: bundle exec rake extract[${{ matrix.platform }}]
116
+
117
+ - name: Run specs
118
+ run: bundle exec rake spec
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.envrc
2
+ /vendor/bundle
3
+ /vendor/libddwaf
4
+ /pkg
5
+ /tmp
6
+ *.gem
7
+ *.vim
8
+ .ruby-version
9
+ .github-token
10
+ Gemfile.lock
data/.steepignore ADDED
@@ -0,0 +1,4 @@
1
+ ffi/library.rbs:36:45 "Type `::FFI::DataConverter` is generic but used as a non generic type"
2
+ ffi/struct.rbs:5:15 "Type application of `::FFI::Type::Mapped` doesn't satisfy the constraints"
3
+ ffi/struct.rbs:23:29 "Type application of `::FFI::Type::Mapped` doesn't satisfy the constraints"
4
+ ffi/auto_pointer.rbs:15:65 "Type application of `::FFI::AutoPointer::Releaser::_Proc` doesn't satisfy the constraints"
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,84 @@
1
+ # Contributing
2
+
3
+ Community contributions to the Datadog bindings to libddwaf for Ruby are welcome! See below for some basic guidelines.
4
+
5
+ ## Want to request a new feature?
6
+
7
+ Many great ideas for new features come from the community, and we'd be happy to consider yours!
8
+
9
+ To share your request, you can [open a Github issue](https://github.com/DataDog/libddwaf-rb/issues/new) with the details about what you'd like to see. At a minimum, please provide:
10
+
11
+ - The goal of the new feature
12
+ - A description of how it might be used or behave
13
+ - Links to any important resources (e.g. Github repos, websites, screenshots, specifications, diagrams)
14
+
15
+ Additionally, if you can, include:
16
+
17
+ - A description of how it could be accomplished
18
+ - Code snippets that might demonstrate its use or implementation
19
+ - Screenshots or mockups that visually demonstrate the feature
20
+ - Links to similar features that would serve as a good comparison
21
+ - (Any other details that would be useful for implementing this feature!)
22
+
23
+ Feature requests will be reviewed and discussed.
24
+
25
+ ## Found a bug?
26
+
27
+ For any urgent matters (such as outages) or issues concerning the Datadog service or UI, contact our support team via https://docs.datadoghq.com/help/ for direct, faster assistance.
28
+
29
+ You may submit bug reports concerning the Datadog bindings to libddwaf for Ruby by [opening a Github issue](https://github.com/DataDog/libddwaf-rb/issues/new). At a minimum, please provide:
30
+
31
+ - A description of the problem
32
+ - Steps to reproduce
33
+ - Expected behavior
34
+ - Actual behavior
35
+ - Errors (with stack traces) or warnings received
36
+ - Any details you can share about your configuration including:
37
+ - Ruby version & platform
38
+ - `libddwaf` version
39
+ - Versions of any other relevant gems (or a `Gemfile.lock` if available)
40
+
41
+ If at all possible, also provide:
42
+
43
+ - Logs from the application or other diagnostics
44
+ - Screenshots, links, or other visual aids that are publicly accessible
45
+ - Code sample or test that reproduces the problem
46
+ - An explanation of what causes the bug and/or how it can be fixed
47
+
48
+ Reports that include rich detail are better, and ones with code that reproduce the bug are best. Bug requests will be triaged and reviewed by our collaborators.
49
+
50
+ ## Have a patch?
51
+
52
+ We welcome code contributions to the library, which you can [submit as a pull request](https://github.com/DataDog/libddwaf-rb/pull/new/master). To create a pull request:
53
+
54
+ 1. **Fork the repository** from https://github.com/DataDog/libddwaf-rb
55
+ 2. **Make any changes** for your patch.
56
+ 3. **Write tests** that demonstrate how the feature works or how the bug is fixed.
57
+ 4. **Update any documentation** especially for new features.
58
+ 5. **Submit the pull request** from your fork back to the latest revision of the `master` branch on https://github.com/DataDog/libddwaf-rb.
59
+
60
+ The pull request will be run through our CI pipeline, and a project member will review the changes with you. At a minimum, to be accepted and merged, pull requests must:
61
+
62
+ - Have a stated goal and detailed description of the changes made
63
+ - Include thorough test coverage and documentation, where applicable
64
+ - Pass all tests and code quality checks (linting/coverage) on CI
65
+ - Receive at least one approval from a project member with push permissions
66
+
67
+ We also recommend that you share in your description:
68
+
69
+ - Any motivations or intent for the contribution
70
+ - Links to any issues/pull requests it might be related to
71
+ - Links to any webpages or other external resources that might be related to the change
72
+ - Screenshots, code samples, or other visual aids that demonstrate the changes or how they are implemented
73
+ - Benchmarks if the feature is anticipated to have performance implications
74
+ - Any limitations, constraints or risks that are important to consider
75
+
76
+ Pull requests will be reviewed by our collaborators.
77
+
78
+ For more information on common topics such as debugging locally, or how to write new integrations, check out [our development guide](https://github.com/DataDog/libddwaf-rb/blob/master/README.md#development). If at any point you have a question or need assistance with your pull request, feel free to mention a project member! We're always happy to help contributors with their pull requests.
79
+
80
+ ## Final word
81
+
82
+ Many thanks to all of our contributors, and looking forward to seeing you on Github! :tada:
83
+
84
+ - Datadog Ruby Team
data/Steepfile ADDED
@@ -0,0 +1,21 @@
1
+ # D = Steep::Diagnostic
2
+
3
+ target :lib do
4
+ signature "sig"
5
+
6
+ check "lib"
7
+ library "logger"
8
+ library "monitor" # needed by logger
9
+ library "json"
10
+
11
+ repo_path "vendor/rbs"
12
+ library "ffi"
13
+ library "jruby"
14
+ library "gem"
15
+
16
+ # # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
17
+ # # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
18
+ # # configure_code_diagnostics do |hash| # You can setup everything yourself
19
+ # # hash[D::Ruby::NoMethod] = :information
20
+ # # end
21
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module AppSec
5
+ module WAF
6
+ # Ruby representation of the ddwaf_context in libddwaf
7
+ # See https://github.com/DataDog/libddwaf/blob/10e3a1dfc7bc9bb8ab11a09a9f8b6b339eaf3271/BINDING_IMPL_NOTES.md?plain=1#L125-L158
8
+ class Context
9
+ RESULT_CODE = {
10
+ ddwaf_ok: :ok,
11
+ ddwaf_match: :match,
12
+ ddwaf_err_internal: :err_internal,
13
+ ddwaf_err_invalid_object: :err_invalid_object,
14
+ ddwaf_err_invalid_argument: :err_invalid_argument
15
+ }.freeze
16
+
17
+ attr_reader :context_obj
18
+
19
+ def initialize(handle)
20
+ handle_obj = handle.handle_obj
21
+ retain(handle)
22
+
23
+ @context_obj = LibDDWAF.ddwaf_context_init(handle_obj)
24
+ raise LibDDWAF::Error, 'Could not create context' if @context_obj.null?
25
+
26
+ validate!
27
+ end
28
+
29
+ def finalize
30
+ invalidate!
31
+
32
+ retained.each do |retained_obj|
33
+ next unless retained_obj.is_a?(LibDDWAF::Object)
34
+
35
+ LibDDWAF.ddwaf_object_free(retained_obj)
36
+ end
37
+
38
+ LibDDWAF.ddwaf_context_destroy(context_obj)
39
+ end
40
+
41
+ def run(persistent_data, ephemeral_data, timeout = LibDDWAF::DDWAF_RUN_TIMEOUT)
42
+ valid!
43
+
44
+ persistent_data_obj = Converter.ruby_to_object(
45
+ persistent_data,
46
+ max_container_size: LibDDWAF::DDWAF_MAX_CONTAINER_SIZE,
47
+ max_container_depth: LibDDWAF::DDWAF_MAX_CONTAINER_DEPTH,
48
+ max_string_length: LibDDWAF::DDWAF_MAX_STRING_LENGTH,
49
+ coerce: false
50
+ )
51
+ if persistent_data_obj.null?
52
+ raise LibDDWAF::Error, "Could not convert persistent data: #{persistent_data.inspect}"
53
+ end
54
+
55
+ # retain C objects in memory for subsequent calls to run
56
+ retain(persistent_data_obj)
57
+
58
+ ephemeral_data_obj = Converter.ruby_to_object(
59
+ ephemeral_data,
60
+ max_container_size: LibDDWAF::DDWAF_MAX_CONTAINER_SIZE,
61
+ max_container_depth: LibDDWAF::DDWAF_MAX_CONTAINER_DEPTH,
62
+ max_string_length: LibDDWAF::DDWAF_MAX_STRING_LENGTH,
63
+ coerce: false
64
+ )
65
+ if ephemeral_data_obj.null?
66
+ raise LibDDWAF::Error, "Could not convert ephemeral data: #{ephemeral_data.inspect}"
67
+ end
68
+
69
+ result_obj = LibDDWAF::Result.new
70
+ raise LibDDWAF::Error, 'Could not create result object' if result_obj.null?
71
+
72
+ code = LibDDWAF.ddwaf_run(@context_obj, persistent_data_obj, ephemeral_data_obj, result_obj, timeout)
73
+
74
+ result = Result.new(
75
+ RESULT_CODE[code],
76
+ Converter.object_to_ruby(result_obj[:events]),
77
+ result_obj[:total_runtime],
78
+ result_obj[:timeout],
79
+ Converter.object_to_ruby(result_obj[:actions]),
80
+ Converter.object_to_ruby(result_obj[:derivatives])
81
+ )
82
+
83
+ [RESULT_CODE[code], result]
84
+ ensure
85
+ LibDDWAF.ddwaf_result_free(result_obj) if result_obj
86
+ end
87
+
88
+ private
89
+
90
+ def validate!
91
+ @valid = true
92
+ end
93
+
94
+ def invalidate!
95
+ @valid = false
96
+ end
97
+
98
+ def valid?
99
+ @valid
100
+ end
101
+
102
+ def valid!
103
+ return if valid?
104
+
105
+ raise LibDDWAF::Error, "Attempt to use an invalid instance: #{inspect}"
106
+ end
107
+
108
+ def retained
109
+ @retained ||= []
110
+ end
111
+
112
+ def retain(object)
113
+ retained << object
114
+ end
115
+
116
+ def release(object)
117
+ retained.delete(object)
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end