From 740c0fc9aee83c089d9ad5134900749b03dda68b Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 4 Jan 2023 18:02:53 +0000 Subject: [PATCH 1/4] nginx redirects: echo scheme and host explicitly --- .../lib/produce-redirects.js | 5 +- .../test/produce-redirects-test.js | 52 +++++++++---------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/packages/redirect-producer/lib/produce-redirects.js b/packages/redirect-producer/lib/produce-redirects.js index 73d38e0b9..c9ae755bb 100644 --- a/packages/redirect-producer/lib/produce-redirects.js +++ b/packages/redirect-producer/lib/produce-redirects.js @@ -116,11 +116,12 @@ function createNginxRewriteConf (files, urlPath) { fromUrl = ~fromUrl.indexOf('%20') ? `'${urlPath}${fromUrl.replace(ENCODED_SPACE_RX, ' ')}'` : urlPath + fromUrl let toUrl = file.rel.pub.url toUrl = ~toUrl.indexOf('%20') ? `'${urlPath}${toUrl.replace(ENCODED_SPACE_RX, ' ')}'` : urlPath + toUrl + let toHostPrefix = '$scheme://$http_host' if (file.pub.splat) { const toUrlWithTrailingSlash = ensureTrailingSlash(toUrl) - return `location ^~ ${fromUrl}/ { rewrite ^${regexpEscape(fromUrl)}/(.*)$ ${toUrlWithTrailingSlash}$1 redirect; }` + return `location ^~ ${fromUrl}/ { rewrite ^${regexpEscape(fromUrl)}/(.*)$ ${toHostPrefix}${toUrlWithTrailingSlash}$1 redirect; }` } else { - return `location = ${fromUrl} { return 301 ${toUrl}; }` + return `location = ${fromUrl} { return 301 ${toHostPrefix}${toUrl}; }` } }) return [new File({ contents: Buffer.from(rules.join('\n') + '\n'), out: { path: '.etc/nginx/rewrite.conf' } })] diff --git a/packages/redirect-producer/test/produce-redirects-test.js b/packages/redirect-producer/test/produce-redirects-test.js index 02daca164..30c67e4af 100644 --- a/packages/redirect-producer/test/produce-redirects-test.js +++ b/packages/redirect-producer/test/produce-redirects-test.js @@ -509,11 +509,11 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location = /component-a/module-a/alias-a.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-a/module-a/old-target/index.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-a/module-b/alias-b.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-b/1.0/alias-c.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /index.html { return 301 /component-a/module-a/the-target.html; }', + 'location = /component-a/module-a/alias-a.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-a/module-a/old-target/index.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-a/module-b/alias-b.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-b/1.0/alias-c.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /index.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', ]) }) @@ -537,7 +537,7 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location ^~ /component-a/current/ { rewrite ^/component-a/current/(.*)$ /component-a/1.0/$1 redirect; }', + 'location ^~ /component-a/current/ { rewrite ^/component-a/current/(.*)$ $scheme://$http_host/component-a/1.0/$1 redirect; }', ]) }) @@ -555,7 +555,7 @@ describe('produceRedirects()', () => { const result = produceRedirects(playbook, contentCatalog) expect(result).to.have.lengthOf(1) const rules = extractRules(result[0]) - expect(rules).to.eql(['location ^~ /3.0/ { rewrite ^/3\\.0/(.*)$ /$1 redirect; }']) + expect(rules).to.eql(['location ^~ /3.0/ { rewrite ^/3\\.0/(.*)$ $scheme://$http_host/$1 redirect; }']) }) it('should not add extra trailing slash to splat alias when site path is non-empty', () => { @@ -573,7 +573,7 @@ describe('produceRedirects()', () => { const result = produceRedirects(playbook, contentCatalog) expect(result).to.have.lengthOf(1) const rules = extractRules(result[0]) - expect(rules).to.eql(['location ^~ /docs/3.0/ { rewrite ^/docs/3\\.0/(.*)$ /docs/$1 redirect; }']) + expect(rules).to.eql(['location ^~ /docs/3.0/ { rewrite ^/docs/3\\.0/(.*)$ $scheme://$http_host/docs/$1 redirect; }']) }) it('should escape special regex characters in splat pattern', () => { @@ -596,7 +596,7 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location ^~ /component-c++/1.0/ { rewrite ^/component-c\\+\\+/1\\.0/(.*)$ /component-c++/latest/$1 redirect; }', + 'location ^~ /component-c++/1.0/ { rewrite ^/component-c\\+\\+/1\\.0/(.*)$ $scheme://$http_host/component-c++/latest/$1 redirect; }', ]) }) @@ -614,7 +614,7 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - "location = '/component-a/module-a/alias with spaces.html' { return 301 '/component-a/module-a/target with spaces.html'; }", + "location = '/component-a/module-a/alias with spaces.html' { return 301 '$scheme://$http_host/component-a/module-a/target with spaces.html'; }", ]) }) @@ -626,11 +626,11 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location = /docs/component-a/module-a/alias-a.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-a/module-a/old-target/index.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-a/module-b/alias-b.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-b/1.0/alias-c.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/index.html { return 301 /docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-a/alias-a.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-a/old-target/index.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-b/alias-b.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-b/1.0/alias-c.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/index.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', ]) }) @@ -642,11 +642,11 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location = /docs/component-a/module-a/alias-a.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-a/module-a/old-target/index.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-a/module-b/alias-b.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-b/1.0/alias-c.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/index.html { return 301 /docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-a/alias-a.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-a/old-target/index.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-b/alias-b.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-b/1.0/alias-c.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/index.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', ]) }) @@ -658,7 +658,7 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.include( - 'location = /docs/component-a/module-a/alias-a.html { return 301 /docs/component-a/module-a/the-target.html; }' + 'location = /docs/component-a/module-a/alias-a.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }' ) }) @@ -670,11 +670,11 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location = /component-a/module-a/alias-a.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-a/module-a/old-target/index.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-a/module-b/alias-b.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-b/1.0/alias-c.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /index.html { return 301 /component-a/module-a/the-target.html; }', + 'location = /component-a/module-a/alias-a.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-a/module-a/old-target/index.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-a/module-b/alias-b.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-b/1.0/alias-c.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /index.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', ]) }) -- GitLab From 56e49baed4a3b0edb6b4c6bb68fa6df2ec1a0442 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 4 Jan 2023 18:07:42 +0000 Subject: [PATCH 2/4] linter be gone --- packages/redirect-producer/lib/produce-redirects.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/redirect-producer/lib/produce-redirects.js b/packages/redirect-producer/lib/produce-redirects.js index c9ae755bb..3a2f6298f 100644 --- a/packages/redirect-producer/lib/produce-redirects.js +++ b/packages/redirect-producer/lib/produce-redirects.js @@ -116,7 +116,7 @@ function createNginxRewriteConf (files, urlPath) { fromUrl = ~fromUrl.indexOf('%20') ? `'${urlPath}${fromUrl.replace(ENCODED_SPACE_RX, ' ')}'` : urlPath + fromUrl let toUrl = file.rel.pub.url toUrl = ~toUrl.indexOf('%20') ? `'${urlPath}${toUrl.replace(ENCODED_SPACE_RX, ' ')}'` : urlPath + toUrl - let toHostPrefix = '$scheme://$http_host' + const toHostPrefix = '$scheme://$http_host' if (file.pub.splat) { const toUrlWithTrailingSlash = ensureTrailingSlash(toUrl) return `location ^~ ${fromUrl}/ { rewrite ^${regexpEscape(fromUrl)}/(.*)$ ${toHostPrefix}${toUrlWithTrailingSlash}$1 redirect; }` -- GitLab From 4011e235d6fa78724fec50fe6fa002778e3f5f74 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 4 Jan 2023 18:12:53 +0000 Subject: [PATCH 3/4] linter be gone, part II --- packages/redirect-producer/lib/produce-redirects.js | 4 +++- packages/redirect-producer/test/produce-redirects-test.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/redirect-producer/lib/produce-redirects.js b/packages/redirect-producer/lib/produce-redirects.js index 3a2f6298f..6d51576e5 100644 --- a/packages/redirect-producer/lib/produce-redirects.js +++ b/packages/redirect-producer/lib/produce-redirects.js @@ -119,7 +119,9 @@ function createNginxRewriteConf (files, urlPath) { const toHostPrefix = '$scheme://$http_host' if (file.pub.splat) { const toUrlWithTrailingSlash = ensureTrailingSlash(toUrl) - return `location ^~ ${fromUrl}/ { rewrite ^${regexpEscape(fromUrl)}/(.*)$ ${toHostPrefix}${toUrlWithTrailingSlash}$1 redirect; }` + return `location ^~ ${fromUrl}/ { rewrite ^${regexpEscape( + fromUrl + )}/(.*)$ ${toHostPrefix}${toUrlWithTrailingSlash}$1 redirect; }` } else { return `location = ${fromUrl} { return 301 ${toHostPrefix}${toUrl}; }` } diff --git a/packages/redirect-producer/test/produce-redirects-test.js b/packages/redirect-producer/test/produce-redirects-test.js index 30c67e4af..f77ed15a0 100644 --- a/packages/redirect-producer/test/produce-redirects-test.js +++ b/packages/redirect-producer/test/produce-redirects-test.js @@ -573,7 +573,9 @@ describe('produceRedirects()', () => { const result = produceRedirects(playbook, contentCatalog) expect(result).to.have.lengthOf(1) const rules = extractRules(result[0]) - expect(rules).to.eql(['location ^~ /docs/3.0/ { rewrite ^/docs/3\\.0/(.*)$ $scheme://$http_host/docs/$1 redirect; }']) + expect(rules).to.eql([ + 'location ^~ /docs/3.0/ { rewrite ^/docs/3\\.0/(.*)$ $scheme://$http_host/docs/$1 redirect; }' + ]) }) it('should escape special regex characters in splat pattern', () => { -- GitLab From 614c0066f8d4b6497b6f5c520b1ddff076d0afbe Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 4 Jan 2023 18:17:23 +0000 Subject: [PATCH 4/4] linter be gone, part III --- packages/redirect-producer/test/produce-redirects-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/redirect-producer/test/produce-redirects-test.js b/packages/redirect-producer/test/produce-redirects-test.js index f77ed15a0..6e29898a7 100644 --- a/packages/redirect-producer/test/produce-redirects-test.js +++ b/packages/redirect-producer/test/produce-redirects-test.js @@ -574,7 +574,7 @@ describe('produceRedirects()', () => { expect(result).to.have.lengthOf(1) const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location ^~ /docs/3.0/ { rewrite ^/docs/3\\.0/(.*)$ $scheme://$http_host/docs/$1 redirect; }' + 'location ^~ /docs/3.0/ { rewrite ^/docs/3\\.0/(.*)$ $scheme://$http_host/docs/$1 redirect; }', ]) }) -- GitLab