Finds the plan file and returns its contents

This commit is contained in:
pmb
2025-06-24 15:58:30 -07:00
parent f71f1c9b11
commit e648b8e319
3 changed files with 143 additions and 86 deletions
+62 -20
View File
@@ -1,27 +1,69 @@
#include "handler.hpp" #include "handler.hpp"
#include <filesystem>
#include <fstream>
#include <string_view>
std::string process(const std::string &username){ const std::filesystem::path kPATH{"/var/finger/users/"};
try{
// Check for directory traversal patterns
if (username.find("../") != std::string::npos ||
username.find("..\\") != std::string::npos ||
username.find("%2e%2e%2f") != std::string::npos ||
username.find("%2e%2e%5c") != std::string::npos ||
username.find("%2E%2E%2F") != std::string::npos ||
username.find("%2E%2E%5C") != std::string::npos ||
username.find("..%2f") != std::string::npos ||
username.find("..%5c") != std::string::npos ||
username.find("..%2F") != std::string::npos ||
username.find("..%5C") != std::string::npos) {
throw InvalidInput("Directory traversal detected in username");
}
if (username.find("/") != std::string::npos){ std::string process(const std::string &username) {
throw InvalidInput("Path detected in username"); try {
} // Check for directory traversal patterns
if (username.find("../") != std::string::npos ||
username.find("..\\") != std::string::npos ||
username.find("%2e%2e%2f") != std::string::npos ||
username.find("%2e%2e%5c") != std::string::npos ||
username.find("%2E%2E%2F") != std::string::npos ||
username.find("%2E%2E%5C") != std::string::npos ||
username.find("..%2f") != std::string::npos ||
username.find("..%5c") != std::string::npos ||
username.find("..%2F") != std::string::npos ||
username.find("..%5C") != std::string::npos) {
throw InvalidInput("Directory traversal detected in username");
} }
catch(InvalidInput&e){
return std::string("InvalidInput: ") + e.what() + std::string("\r\n"); if (username.find("/") != std::string::npos) {
throw InvalidInput("Path detected in username");
} }
} catch (InvalidInput &e) {
return std::string("InvalidInput: ") + e.what() + std::string("\r\n");
}
// Attempt to open the plan file (if any) and return the contents as a string
std::filesystem::path planPath = kPATH / username;
// Check if the plan file exists
if (!std::filesystem::exists(planPath)) {
// If no plan file exists, return just the username
return username; return username;
}
// Try to read the plan file
try {
std::ifstream planFile(planPath);
if (!planFile.is_open()) {
return username;
}
std::string content;
std::string line;
while (std::getline(planFile, line)) {
content += line + "\n";
}
// Return the plan content with proper line endings
if (!content.empty() && content.back() == '\n') {
content.pop_back(); // Remove the last newline
content += "\r\n";
return content;
} else if (!content.empty()) {
content += "\r\n";
return content;
} else {
// If file exists but is empty, return just the username
return username;
}
} catch (...) {
// If there's any error reading the file, just return the username
return username;
}
} }
+20 -5
View File
@@ -22,18 +22,33 @@ namespace this_coro = boost::asio::this_coro;
awaitable<std::string> dofinger(const std::string &username) { awaitable<std::string> dofinger(const std::string &username) {
co_return process(username); co_return process(username);
//co_return std::string("this is some finger information for ") + username + std::string("\r\n"); // co_return std::string("this is some finger information for ") + username +
// std::string("\r\n");
} }
awaitable<void> echo(tcp::socket socket) { awaitable<void> echo(tcp::socket socket) {
try { try {
char data[1024]; char data[1024];
co_await socket.async_read_some(boost::asio::buffer(data), deferred); auto bytes_read =
auto response = co_await dofinger(std::string(data)); co_await socket.async_read_some(boost::asio::buffer(data), deferred);
// strip \r\n from data
std::string username(data, bytes_read);
// Remove trailing \r\n characters
while (!username.empty() &&
(username.back() == '\r' || username.back() == '\n')) {
username.pop_back();
}
auto response = co_await dofinger(username);
if (response.compare(std::string(username)) == 0) {
// No plan found
co_await async_write(
socket, boost::asio::buffer(std::string("No plan found\r\n")),
deferred);
co_return;
}
co_await async_write(socket, boost::asio::buffer(response), deferred); co_await async_write(socket, boost::asio::buffer(response), deferred);
co_return; co_return;
} } catch (std::exception &e) {
catch (std::exception &e) {
std::printf("echo Exception: %s\n", e.what()); std::printf("echo Exception: %s\n", e.what());
} }
} }
+61 -61
View File
@@ -4,138 +4,138 @@
// Test fixture for process function tests // Test fixture for process function tests
class ProcessTest : public ::testing::Test { class ProcessTest : public ::testing::Test {
protected: protected:
void SetUp() override {} void SetUp() override {}
void TearDown() override {} void TearDown() override {}
}; };
// Valid username tests // Valid username tests
TEST_F(ProcessTest, ValidUsernameSimple) { TEST_F(ProcessTest, ValidUsernameSimple) {
std::string result = process("john"); std::string result = process("john");
EXPECT_EQ(result, "john"); EXPECT_EQ(result, "john");
} }
TEST_F(ProcessTest, ValidUsernameWithNumbers) { TEST_F(ProcessTest, ValidUsernameWithNumbers) {
std::string result = process("user123"); std::string result = process("user123");
EXPECT_EQ(result, "user123"); EXPECT_EQ(result, "user123");
} }
TEST_F(ProcessTest, ValidUsernameWithUnderscore) { TEST_F(ProcessTest, ValidUsernameWithUnderscore) {
std::string result = process("user_name"); std::string result = process("user_name");
EXPECT_EQ(result, "user_name"); EXPECT_EQ(result, "user_name");
} }
TEST_F(ProcessTest, ValidUsernameWithHyphen) { TEST_F(ProcessTest, ValidUsernameWithHyphen) {
std::string result = process("user-name"); std::string result = process("user-name");
EXPECT_EQ(result, "user-name"); EXPECT_EQ(result, "user-name");
} }
TEST_F(ProcessTest, ValidUsernameEmptyString) { TEST_F(ProcessTest, ValidUsernameEmptyString) {
std::string result = process(""); std::string result = process("");
EXPECT_EQ(result, ""); EXPECT_EQ(result, "");
} }
// Directory traversal tests // Directory traversal tests
TEST_F(ProcessTest, DirectoryTraversalBasicDotDotSlash) { TEST_F(ProcessTest, DirectoryTraversalBasicDotDotSlash) {
std::string result = process("user../file"); std::string result = process("user../file");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
TEST_F(ProcessTest, DirectoryTraversalBasicDotDotBackslash) { TEST_F(ProcessTest, DirectoryTraversalBasicDotDotBackslash) {
std::string result = process("user..\\file"); std::string result = process("user..\\file");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
TEST_F(ProcessTest, DirectoryTraversalURLEncodedLowercase2e2e2f) { TEST_F(ProcessTest, DirectoryTraversalURLEncodedLowercase2e2e2f) {
std::string result = process("user%2e%2e%2ffile"); std::string result = process("user%2e%2e%2ffile");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
TEST_F(ProcessTest, DirectoryTraversalURLEncodedLowercase2e2e5c) { TEST_F(ProcessTest, DirectoryTraversalURLEncodedLowercase2e2e5c) {
std::string result = process("user%2e%2e%5cfile"); std::string result = process("user%2e%2e%5cfile");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
TEST_F(ProcessTest, DirectoryTraversalURLEncodedUppercase2E2E2F) { TEST_F(ProcessTest, DirectoryTraversalURLEncodedUppercase2E2E2F) {
std::string result = process("user%2E%2E%2Ffile"); std::string result = process("user%2E%2E%2Ffile");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
TEST_F(ProcessTest, DirectoryTraversalURLEncodedUppercase2E2E5C) { TEST_F(ProcessTest, DirectoryTraversalURLEncodedUppercase2E2E5C) {
std::string result = process("user%2E%2E%5Cfile"); std::string result = process("user%2E%2E%5Cfile");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
TEST_F(ProcessTest, DirectoryTraversalMixedDotDot2f) { TEST_F(ProcessTest, DirectoryTraversalMixedDotDot2f) {
std::string result = process("user..%2ffile"); std::string result = process("user..%2ffile");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
TEST_F(ProcessTest, DirectoryTraversalMixedDotDot5c) { TEST_F(ProcessTest, DirectoryTraversalMixedDotDot5c) {
std::string result = process("user..%5cfile"); std::string result = process("user..%5cfile");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
TEST_F(ProcessTest, DirectoryTraversalMixedDotDot2F) { TEST_F(ProcessTest, DirectoryTraversalMixedDotDot2F) {
std::string result = process("user..%2Ffile"); std::string result = process("user..%2Ffile");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
TEST_F(ProcessTest, DirectoryTraversalMixedDotDot5C) { TEST_F(ProcessTest, DirectoryTraversalMixedDotDot5C) {
std::string result = process("user..%5Cfile"); std::string result = process("user..%5Cfile");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos); EXPECT_TRUE(result.find("Directory traversal detected") != std::string::npos);
} }
// Path detection tests // Path detection tests
TEST_F(ProcessTest, PathDetectionForwardSlash) { TEST_F(ProcessTest, PathDetectionForwardSlash) {
std::string result = process("user/name"); std::string result = process("user/name");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Path detected") != std::string::npos); EXPECT_TRUE(result.find("Path detected") != std::string::npos);
} }
TEST_F(ProcessTest, PathDetectionForwardSlashAtStart) { TEST_F(ProcessTest, PathDetectionForwardSlashAtStart) {
std::string result = process("/username"); std::string result = process("/username");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Path detected") != std::string::npos); EXPECT_TRUE(result.find("Path detected") != std::string::npos);
} }
TEST_F(ProcessTest, PathDetectionForwardSlashAtEnd) { TEST_F(ProcessTest, PathDetectionForwardSlashAtEnd) {
std::string result = process("username/"); std::string result = process("username/");
EXPECT_TRUE(result.find("InvalidInput:") == 0); EXPECT_TRUE(result.find("InvalidInput:") == 0);
EXPECT_TRUE(result.find("Path detected") != std::string::npos); EXPECT_TRUE(result.find("Path detected") != std::string::npos);
} }
// Edge cases // Edge cases
TEST_F(ProcessTest, SingleDot) { TEST_F(ProcessTest, SingleDot) {
std::string result = process("."); std::string result = process(".");
EXPECT_EQ(result, "."); EXPECT_EQ(result, ".");
} }
TEST_F(ProcessTest, DoubleDotWithoutSlash) { TEST_F(ProcessTest, DoubleDotWithoutSlash) {
std::string result = process(".."); std::string result = process("..");
EXPECT_EQ(result, ".."); EXPECT_EQ(result, "..");
} }
TEST_F(ProcessTest, ContainsDotButNotTraversal) { TEST_F(ProcessTest, ContainsDotButNotTraversal) {
std::string result = process("user.name"); std::string result = process("user.name");
EXPECT_EQ(result, "user.name"); EXPECT_EQ(result, "user.name");
} }
TEST_F(ProcessTest, BackslashWithoutDots) { TEST_F(ProcessTest, BackslashWithoutDots) {
std::string result = process("user\\name"); std::string result = process("user\\name");
EXPECT_EQ(result, "user\\name"); EXPECT_EQ(result, "user\\name");
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }