Skip to content
Myr

Polish HTML structure of the response in the res.redirect() function (#5167)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* structure the DOM body * structure the DOM body * test: add html title to redirect test * fix: update HTML structure for include body and head tags * docs: improve HTML structure in res.redirect() responses for better browser compatibility ---------

authored by Bernice Wu · committed by GitHub · parent 2cd372e · Browse files · 3 files+12 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Sebastian Beltran <bjohansebas@gmail.com>

Diff stats

3 files+12 added−5 removed

  • tests:+4 added−4 removed
  • other:+8 added−1 removed

3 files+12 added−5 removed

modified:lib/response.js+2 added-1 removed
848 unmodified lines
849 },850851 html: function(){852 var u = escapeHtml(address);853 body = '<p>' + statuses.message[status] + '. Redirecting to ' + u + '</p>'853 body = '<!DOCTYPE html><head><title>' + statuses.message[status] + '</title></head>'854 + '<body><p>' + statuses.message[status] + '. Redirecting to ' + u + '</p></body>'855 },856857 default: function(){858 body = '';
196 unmodified lines
modified:test/res.redirect.js+4 added-4 removed
89 unmodified lines
90 .get('/')91 .set('Accept', 'text/html')92 .expect('Content-Type', /html/)93 .expect('Location', 'http://google.com')94 .expect(302, '<p>Found. Redirecting to http://google.com</p>', done)94 .expect(302, '<!DOCTYPE html><head><title>Found</title></head><body><p>Found. Redirecting to http://google.com</p></body>', done)95 })9697 it('should escape the url', function(done){98 var app = express();
7 unmodified lines
106 .set('Host', 'http://example.com')107 .set('Accept', 'text/html')108 .expect('Content-Type', /html/)109 .expect('Location', '%3Cla\'me%3E')110 .expect(302, '<p>Found. Redirecting to %3Cla&#39;me%3E</p>', done)110 .expect(302, '<!DOCTYPE html><head><title>Found</title></head><body><p>Found. Redirecting to %3Cla&#39;me%3E</p></body>', done)111 })112113 it('should not render evil javascript links in anchor href (prevent XSS)', function(done){114 var app = express();
9 unmodified lines
124 .set('Host', 'http://example.com')125 .set('Accept', 'text/html')126 .expect('Content-Type', /html/)127 .expect('Location', encodedXss)128 .expect(302, '<p>Found. Redirecting to ' + encodedXss +'</p>', done);128 .expect(302, '<!DOCTYPE html><head><title>Found</title></head><body><p>Found. Redirecting to ' + encodedXss +'</p></body>', done);129 });130131 it('should include the redirect type', function(done){132 var app = express();
6 unmodified lines
139 .get('/')140 .set('Accept', 'text/html')141 .expect('Content-Type', /html/)142 .expect('Location', 'http://google.com')143 .expect(301, '<p>Moved Permanently. Redirecting to http://google.com</p>', done);143 .expect(301, '<!DOCTYPE html><head><title>Moved Permanently</title></head><body><p>Moved Permanently. Redirecting to http://google.com</p></body>', done);144 })145 })146147 describe('when accepting text', function(){
67 unmodified lines
modified:History.md+6 added
1# Unreleased Changes23## 🚀 Improvements45- Improve HTML structure in `res.redirect()` responses when HTML format is accepted by adding `<!DOCTYPE html>`, `<title>`, and `<body>` tags for better browser compatibility - by [@Bernice55231](https://github.com/Bernice55231) in [#5167](https://github.com/expressjs/express/pull/5167)675.2.1 / 2025-12-018=======================910* Revert security fix for [CVE-2024-51999](https://www.cve.org/CVERecord?id=CVE-2024-51999) ([GHSA-pj86-cfqh-vqx6](https://github.com/expressjs/express/security/advisories/GHSA-pj86-cfqh-vqx6))
3867 unmodified lines