secrules_parsing icon indicating copy to clipboard operation
secrules_parsing copied to clipboard

fix: add more data for debug (context)

Open touchweb-vincent opened this issue 2 months ago • 6 comments

Hi

I added some debug output to include the content of e.context, which comes from the TextX library in Python.

Currently, we only have the line and column numbers, along with a very brief message about the expected format.

This addition should provide more context and make troubleshooting easier when exceptions occur.

touchweb-vincent avatar Oct 31 '25 08:10 touchweb-vincent

I honestly don’t know what else to say - it’s just additional debugging. It seems to be something native to TextX, which I’m not familiar with, as I don’t know the Python ecosystem.

touchweb-vincent avatar Oct 31 '25 18:10 touchweb-vincent

I honestly don’t know what else to say - it’s just additional debugging.

Then you should add that: the PR adds more information and helps debugging. And you can explain why did you think that the context is necessary.

airween avatar Oct 31 '25 18:10 airween

You're right, I’ve updated the PR accordingly.

touchweb-vincent avatar Nov 01 '25 07:11 touchweb-vincent

You're right, I’ve updated the PR accordingly.

Thanks, I think now it's fine.

But I tried your modification with a real example (I made a rule with wrong syntax and tried to parse that), and unfortunately I got this:

arpeggio.NoMatch: Expected 'accuracy:' ...

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/airween/src/secrules_parsing/src/secrules_parsing/parser.py", line 36, in process_rules
    model = modsec_mm.model_from_file(rule_file)
    ...

textx.exceptions.TextXSyntaxError: None:9:58: error: Expected 'accuracy:' ...

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/airween/src/secrules_parsing/src/secrules_parsing/cli.py", line 90, in run
    models = process_rules(args.files, args.verbose, args.debug)
  File "/home/airween/src/secrules_parsing/src/secrules_parsing/parser.py", line 43, in process_rules
    "context": e.context,
AttributeError: 'TextXSyntaxError' object has no attribute 'context'

Is there any version dependency for textx?

airween avatar Nov 01 '25 08:11 airween

I did a standard install - I don’t remember running any exotic commands.

pip show textx

Name: textX Version: 3.1.1 Summary: Meta-language for DSL implementation inspired by Xtext Home-page: https://github.com/textX/textX Author: Igor R. Dejanovic Author-email: [email protected] License: MIT Location: /.venv/lib/python3.11/site-packages Requires: Arpeggio Required-by: secrules-parsing

pip index versions textx

textx (4.2.3) Available versions: 4.2.3, 4.2.2, 4.2.1, 4.2.0, 4.1.0, 4.0.1, 4.0.0, 3.1.1, 3.1.0, 3.0.0, 2.3.0, 2.2.0, 2.1.0, 2.0.1, 1.8.0, 1.7.1, 1.7.0, 1.6.1, 1.6, 1.5.2, 1.5.1, 1.5, 1.4, 1.3.1, 1.3, 1.2, 1.1.1, 1.1, 1.0, 0.4.2, 0.4.1, 0.4, 0.3.1, 0.3, 0.2.1, 0.2, 0.1.2, 0.1.1 INSTALLED: 3.1.1 LATEST: 4.2.3

Can you show me what you tested, please? I’ll probably get the same error.

touchweb-vincent avatar Nov 02 '25 10:11 touchweb-vincent

I did a standard install - I don’t remember running any exotic commands.

pip show textx

Name: textX Version: 3.1.1

uhmm, it seems like I have (had) an old version:

$ pip3 show textx
Name: textX
Version: 2.3.0
...

Now I upgraded and have this one:

$ pip3 show textx
Name: textx
Version: 4.2.3
...

but the error is still exists.

Can you show me what you tested, please? I’ll probably get the same error.

Sure:

$ cat fpplug_3.conf 

SecRule TX:FALSE-POSITIVE-REPORT-PLUGIN_FILTER_IP "@gt 0" \
    "id:9525140,\
    phase:5,\
    pass,\
    t:none,t:length,\
    nolog,\
    ver:'false-positive-report-plugin/1.0.0',\
    setvar:'tx.false-positive-report-plugin_remote_addr=,%{remote_addr},',\
    skipAfter:FALSE-POSITIVE-REPORT-PLUGIN-END,\
    chain"
    SecRule TX:FALSE-POSITIVE-REPORT-PLUGIN_REMOTE_ADDR "!@within ,%{tx.false-positive-report-plugin_filter_ip}," \
        "t:none"

$ poetry run secrules-parser -c --output-type github -f fpplug_3.conf
...
Traceback (most recent call last):
  File "/home/airween/.cache/pypoetry/virtualenvs/secrules-parsing-UNC6XTUb-py3.9/lib/python3.9/site-packages/textx/model.py", line 291, in _parse
    return self.parser_model.parse(self)
...
arpeggio.NoMatch: Expected 'accuracy:' or 'block' or 'accept' or 'deny' or 'drop' or 'pass' or 'pause' or 'append' or 'auditlog' or 'capture' or 'chain' or 'ctl:' or 'deprecatevar:' or 'exec:' or 'expirevar:' or 'id:' or 'initcol:' or 'logdata:' or 'log' or 'maturity:' or 'msg:' or 'multiMatch' or 'noauditlog' or 'nolog' or 'phase:' or 'prepend:' or 'proxy:' or 'redirect:' or 'rev:' or 'sanitiseArg:' or 'sanitiseMatched' or 'sanitiseMatchedBytes:' or 'sanitiseRequestHeader:' or 'sanitiseResponseHeader:' or 'severity:' or 'setuid:' or 'setrsc:' or 'setsid:' or 'setenv:' or 'setvar:' or 'skip:' or 'skipAfter:' or 'status:' or 't:' or 'tag:'' or 'ver:'' or 'xmlns:' at position /home/airween/src/secrules_parsing/fpplug_3.conf:(9, 58) => 'ote_addr=,*%{remote_a'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/airween/src/secrules_parsing/src/secrules_parsing/parser.py", line 36, in process_rules
    model = modsec_mm.model_from_file(rule_file)
...
textx.exceptions.TextXSyntaxError: None:9:58: error: Expected 'accuracy:' or 'block' or 'accept' or 'deny' or 'drop' or 'pass' or 'pause' or 'append' or 'auditlog' or 'capture' or 'chain' or 'ctl:' or 'deprecatevar:' or 'exec:' or 'expirevar:' or 'id:' or 'initcol:' or 'logdata:' or 'log' or 'maturity:' or 'msg:' or 'multiMatch' or 'noauditlog' or 'nolog' or 'phase:' or 'prepend:' or 'proxy:' or 'redirect:' or 'rev:' or 'sanitiseArg:' or 'sanitiseMatched' or 'sanitiseMatchedBytes:' or 'sanitiseRequestHeader:' or 'sanitiseResponseHeader:' or 'severity:' or 'setuid:' or 'setrsc:' or 'setsid:' or 'setenv:' or 'setvar:' or 'skip:' or 'skipAfter:' or 'status:' or 't:' or 'tag:'' or 'ver:'' or 'xmlns:' at position /home/airween/src/secrules_parsing/fpplug_3.conf:(9, 58) => 'ote_addr=,*%{remote_a'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/airween/src/secrules_parsing/src/secrules_parsing/cli.py", line 90, in run
    models = process_rules(args.files, args.verbose, args.debug)
  File "/home/airween/src/secrules_parsing/src/secrules_parsing/parser.py", line 43, in process_rules
    "context": e.context,
AttributeError: 'TextXSyntaxError' object has no attribute 'context'

Note, that the file what I want to parse is wrong on purpose.

airween avatar Nov 03 '25 15:11 airween