-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver_org.js
More file actions
94 lines (75 loc) · 3.5 KB
/
Copy pathserver_org.js
File metadata and controls
94 lines (75 loc) · 3.5 KB
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
const http = require('http');
const fs = require('fs');
const url = require('url');
const pathutil = require("path");
const hostname = '127.0.0.1';
const port = 8080;
const server = http.createServer((rq, rs) => {
let query = url.parse(rq.url, true);
let data = query.query;
let filename = query.pathname;
let path = filename;
//Не даём никому никакие файлы кроме тех которые хотим отдать: из своей папки и тлко с расширением html
//см. https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/05-Authorization_Testing/01-Testing_Directory_Traversal_File_Include
//Поэтому проверим путь, который нам прислали
//1. Нормализуем путь, удалим оттуда все ненужные нам точки https://nodejs.org/api/path.html#pathnormalizepath
path = pathutil.normalize(path);
//2. Преобразуем относительный путь из адреса в путь относительно корня сайта https://nodejs.org/api/path.html#pathbasenamepath-ext
path = pathutil.basename(path);
//3. Проверим расширение https://nodejs.org/api/path.html#pathextnamepath
if(pathutil.extname(path) !== '.html'){
rs.writeHead(400);
rs.end('we host only html files');
return; //Выходим с ошибкой
}
if(filename === "/signin.html"){//Это страница проверки пароля
let login = data.name;//Имя пользователя со страницы логина
//прочитаем файл с паролями с диска и найдём в нём нашего пользователя
let users = fs.readFileSync('./users.txt', 'utf8');
console.log(users);
let account_lines = users.split(/\n/); //Строки файла, в каждой строке аккаунт - имя и пароль соединённые через; Проверьте концы строк: Open the command pallette (CTRL+SHIFT+P) and type "Change All End Of Line Sequence".
let is_user_exists = false;
let password_to_check = undefined;
for (let account_line of account_lines){
let account = account_line.split(';');
let account_name = account[0];
let account_pass = account[1];
if(login === account_name){
is_user_exists = true;
password_to_check = account_pass;
break; //Нашли пользователя, сохраним его пароль для последующей проверки
}
}
if(is_user_exists === false) //Не нашли пользователя
{
rs.writeHead(403);
rs.end('user does not exists');
return; //Выходим с ошибкой
}
if(data.pass === password_to_check)//Нашли пользователя, проверим пароль
{
rs.writeHead(200);
rs.end('SUCCESS'); //пароль верный
return;
} else { //пароль не совпал
rs.writeHead(403);
rs.end('wrong password');
return; //Выходим с ошибкой
}
}
//Это не проверка пароля, а просто ктото попрсил страницу, отдадим её
fs.readFile(path, (err, data) => {
if (err) {
console.error(err);
rs.writeHead(404);
rs.end();
return;
}
rs.statusCode = 200;
rs.setHeader('Content-Type', 'text/html');
rs.end(data);
});
});
server.listen(port, hostname, ()=>{
console.log(`server running at http://${hostname}:${port}`);
})