Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion g4/SecLangLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ WS
;

COMMENT
: ('#' .*? '\r'? '\n')+ '\n'?
: '#' ~[\r\n]* ('\r'? '\n' | EOF)
;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is a good idea to move COMMENT to a new mode COMMENT_MODE, triggered by something like COMMENT_START: '#' ' '?. Note that this change implies that COMMENT_START should be placed before the "comment" event in the parser file.


SPACE
Expand Down
10 changes: 10 additions & 0 deletions listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ParserResult struct {
rangeEvents []string
rangeStartEvents []int
rangeEndEvents []int
comments []string
}

type TreeShapeListener struct {
Expand Down Expand Up @@ -156,3 +157,12 @@ func (l *TreeShapeListener) EnterOperator_value(ctx *parser.Operator_valueContex
func (l *TreeShapeListener) EnterOperator_not(ctx *parser.Operator_notContext) {
l.results.negatedOperatorCount++
}

func (l *TreeShapeListener) EnterComment(ctx *parser.CommentContext) {
commentText := ctx.GetText()
// Remove the "#" from the beginning of the comment
if len(commentText) > 0 && commentText[0] == '#' {
commentText = commentText[1:]
}
l.results.comments = append(l.results.comments, commentText)
}
5,027 changes: 2,512 additions & 2,515 deletions parser/seclang_lexer.go

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ var genericTests = map[string]struct {
0,
"",
},
"testdata/test_40_comments.conf": {
0,
"Test comment processing with Pineapple Pizza",
},
}

var checkOutputTests = map[string]struct {
Expand All @@ -265,6 +269,28 @@ var checkOutputTests = map[string]struct {
rangeEndEvents: []int{9010},
},
},
"testdata/test_40_comments.conf": {
0,
"Test comment processing with Pineapple Pizza",
ParserResult{
comments: []string{
" This is a comment about Pineapple Pizza\n",
" Another comment mentioning Pineapple Pizza\n",
" Pineapple Pizza is controversial\n",
" Comment with multiple lines\n",
" Line 1: Pineapple Pizza\n",
" Line 2: Some people love it\n",
" Line 3: Others hate it\n",
" Comment with special characters: Pineapple Pizza!@#$%^&*()\n",
" Comment with numbers: Pineapple Pizza 12345\n",
" Comment with quotes: \"Pineapple Pizza\"\n",
" Empty comment above\n",
" Comment with tabs:\tPineapple Pizza\twith\ttabs\n",
" Comment with spaces: Pineapple Pizza with spaces\n",
" Comment ending with Pineapple Pizza\n",
},
},
},
"testdata/test_38_update_rules.conf": {
0,
"",
Expand All @@ -276,6 +302,7 @@ var checkOutputTests = map[string]struct {
directiveList: []string{"SecRuleUpdateTargetById", "SecRuleUpdateTargetById", "SecRuleUpdateTargetById", "SecRuleUpdateTargetById",
"SecRuleUpdateTargetByTag", "SecRuleUpdateTargetByMsg"},
directiveValues: []string{"12345", "958895", "981172", "958895", "WASCTC/WASC-31", "System Command Injection"},
comments: []string{" Test comment\n"},
},
},
"testdata/test_40_var_operators.conf": {
Expand All @@ -294,6 +321,7 @@ var checkOutputTests = map[string]struct {
rangeEvents: []string{"42-59", "65-90", "97-122"},
rangeStartEvents: []int{42, 65, 97},
rangeEndEvents: []int{59, 90, 122},
comments: []string{"\n", " -=[ Default setup values ]=-\n", "\n", " The CRS checks the tx.crs_setup_version variable to ensure that the setup\n", " file is included at the correct time. This detects situations where\n", " necessary settings are not defined, for instance if the file\n", " inclusion order is incorrect, or if the user has forgotten to\n", " include the crs-setup.conf file.\n", "\n", " If you are upgrading from an earlier version of the CRS and you are\n", " getting this error, please make a new copy of the setup template\n", " crs-setup.conf.example to crs-setup.conf, and re-apply your policy\n", " changes. There have been many changes in settings syntax from CRS2\n", " to CRS3, so an old setup file may cause unwanted behavior.\n", "\n", " If you are not planning to use the crs-setup.conf template, you must\n", " manually set the tx.crs_setup_version variable before including\n", " the CRS rules/* files.\n", "\n", " The variable is a numerical representation of the CRS version number.\n", " E.g., v3.0.0 is represented as 300.\n", "\n"},
},
},
"testdata/test_41_negated_operator_0.conf": {
Expand Down Expand Up @@ -329,6 +357,7 @@ var checkOutputTests = map[string]struct {
operatorValueList: []string{"(?:URLENCODED|MULTIPART|XML|JSON)", "1", "(?:URLENCODED|MULTIPART|XML|JSON)", "%{tx.sampling_percentage}"},
negatedOperatorCount: 3,
directiveList: []string{"SecRule", "SecRule", "SecRule", "SecRule"},
comments: []string{" Force body variable\n", " Force body processor URLENCODED\n"},
},
},
}
Expand Down
4,248 changes: 2,122 additions & 2,126 deletions src/seclang_parser/SecLangLexer.py

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src/seclang_parser/SecLangParser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Copyright 2025 OWASP CRS Project
# SPDX-License-Identifier: Apache-2.0

# Generated from SecLangParser.g4 by ANTLR 4.13.2
# encoding: utf-8
from antlr4 import *
Expand Down
3 changes: 0 additions & 3 deletions src/seclang_parser/SecLangParserListener.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Copyright 2025 OWASP CRS Project
# SPDX-License-Identifier: Apache-2.0

# Generated from SecLangParser.g4 by ANTLR 4.13.2
from antlr4 import *
if "." in __name__:
Expand Down
19 changes: 19 additions & 0 deletions testdata/test_40_comments.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This is a comment about Pineapple Pizza
# Another comment mentioning Pineapple Pizza
# Pineapple Pizza is controversial

# Comment with multiple lines
# Line 1: Pineapple Pizza
# Line 2: Some people love it
# Line 3: Others hate it

# Comment with special characters: Pineapple Pizza!@#$%^&*()
# Comment with numbers: Pineapple Pizza 12345
# Comment with quotes: "Pineapple Pizza"

# Empty comment above

# Comment with tabs: Pineapple Pizza with tabs
# Comment with spaces: Pineapple Pizza with spaces

# Comment ending with Pineapple Pizza