Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-static

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-static - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

89

index.js

@@ -6,5 +6,9 @@

var send = require('send');
debug = require('debug')('koa-static');
var path = require('path');
var normalize = path.normalize;
var basename = path.basename;
var extname = path.extname;
var resolve = path.resolve;
var fs = require('fs');
var join = path.join;

@@ -33,2 +37,3 @@

// options
debug('static "%s" %j', root, opts);
var root = resolve(root);

@@ -38,2 +43,3 @@ var index = opts.index || 'index.html';

var redirect = false !== opts.redirect;
var hidden = opts.hidden || false;

@@ -43,14 +49,79 @@ return function(next){

if ('GET' != this.method && 'HEAD' != this.method) return next();
// TODO: move this stuff into a lib for this.sendfile() etc
var path = this.path;
var trailingSlash = '/' == path[path.length - 1];
// file stream
var stream = send(this.req, path)
.hidden(opts.hidden)
.maxage(maxage)
.index(index)
.root(root);
// normalize path
path = decode(path);
this.body = stream;
if (-1 == path) return this.error('failed to decode', 400);
// null byte(s)
if (~path.indexOf('\0')) return this.error('null bytes', 400);
// relative to root
path = normalize(join(root, path));
// malicious path, ignore
if (0 != path.indexOf(root)) return;
// hidden file support, ignore
if (!hidden && leadingDot(path)) return;
// index file support
if (index && trailingSlash) path += index;
// stat
try {
var stats = yield stat(path);
} catch (err) {
var notfound = ['ENOENT', 'ENAMETOOLONG', 'ENOTDIR'];
if (~notfound.indexOf(err.code)) return;
err.status = 500;
throw err;
}
// dir
if (stats.isDirectory() && redirect && !trailingSlash) {
this.redirect(this.path + '/');
this.status = 303;
return;
}
// stream
this.type = extname(path);
this.body = fs.createReadStream(path);
}
}
}
}
/**
* Check if it's hidden.
*/
function leadingDot(path) {
return '.' == basename(path)[0];
}
/**
* Stat thunk.
*/
function stat(file) {
return function(done){
fs.stat(file, done);
}
}
/**
* Decode `path`.
*/
function decode(path) {
try {
return decodeURIComponent(path);
} catch (err) {
return -1;
}
}

11

package.json

@@ -5,3 +5,3 @@ {

"repository": "koajs/static",
"version": "1.0.0",
"version": "1.1.0",
"keywords": [

@@ -18,10 +18,11 @@ "koa",

"devDependencies": {
"koa": "0.0.1"
"koa": "0.0.1",
"should": "~1.2.2",
"mocha": "~1.12.0",
"supertest": "~0.7.1"
},
"license": "MIT",
"dependencies": {
"should": "~1.2.2",
"mocha": "~1.12.0",
"supertest": "~0.7.1"
"debug": "*"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc