1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Unless explicitly stated otherwise all files in this repository are
// dual-licensed under the Apache-2.0 License or BSD-3-Clause License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2021 Datadog, Inc.
#include <cstddef>
#include <cstdint>
#include <utility>
#include "cow_string.hpp"
#include "transformer/remove_comments.hpp"
namespace ddwaf::transformer {
bool remove_comments::transform_impl(cow_string &str)
{
enum class comment_type : uint8_t { unknown, html, c, eol } type = comment_type::unknown;
bool comment_found = false;
std::size_t read = 0;
std::size_t write = 0;
while (read < str.length()) {
type = comment_type::unknown;
while (read < str.length()) {
if (str.at(read) == '<' && read + 3 < str.length() && str.at(read + 1) == '!' &&
str.at(read + 2) == '-' && str.at(read + 3) == '-') {
read += 4;
type = comment_type::html;
break;
}
if (str.at(read) == '-' && read + 1 < str.length() && str.at(read + 1) == '-') {
// Don't bother updating the read index since we'll exit anyway
type = comment_type::eol; // SQL comment
break;
}
if (str.at(read) == '#') {
// Don't bother updating the read index since we'll exit anyway
type = comment_type::eol; // Shell comment
break;
}
if (str.at(read) == '/' && read + 1 < str.length()) {
if (str.at(read + 1) == '*') {
read += 2;
type = comment_type::c;
break;
}
if (str.at(read + 1) == '/') {
// Don't bother updating the read index since we'll exit anyway
type = comment_type::eol; // C/C++ end of line comment
break;
}
}
if (comment_found) {
str[write] = str.at(read);
}
++write;
++read;
}
if (type == comment_type::unknown || type == comment_type::eol) {
break;
}
comment_found = true;
while (read < str.length()) {
std::pair<bool, std::size_t> result;
// NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
if (type == comment_type::html && (result = str.find('-', read)).first) {
read = result.second;
if (read + 2 < str.length() && str.at(read + 1) == '-' && str.at(read + 2) == '>') {
read += 3;
break;
}
++read;
// NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
} else if (type == comment_type::c && (result = str.find('*', read)).first) {
read = result.second;
if (read + 1 < str.length() && str.at(read + 1) == '/') {
read += 2;
break;
}
++read;
} else {
// If we reach this point we have found no comment terminator
// so we set read to str.length() in order to exit.
read = str.length();
break;
}
}
}
if (!comment_found && type == comment_type::unknown) {
return false;
}
str.truncate(write);
return true;
}
} // namespace ddwaf::transformer