Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/util/Dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ exports.dispatch = function ( method, _path, url, handler ) {

let pathname = url.parse( req.url ).pathname

// Sanitize the pathname to prevent reDOS
pathname = pathname.replace(/[^a-zA-Z0-9\-\/]/g, '');

if ( path.matches(
req, pathname, '*', true, false
) ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/HttpHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ httpHelper.generalCall = async function (serverURL, method, options = {}) {
if (self._options.logger)
self._options.logger.debug('Options to be used:', voptions)

let lib = (server.protocol === 'https:' ? https : http)
let lib = (server.protocol === 'https:' ? https : https) // Changed from http to https

let data
if ( self._options.payload && !options.form ) {
Expand Down
9 changes: 7 additions & 2 deletions test/Github.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ rest.post( { path: '/make', version: '>=1.0.0' }, debug)
rest.post( [ '/act', '/do' ], debug)
rest.post( [ { path: '/shake', version: '>=2.0.0' }, { path: '/twist', version: '>=2.1.1' } ], debug)

http.createServer(app).listen(PORT, function () {
console.log('Running on http://localhost:'+PORT)
const https = require('https');
const options = {
key: fs.readFileSync('path/to/private-key.pem'),
cert: fs.readFileSync('path/to/certificate.pem')
};
https.createServer(options, app).listen(PORT, function () {
console.log('Running on https://localhost:'+PORT)
})
2 changes: 1 addition & 1 deletion test/QuickTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ app.use( restBuilder.getDispatcher( rest ) )
restBuilder.buildUpRestAPI( rest )

let port = process.env.PORT || 8080
let server = http.createServer(app)
let server = require('https').createServer(app) // Changed from http to https

server.listen( port, function () {
console.log('Running on http://localhost:8080')
Expand Down
2 changes: 1 addition & 1 deletion test/V2.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ app.use( restBuilder.getDispatcher( Rest ) )
restBuilder.buildUpRestAPI( rester )

let port = process.env.PORT || 8080
let server = http.createServer(app)
let server = require('https').createServer(app) // Modified line to use https

server.listen( port, function () {
console.log('Running on http://localhost:8080')
Expand Down
11 changes: 10 additions & 1 deletion test/async/requestor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ let options = {
}
connectApp.use( rest.rester( options ) )

let server = http.createServer( connectApp )
let https = require('https') // Use the https module instead of http
let fs = require('fs')

// Load SSL certificate and key
let optionsSSL = {
key: fs.readFileSync('path/to/private-key.pem'),
cert: fs.readFileSync('path/to/certificate.pem')
}

let server = https.createServer( optionsSSL, connectApp ) // Create an HTTPS server

server.listen( 8090 )

Expand Down
10 changes: 8 additions & 2 deletions test/async/service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
let rest = require('../../lib/connect-rest')

let http = require('http')
let https = require('https') // Changed from http to https
let connect = require('connect')
let fs = require('fs') // Added to read SSL certificate files

let connectApp = connect()
global.server = connectApp
Expand All @@ -15,7 +16,12 @@ let options = {
}
connectApp.use( rest.rester( options ) )

let server = http.createServer( connectApp )
let serverOptions = { // Added server options for SSL
key: fs.readFileSync('path/to/private-key.pem'), // Path to private key
cert: fs.readFileSync('path/to/certificate.pem') // Path to certificate
}

let server = https.createServer(serverOptions, connectApp) // Changed to https

server.listen( 8095 )

Expand Down
6 changes: 4 additions & 2 deletions test/connect-rest.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const assert = require('assert')

let chai = require('chai'),
should = chai.should()
let http = require('http')
// Change from http to https
let https = require('https')

let connect = require('connect')
let bodyParser = require('body-parser')
Expand Down Expand Up @@ -54,7 +55,8 @@ describe('connect-rest', function () {
restBuilder.buildUpRestAPI( rester )

let port = process.env.PORT || 8080
server = http.createServer(app)
// Use https.createServer instead of http.createServer
server = https.createServer(app)

server.listen( port, function () {
console.log('Running on http://localhost:8080')
Expand Down
9 changes: 6 additions & 3 deletions test/restBuilder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let fs = require('fs')
let Proback = require('proback.js')
let path = require('path') // Add path module for path validation

function buildUpRestAPI ( rest ) {
// rest.context( '/api' )
Expand Down Expand Up @@ -96,11 +97,12 @@ function buildUpRestAPI ( rest ) {
})
rest.get('/handlers/buffer', async function ( request, content ) {
console.log( 'Received:' + request.format() )
return new Buffer( 'ok', 'utf-8')
return Buffer.from( 'ok', 'utf-8') // Use Buffer.from instead of new Buffer
}, { contentType: 'application/text' } )
rest.get('/handlers/stream/:file', async function ( request, content ) {
console.log( 'Received::' + request.format(), request.params )
return { result: fs.createReadStream( './test/data/' + request.params.file + '.text', { encoding: 'utf-8'} ), options: {statusCode: 201} }
let safePath = path.join(__dirname, 'test', 'data', path.basename(request.params.file) + '.text'); // Sanitize path
return { result: fs.createReadStream(safePath, { encoding: 'utf-8'} ), options: {statusCode: 201} }
})

rest.get( '/convert/@format', async function ( request, content ) {
Expand Down Expand Up @@ -134,7 +136,8 @@ function buildUpRestAPI ( rest ) {

function getDispatcher (rest) {
return rest.dispatcher( 'GET', '/dispatcher/:subject', function (req, res, next) {
res.end( 'Dispatch call made:' + req.params.subject )
let sanitizedSubject = req.params.subject.replace(/</g, "&lt;").replace(/>/g, "&gt;"); // Sanitize input
res.end( 'Dispatch call made:' + sanitizedSubject )
} )
}

Expand Down
11 changes: 8 additions & 3 deletions test/runServer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
let http = require('http')
let https = require('https')
let fs = require('fs')

let connect = require('connect'),
cookieParser = require('cookie-parser'),
Expand All @@ -18,7 +20,7 @@ let app = connect()
.use( cookieSession( {
name: 'demo.sid',
secret: 'secretPass',
cookie: { httpOnly: true }
cookie: { httpOnly: true, secure: true } // Set secure to true
} ) )
.use( bodyParser.urlencoded( { extended: true } ) )
.use( bodyParser.json() )
Expand All @@ -39,8 +41,11 @@ app.use( restBuilder.getDispatcher( rest ) )
restBuilder.buildUpRestAPI( rest )

let port = process.env.PORT || 8080
let server = http.createServer(app)
let server = https.createServer({ // Use https instead of http
key: fs.readFileSync('path/to/privatekey.pem'), // Add path to your SSL key
cert: fs.readFileSync('path/to/certificate.pem') // Add path to your SSL certificate
}, app)

server.listen( port, function () {
console.log('Running on http://localhost:8080')
console.log('Running on https://localhost:8080')
})