@@ -5,28 +5,30 @@ 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 =
24+ / ^ \/ ( 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 ) ( \/ | $ ) /
2325
2426/**
2527 * middlewares
2628 */
2729const M = {
2830 // only allow access from localhost
29- forbiddenRemote : function ( req , res , next ) {
31+ forbiddenRemote : function ( req , _res , next ) {
3032 if (
3133 [ '127.0.0.1' , '::ffff:127.0.0.1' , '::1' ] . indexOf (
3234 req . connection . remoteAddress
@@ -38,6 +40,17 @@ const M = {
3840 next ( )
3941 }
4042 } ,
43+
44+ // forbid access to system paths
45+ forbiddenPaths : function ( req , _res , next ) {
46+ const reqPath = uri2filename ( req . url )
47+ if ( FORBIDDEN_RE . test ( reqPath ) ) {
48+ next ( newError ( 403 ) )
49+ return
50+ }
51+ next ( )
52+ } ,
53+
4154 // check session
4255 session : ( token ) => ( req , res , next ) => {
4356 const cookies = cookie . parse ( req . headers . cookie || '' )
@@ -56,29 +69,35 @@ const M = {
5669 next ( newError ( 403 ) )
5770 }
5871 } ,
72+
73+ // load config
5974 config : ( appConfig ) => ( req , res , next ) => {
6075 req . _config = Object . assign ( { } , config , appConfig . config )
6176 next ( )
6277 } ,
6378 renderConfig,
6479 updateConfig,
80+
6581 // render cheatsheet
66- cheatsheet : ( req , res , next ) => {
82+ cheatsheet : ( req , _res , next ) => {
6783 const file = path . resolve ( __dirname , '..' , 'test' , 'cheatsheet.md' )
6884 req . url = filename2uri ( file )
6985 next ( )
7086 } ,
87+
7188 // redirect to homeDir
7289 home : ( req , res ) => {
7390 res . redirect ( homedir ( ) )
7491 } ,
92+
7593 // unescape an escaped URL
76- unescape : function ( req , res , next ) {
94+ unescape : function ( req , _res , next ) {
7795 req . url = decodeURIComponent ( req . url )
7896 next ( )
7997 } ,
98+
8099 // check if file exists and if is directory
81- stat : function ( req , res , next ) {
100+ stat : function ( req , _res , next ) {
82101 const filename = uri2filename ( req . url )
83102 fs . stat ( filename , ( err , stats ) => {
84103 if ( err ) {
@@ -89,6 +108,7 @@ const M = {
89108 }
90109 } )
91110 } ,
111+
92112 // try to autocorrect markdown links (e.g. for gitlab)
93113 noextfile : function ( req , res , next ) {
94114 let path
@@ -117,6 +137,7 @@ const M = {
117137 next ( )
118138 }
119139 } ,
140+
120141 // serve files without extension as plaintext files
121142 plaintext : function ( req , res , next ) {
122143 if ( ! req . isDirectory && FILETEXT . test ( req . url ) ) {
@@ -135,6 +156,7 @@ const M = {
135156 next ( )
136157 }
137158 } ,
159+
138160 // make markdown conversion
139161 markdown : function ( req , res , next ) {
140162 // transform markdown pages
@@ -145,6 +167,7 @@ const M = {
145167 next ( )
146168 }
147169 } ,
170+
148171 // show index for directories
149172 serveIndex : function ( req , res , next ) {
150173 if ( ! req . isDirectory ) {
@@ -174,6 +197,7 @@ const M = {
174197 next ( err )
175198 } )
176199 } ,
200+
177201 // serve static files
178202 serveStatic : function ( path , options ) {
179203 return function ( req , res , next ) {
@@ -190,12 +214,13 @@ const M = {
190214 } )
191215 }
192216 } ,
217+
193218 // final 404 handler
194219 four0four : function ( req , res , next ) {
195220 next ( newError ( 404 ) )
196221 } ,
197- // error handler
198222
223+ // error handler
199224 error : function ( err , req , res , _next ) {
200225 if ( ! err . status ) {
201226 // Error objects
0 commit comments