Skip to content

Commit bdd81f8

Browse files
authored
Delete back as a magic string (#5933)
1 parent 6c98f80 commit bdd81f8

File tree

6 files changed

+6
-76
lines changed

6 files changed

+6
-76
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ unreleased
77
* will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.` for inputs outside this range
88
* will throw a `TypeError: Invalid status code: ${code}. Status code must be an integer.` for non integer inputs
99
10+
* `res.redirect('back')` and `res.location('back')` is no longer a supported magic string, explicitly use `req.get('Referrer') || '/'`.
1011
* change:
1112
- `res.clearCookie` will ignore user provided `maxAge` and `expires` options
1213
* deps: cookie-signature@^1.2.1

examples/auth/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ app.post('/login', function (req, res, next) {
116116
req.session.success = 'Authenticated as ' + user.name
117117
+ ' click to <a href="/logout">logout</a>. '
118118
+ ' You may now access <a href="/restricted">/restricted</a>.';
119-
res.redirect('back');
119+
res.redirect(req.get('Referrer') || '/');
120120
});
121121
} else {
122122
req.session.error = 'Authentication failed, please check your '

examples/cookies/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ app.get('/', function(req, res){
3333

3434
app.get('/forget', function(req, res){
3535
res.clearCookie('remember');
36-
res.redirect('back');
36+
res.redirect(req.get('Referrer') || '/');
3737
});
3838

3939
app.post('/', function(req, res){
@@ -43,7 +43,7 @@ app.post('/', function(req, res){
4343
res.cookie('remember', 1, { maxAge: minute })
4444
}
4545

46-
res.redirect('back');
46+
res.redirect(req.get('Referrer') || '/');
4747
});
4848

4949
/* istanbul ignore next */

examples/route-separation/user.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ exports.update = function(req, res){
4343
var user = req.body.user;
4444
req.user.name = user.name;
4545
req.user.email = user.email;
46-
res.redirect('back');
46+
res.redirect(req.get('Referrer') || '/');
4747
};

lib/response.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -785,26 +785,13 @@ res.cookie = function (name, value, options) {
785785
*/
786786

787787
res.location = function location(url) {
788-
var loc;
789-
790-
// "back" is an alias for the referrer
791-
if (url === 'back') {
792-
loc = this.req.get('Referrer') || '/';
793-
} else {
794-
loc = String(url);
795-
}
796-
797-
return this.set('Location', encodeUrl(loc));
788+
return this.set('Location', encodeUrl(url));
798789
};
799790

800791
/**
801792
* Redirect to the given `url` with optional response `status`
802793
* defaulting to 302.
803794
*
804-
* The resulting `url` is determined by `res.location()`, so
805-
* it will play nicely with mounted apps, relative paths,
806-
* `"back"` etc.
807-
*
808795
* Examples:
809796
*
810797
* res.redirect('/foo/bar');

test/res.location.js

-58
Original file line numberDiff line numberDiff line change
@@ -46,64 +46,6 @@ describe('res', function(){
4646
.expect(200, done)
4747
})
4848

49-
describe('when url is "back"', function () {
50-
it('should set location from "Referer" header', function (done) {
51-
var app = express()
52-
53-
app.use(function (req, res) {
54-
res.location('back').end()
55-
})
56-
57-
request(app)
58-
.get('/')
59-
.set('Referer', '/some/page.html')
60-
.expect('Location', '/some/page.html')
61-
.expect(200, done)
62-
})
63-
64-
it('should set location from "Referrer" header', function (done) {
65-
var app = express()
66-
67-
app.use(function (req, res) {
68-
res.location('back').end()
69-
})
70-
71-
request(app)
72-
.get('/')
73-
.set('Referrer', '/some/page.html')
74-
.expect('Location', '/some/page.html')
75-
.expect(200, done)
76-
})
77-
78-
it('should prefer "Referrer" header', function (done) {
79-
var app = express()
80-
81-
app.use(function (req, res) {
82-
res.location('back').end()
83-
})
84-
85-
request(app)
86-
.get('/')
87-
.set('Referer', '/some/page1.html')
88-
.set('Referrer', '/some/page2.html')
89-
.expect('Location', '/some/page2.html')
90-
.expect(200, done)
91-
})
92-
93-
it('should set the header to "/" without referrer', function (done) {
94-
var app = express()
95-
96-
app.use(function (req, res) {
97-
res.location('back').end()
98-
})
99-
100-
request(app)
101-
.get('/')
102-
.expect('Location', '/')
103-
.expect(200, done)
104-
})
105-
})
106-
10749
it('should encode data uri1', function (done) {
10850
var app = express()
10951
app.use(function (req, res) {

0 commit comments

Comments
 (0)