Skip to content

Commit a1b5af2

Browse files
committed
Disallow reading hidden files
1 parent 50165ab commit a1b5af2

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/HTTPServer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,10 @@ void HTTPServer::sendStatusResponse(Client* cl, int status, std::string msg) {
621621
resp->setStatus(Status(status));
622622

623623
// Body message: Reason string + additional msg
624-
std::string body = resp->getReason() + ": " + msg;
624+
std::string body = resp->getReason();
625+
if (msg.length() > 0)
626+
body += ": " + msg;
627+
625628
unsigned int slen = body.length();
626629
char* sdata = new char[slen];
627630
bzero(sdata, slen);

src/Resource.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ class Resource {
6969
return size;
7070
}
7171

72+
// Get the file name
73+
std::string getName() {
74+
std::string name = "";
75+
size_t slash_pos = location.find_last_of("/");
76+
if (slash_pos != std::string::npos)
77+
name = location.substr(slash_pos + 1);
78+
return name;
79+
}
80+
7281
// Get the file extension
7382
std::string getExtension() {
7483
std::string ext = "";

src/ResourceHost.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ Resource* ResourceHost::readFile(std::string path, struct stat sb) {
6666
return NULL;
6767

6868
// Get the length of the file
69-
/*file.seekg(0, std::ios::end);
70-
len = file.tellg();
71-
file.seekg(0, std::ios::beg);*/
7269
len = sb.st_size;
7370

7471
// Allocate memory for contents of file and read in the contents
@@ -81,6 +78,15 @@ Resource* ResourceHost::readFile(std::string path, struct stat sb) {
8178

8279
// Create a new Resource object and setup it's contents
8380
Resource* res = new Resource(path);
81+
std::string name = res->getName();
82+
if (name.length() == 0)
83+
return NULL; // Malformed name
84+
85+
// Always disallow hidden files
86+
if (name.c_str()[0] == '.') {
87+
return NULL;
88+
}
89+
8490
std::string mimetype = lookupMimeType(res->getExtension());
8591
if (mimetype.length() != 0)
8692
res->setMimeType(mimetype);

0 commit comments

Comments
 (0)