@@ -63,6 +63,17 @@ app.use(session({
63
63
secret : secret ,
64
64
} ) ) ;
65
65
66
+
67
+ function isDefinedRoute ( name ) {
68
+ // prevent the user from using well-defined routes as a short URL
69
+ app . _router . stack . forEach ( function ( r ) {
70
+ if ( r . route && r . route . path && r . route . path == `/${ name } ` ) {
71
+ return true
72
+ }
73
+ } )
74
+ return false ;
75
+ }
76
+
66
77
//-----------------------------------------------------------------------------
67
78
// To support persistent login sessions, Passport needs to be able to
68
79
// serialize users into and deserialize users out of the session. Typically,
@@ -146,10 +157,18 @@ app.use(passport.initialize());
146
157
app . use ( passport . session ( ) ) ;
147
158
app . use ( favicon ( __dirname + '/public/img/favicon.ico' ) ) ;
148
159
app . use ( '/static' , express . static ( 'public' ) )
149
- function ensureAuthenticated ( req , res , next ) {
150
- if ( req . isAuthenticated ( ) ) { return next ( ) ; }
151
- res . redirect ( '/#' ) ;
160
+
161
+ async function ensureAuthenticated ( req , res , next ) {
162
+ if ( ! req . user ) { return res . redirect ( '/#' ) ; }
163
+ req . user . _json . groups = await getUserGroups ( req . user . oid , gat ) ;
164
+ const intserect = validateArray ( config . groups_permitted , req . user . _json . groups ) ;
165
+ const intersect2 = validateArray ( config . admin_groups , req . user . _json . groups )
166
+ if ( ! intserect && ! intersect2 ) {
167
+ return res . status ( 401 ) . redirect ( "/unauthorized" ) ;
168
+ }
169
+ next ( ) ;
152
170
} ;
171
+
153
172
function checkIfAdmin ( req ) {
154
173
const userGroups = new Set ( req . user . _json . groups !== undefined ? req . user . _json . groups : [ ] ) ;
155
174
const adminGroups = new Set ( config . admin_groups ) ;
@@ -358,23 +377,9 @@ function validateArray(userGroups, accessGroups) {
358
377
return false ;
359
378
}
360
379
361
- // group access check
362
- app . use ( async ( req , res , next ) => {
363
- if ( ! req . user ) { return next ( ) ; }
364
- req . user . _json . groups = await getUserGroups ( req . user . oid , gat ) ;
365
- const intserect = validateArray ( config . groups_permitted , req . user . _json . groups ) ;
366
- const intersect2 = validateArray ( config . admin_groups , req . user . _json . groups )
367
- if ( ! intserect && ! intersect2 ) {
368
- return res . status ( 401 ) . redirect ( "/unauthorized" ) ;
369
- }
370
- next ( ) ;
371
- } )
372
-
373
380
app . use ( '/admin/' , ensureAdmin )
374
- // begin business logic
375
381
376
382
app . get ( '/' , async function ( req , res ) {
377
-
378
383
if ( req . isAuthenticated ( ) ) { return res . redirect ( '/create' ) }
379
384
res . render ( 'home.html' , { partials, productName : config . branding . title , logoPath : config . branding . logoPath , copyrightOwner : config . branding . copyrightOwner , statusURL : config . branding . statusURL , orgHome : config . branding . orgHome , loginProvider : config . branding . loginProvider } ) ;
380
385
return
@@ -402,11 +407,15 @@ app.post('/addURL', ensureAuthenticated, async function (req, res) {
402
407
const url = req . query . url ;
403
408
const name = req . query . name ;
404
409
const groups = req . body . groups
410
+ if ( isDefinedRoute ( name ) ) {
411
+ return res . status ( 409 ) . json ( {
412
+ message : "This short URL is reserved by the system. Please try another."
413
+ } )
414
+ }
405
415
if ( url . indexOf ( baseURL ) > - 1 ) {
406
- res . json ( {
416
+ return res . json ( {
407
417
message : `The origin URL cannot be a path of ${ baseURL } `
408
418
} )
409
- return
410
419
}
411
420
if ( url === undefined || name === undefined ) {
412
421
res . status ( 400 ) . json ( {
@@ -417,7 +426,7 @@ app.post('/addURL', ensureAuthenticated, async function (req, res) {
417
426
addURLToDB ( name , url , email , groups ) . then ( ( obj ) => {
418
427
res . json ( {
419
428
url : obj . url ,
420
- shortURL : `https://go.epochml.org /${ obj . name } ` ,
429
+ shortURL : `${ config . branding . externalDomain } /${ obj . name } ` ,
421
430
email : obj . email ,
422
431
groups : groups
423
432
} ) ;
0 commit comments