@@ -5,28 +5,29 @@ const serveIndex = require('serve-index')
55const serveStatic = require ( 'serve-static' )
66const cookie = require ( 'cookie' )
77
8- const config = require ( '../config' )
9- const { renderRequest } = require ( './markd' )
10- const { renderConfig, updateConfig } = require ( './renderConfig' )
8+ const config = require ( '../config.js ' )
9+ const { renderRequest } = require ( './markd.js ' )
10+ const { renderConfig, updateConfig } = require ( './renderConfig.js ' )
1111const {
1212 newError,
1313 homedir,
1414 drive,
1515 uri2filename,
1616 filename2uri,
1717 urlWithoutDrive
18- } = require ( './utils' )
18+ } = require ( './utils.js ' )
1919
2020const MARKDOWNEXT = / \. ( m d | m d o w n | m a r k d o w n ) $ /
2121const FILETEXT = / [ / \\ ] ( [ A - Z a - z ] + ) $ /
2222const FILENOEXT = / [ / \\ ] ( [ ^ . ] + ) $ /
23+ const FORBIDDEN_RE = / ^ \/ ( e t c | b i n | s b i n | u s r | b o o t | o p t | v a r | l i b | [ C c ] : \/ [ W w ] i n d o w s \/ [ S s ] y s t e m 3 2 ) ( \/ | $ ) /
2324
2425/**
2526 * middlewares
2627 */
2728const M = {
2829 // only allow access from localhost
29- forbiddenRemote : function ( req , res , next ) {
30+ forbiddenRemote : function ( req , _res , next ) {
3031 if (
3132 [ '127.0.0.1' , '::ffff:127.0.0.1' , '::1' ] . indexOf (
3233 req . connection . remoteAddress
@@ -38,6 +39,17 @@ const M = {
3839 next ( )
3940 }
4041 } ,
42+
43+ // forbid access to system paths
44+ forbiddenPaths : function ( req , _res , next ) {
45+ const reqPath = uri2filename ( req . url )
46+ if ( FORBIDDEN_RE . test ( reqPath ) ) {
47+ next ( newError ( 403 ) )
48+ return
49+ }
50+ next ( )
51+ } ,
52+
4153 // check session
4254 session : ( token ) => ( req , res , next ) => {
4355 const cookies = cookie . parse ( req . headers . cookie || '' )
@@ -56,29 +68,35 @@ const M = {
5668 next ( newError ( 403 ) )
5769 }
5870 } ,
71+
72+ // load config
5973 config : ( appConfig ) => ( req , res , next ) => {
6074 req . _config = Object . assign ( { } , config , appConfig . config )
6175 next ( )
6276 } ,
6377 renderConfig,
6478 updateConfig,
79+
6580 // render cheatsheet
66- cheatsheet : ( req , res , next ) => {
81+ cheatsheet : ( req , _res , next ) => {
6782 const file = path . resolve ( __dirname , '..' , 'test' , 'cheatsheet.md' )
6883 req . url = filename2uri ( file )
6984 next ( )
7085 } ,
86+
7187 // redirect to homeDir
7288 home : ( req , res ) => {
7389 res . redirect ( homedir ( ) )
7490 } ,
91+
7592 // unescape an escaped URL
76- unescape : function ( req , res , next ) {
93+ unescape : function ( req , _res , next ) {
7794 req . url = decodeURIComponent ( req . url )
7895 next ( )
7996 } ,
97+
8098 // check if file exists and if is directory
81- stat : function ( req , res , next ) {
99+ stat : function ( req , _res , next ) {
82100 const filename = uri2filename ( req . url )
83101 fs . stat ( filename , ( err , stats ) => {
84102 if ( err ) {
@@ -89,6 +107,7 @@ const M = {
89107 }
90108 } )
91109 } ,
110+
92111 // try to autocorrect markdown links (e.g. for gitlab)
93112 noextfile : function ( req , res , next ) {
94113 let path
@@ -117,6 +136,7 @@ const M = {
117136 next ( )
118137 }
119138 } ,
139+
120140 // serve files without extension as plaintext files
121141 plaintext : function ( req , res , next ) {
122142 if ( ! req . isDirectory && FILETEXT . test ( req . url ) ) {
@@ -135,6 +155,7 @@ const M = {
135155 next ( )
136156 }
137157 } ,
158+
138159 // make markdown conversion
139160 markdown : function ( req , res , next ) {
140161 // transform markdown pages
@@ -145,6 +166,7 @@ const M = {
145166 next ( )
146167 }
147168 } ,
169+
148170 // show index for directories
149171 serveIndex : function ( req , res , next ) {
150172 if ( ! req . isDirectory ) {
@@ -174,6 +196,7 @@ const M = {
174196 next ( err )
175197 } )
176198 } ,
199+
177200 // serve static files
178201 serveStatic : function ( path , options ) {
179202 return function ( req , res , next ) {
@@ -190,12 +213,13 @@ const M = {
190213 } )
191214 }
192215 } ,
216+
193217 // final 404 handler
194218 four0four : function ( req , res , next ) {
195219 next ( newError ( 404 ) )
196220 } ,
197- // error handler
198221
222+ // error handler
199223 error : function ( err , req , res , _next ) {
200224 if ( ! err . status ) {
201225 // Error objects
0 commit comments