Skip to content

Commit 0e05b7b

Browse files
author
Felipe Zimmerle
committed
Avoids to load a directory structure as a rules file
1 parent c97db2f commit 0e05b7b

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/parser/driver.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ int Driver::parseFile(const std::string &f) {
157157
std::ifstream t(f);
158158
std::string str;
159159

160-
if (t.is_open() == false) {
160+
if (utils::isFile(f) == false) {
161161
m_parserError << "Failed to open the file: " << f << std::endl;
162162
return false;
163163
}

src/utils/system.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,19 @@ bool createDir(std::string dir, int mode, std::string *error) {
144144
}
145145

146146

147+
bool isFile(std::string f) {
148+
struct stat fileInfo;
149+
FILE *fp = fopen(f.c_str(), "r");
150+
fstat(fileno(fp), &fileInfo);
151+
if (!S_ISREG(fileInfo.st_mode)) {
152+
fclose(fp);
153+
return false;
154+
}
155+
fclose(fp);
156+
157+
return true;
158+
}
159+
160+
147161
} // namespace utils
148162
} // namespace modsecurity

src/utils/system.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ std::string find_resource(const std::string& file, const std::string& param,
3434
std::string get_path(const std::string& file);
3535
std::list<std::string> expandEnv(const std::string& var, int flags);
3636
bool createDir(std::string dir, int mode, std::string *error);
37-
37+
bool isFile(std::string f);
3838

3939
} // namespace utils
4040
} // namespace modsecurity

0 commit comments

Comments
 (0)