Skip to content

Commit 82d043c

Browse files
authored
Add support for all ddwaf_object types in from_ddwaf_object (#145)
1 parent 0a6971f commit 82d043c

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/convert.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,21 @@ Napi::Value from_ddwaf_object(const ddwaf_object *object, Napi::Env env) {
250250
Napi::Value result;
251251

252252
switch (type) {
253+
case DDWAF_OBJ_NULL:
254+
result = env.Null();
255+
break;
256+
case DDWAF_OBJ_BOOL:
257+
result = Napi::Boolean::New(env, object->boolean);
258+
break;
253259
case DDWAF_OBJ_SIGNED:
254260
result = Napi::Number::New(env, object->intValue);
255261
break;
256262
case DDWAF_OBJ_UNSIGNED:
257263
result = Napi::Number::New(env, object->uintValue);
258264
break;
265+
case DDWAF_OBJ_FLOAT:
266+
result = Napi::Number::New(env, object->f64);
267+
break;
259268
case DDWAF_OBJ_STRING:
260269
result = Napi::String::New(env, object->stringValue, object->nbEntries);
261270
break;

test/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('DDWAF', () => {
4242
'value_matchall',
4343
'key_matchall',
4444
'custom_action_rule',
45+
'test-marshalling',
4546
'long_rule'
4647
],
4748
skipped: [],
@@ -417,6 +418,7 @@ describe('DDWAF', () => {
417418
'value_matchall',
418419
'key_matchall',
419420
'custom_action_rule',
421+
'test-marshalling',
420422
'long_rule'
421423
],
422424
failed: ['invalid_1', 'invalid_2', 'invalid_3'],
@@ -970,6 +972,32 @@ describe('DDWAF', () => {
970972
waf.dispose()
971973
})
972974

975+
it('should perform correct marshalling of ddwaf_object', () => {
976+
const waf = new DDWAF(rules, 'recommended')
977+
const context = waf.createContext()
978+
const result = context.run({
979+
persistent: {
980+
'server.request.headers.no_cookies': 'marshalling'
981+
}
982+
}, TIMEOUT)
983+
984+
assert.strictEqual(result.timeout, false)
985+
assert.strictEqual(result.status, 'match')
986+
assert.strictEqual(result.keep, true)
987+
assert.strictEqual(result.attributes['_dd.appsec.trace.integer'], 662607015)
988+
assert.strictEqual(result.attributes['_dd.appsec.trace.negative_integer'], -662607015)
989+
assert.strictEqual(result.attributes['_dd.appsec.trace.float'], 2.71828)
990+
assert.strictEqual(result.attributes['_dd.appsec.trace.negative_float'], -3.14159)
991+
assert.strictEqual(result.attributes['_dd.appsec.trace.bool'], true)
992+
assert.strictEqual(
993+
result.attributes['_dd.appsec.trace.string'],
994+
'It was a bright cold day in April, and the clocks were striking thirteen.'
995+
)
996+
997+
context.dispose()
998+
waf.dispose()
999+
})
1000+
9731001
describe('Action semantics', () => {
9741002
it('should support action definition in initialisation', () => {
9751003
const waf = new DDWAF(rules, 'recommended')

test/rules.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,54 @@
213213
"customblock"
214214
]
215215
},
216+
{
217+
"id": "test-marshalling",
218+
"name": "Test ddwaf_object marshalling",
219+
"tags": {
220+
"type": "security_scanner",
221+
"category": "attack_attempt"
222+
},
223+
"conditions": [
224+
{
225+
"parameters": {
226+
"inputs": [
227+
{
228+
"address": "server.request.headers.no_cookies"
229+
}
230+
],
231+
"list": [
232+
"marshalling"
233+
]
234+
},
235+
"operator": "phrase_match"
236+
}
237+
],
238+
"output": {
239+
"event": false,
240+
"keep": true,
241+
"attributes": {
242+
"_dd.appsec.trace.string": {
243+
"value": "It was a bright cold day in April, and the clocks were striking thirteen."
244+
},
245+
"_dd.appsec.trace.integer": {
246+
"value": 662607015
247+
},
248+
"_dd.appsec.trace.negative_integer": {
249+
"value": -662607015
250+
},
251+
"_dd.appsec.trace.float": {
252+
"value": 2.71828
253+
},
254+
"_dd.appsec.trace.negative_float": {
255+
"value": -3.14159
256+
},
257+
"_dd.appsec.trace.bool": {
258+
"value": true
259+
}
260+
}
261+
},
262+
"on_match": []
263+
},
216264
{
217265
"id": "long_rule",
218266
"name": "long rule that shouldn't be truncated",

0 commit comments

Comments
 (0)