-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
test: fix static file tests for supertest 7+ URL normalization #6856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ var assert = require('node:assert') | |||||||||||||||||||||||||||||||||||||||||
| var express = require('..') | ||||||||||||||||||||||||||||||||||||||||||
| var path = require('node:path') | ||||||||||||||||||||||||||||||||||||||||||
| const { Buffer } = require('node:buffer'); | ||||||||||||||||||||||||||||||||||||||||||
| var http = require('node:http') | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| var request = require('supertest') | ||||||||||||||||||||||||||||||||||||||||||
| var utils = require('./support/utils') | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -269,9 +270,26 @@ describe('express.static()', function () { | |||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| it('should fall-through when traversing past root', function (done) { | ||||||||||||||||||||||||||||||||||||||||||
| request(this.app) | ||||||||||||||||||||||||||||||||||||||||||
| .get('/users/../../todo.txt') | ||||||||||||||||||||||||||||||||||||||||||
| .expect(404, 'Not Found', done) | ||||||||||||||||||||||||||||||||||||||||||
| var server = this.app.listen(function () { | ||||||||||||||||||||||||||||||||||||||||||
| var port = server.address().port | ||||||||||||||||||||||||||||||||||||||||||
| // Use http.request to avoid URL normalization by superagent | ||||||||||||||||||||||||||||||||||||||||||
| var req = http.request({ | ||||||||||||||||||||||||||||||||||||||||||
| hostname: 'localhost', | ||||||||||||||||||||||||||||||||||||||||||
| port: port, | ||||||||||||||||||||||||||||||||||||||||||
| path: '/users/../../todo.txt', | ||||||||||||||||||||||||||||||||||||||||||
| method: 'GET' | ||||||||||||||||||||||||||||||||||||||||||
| }, function (res) { | ||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(res.statusCode, 404) | ||||||||||||||||||||||||||||||||||||||||||
| var body = '' | ||||||||||||||||||||||||||||||||||||||||||
| res.on('data', function (chunk) { body += chunk }) | ||||||||||||||||||||||||||||||||||||||||||
| res.on('end', function () { | ||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(body, 'Not Found') | ||||||||||||||||||||||||||||||||||||||||||
| server.close(done) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+281
to
+288
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the assertions throw, they prevent cleanup ( They're also throwing in callbacks, so mocha only sees them due to its global uncaught exception handler. (ideally we assert in only one place, so moved the first one) This feedback applies to all of the updated test cases in the PR
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| req.on('error', done) | ||||||||||||||||||||||||||||||||||||||||||
| req.end() | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| it('should fall-through when URL too long', function (done) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -344,9 +362,26 @@ describe('express.static()', function () { | |||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| it('should 403 when traversing past root', function (done) { | ||||||||||||||||||||||||||||||||||||||||||
| request(this.app) | ||||||||||||||||||||||||||||||||||||||||||
| .get('/users/../../todo.txt') | ||||||||||||||||||||||||||||||||||||||||||
| .expect(403, /ForbiddenError/, done) | ||||||||||||||||||||||||||||||||||||||||||
| var server = this.app.listen(function () { | ||||||||||||||||||||||||||||||||||||||||||
| var port = server.address().port | ||||||||||||||||||||||||||||||||||||||||||
| // Use http.request to avoid URL normalization by superagent | ||||||||||||||||||||||||||||||||||||||||||
| var req = http.request({ | ||||||||||||||||||||||||||||||||||||||||||
| hostname: 'localhost', | ||||||||||||||||||||||||||||||||||||||||||
| port: port, | ||||||||||||||||||||||||||||||||||||||||||
| path: '/users/../../todo.txt', | ||||||||||||||||||||||||||||||||||||||||||
| method: 'GET' | ||||||||||||||||||||||||||||||||||||||||||
| }, function (res) { | ||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(res.statusCode, 403) | ||||||||||||||||||||||||||||||||||||||||||
| var body = '' | ||||||||||||||||||||||||||||||||||||||||||
| res.on('data', function (chunk) { body += chunk }) | ||||||||||||||||||||||||||||||||||||||||||
| res.on('end', function () { | ||||||||||||||||||||||||||||||||||||||||||
| assert.match(body, /ForbiddenError/) | ||||||||||||||||||||||||||||||||||||||||||
| server.close(done) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| req.on('error', done) | ||||||||||||||||||||||||||||||||||||||||||
| req.end() | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| it('should 404 when URL too long', function (done) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -578,15 +613,39 @@ describe('express.static()', function () { | |||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| it('should catch urlencoded ../', function (done) { | ||||||||||||||||||||||||||||||||||||||||||
| request(this.app) | ||||||||||||||||||||||||||||||||||||||||||
| .get('/users/%2e%2e/%2e%2e/todo.txt') | ||||||||||||||||||||||||||||||||||||||||||
| .expect(403, done) | ||||||||||||||||||||||||||||||||||||||||||
| var server = this.app.listen(function () { | ||||||||||||||||||||||||||||||||||||||||||
| var port = server.address().port | ||||||||||||||||||||||||||||||||||||||||||
| // Use http.request to avoid URL normalization by superagent | ||||||||||||||||||||||||||||||||||||||||||
| var req = http.request({ | ||||||||||||||||||||||||||||||||||||||||||
| hostname: 'localhost', | ||||||||||||||||||||||||||||||||||||||||||
| port: port, | ||||||||||||||||||||||||||||||||||||||||||
| path: '/users/%2e%2e/%2e%2e/todo.txt', | ||||||||||||||||||||||||||||||||||||||||||
| method: 'GET' | ||||||||||||||||||||||||||||||||||||||||||
| }, function (res) { | ||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(res.statusCode, 403) | ||||||||||||||||||||||||||||||||||||||||||
| server.close(done) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| req.on('error', done) | ||||||||||||||||||||||||||||||||||||||||||
| req.end() | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| it('should not allow root path disclosure', function (done) { | ||||||||||||||||||||||||||||||||||||||||||
| request(this.app) | ||||||||||||||||||||||||||||||||||||||||||
| .get('/users/../../fixtures/todo.txt') | ||||||||||||||||||||||||||||||||||||||||||
| .expect(403, done) | ||||||||||||||||||||||||||||||||||||||||||
| var server = this.app.listen(function () { | ||||||||||||||||||||||||||||||||||||||||||
| var port = server.address().port | ||||||||||||||||||||||||||||||||||||||||||
| // Use http.request to avoid URL normalization by superagent | ||||||||||||||||||||||||||||||||||||||||||
| var req = http.request({ | ||||||||||||||||||||||||||||||||||||||||||
| hostname: 'localhost', | ||||||||||||||||||||||||||||||||||||||||||
| port: port, | ||||||||||||||||||||||||||||||||||||||||||
| path: '/users/../../fixtures/todo.txt', | ||||||||||||||||||||||||||||||||||||||||||
| method: 'GET' | ||||||||||||||||||||||||||||||||||||||||||
| }, function (res) { | ||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(res.statusCode, 403) | ||||||||||||||||||||||||||||||||||||||||||
| server.close(done) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| req.on('error', done) | ||||||||||||||||||||||||||||||||||||||||||
| req.end() | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused?