Skip to content

Commit

Permalink
Merge pull request #32 from KnpLabs/change/manage-complete-url-redire…
Browse files Browse the repository at this point in the history
…ction

Match the url from start for redirections
  • Loading branch information
alexpozzi authored May 27, 2021
2 parents 8ded449 + f90bdc0 commit 90d674e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ MANAGER_HTTP_SERVER_HOST=0.0.0.0
WORKER_ENABLED=1
WORKER_RENDERER_AUTHORIZED_REQUEST_DOMAINS=*
WORKER_RENDERER_AUTHORIZED_REQUEST_RESOURCES=*
WORKER_RENDERER_REDIRECTED_DOMAINS=external-nginx|nginx
WORKER_RENDERER_REDIRECTIONS=http://external-nginx|http://nginx
2 changes: 1 addition & 1 deletion docker-compose.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
- MANAGER_ENABLED=0
- WORKER_ENABLED=1
- QUEUE_REDIS_DSN=redis://redis:6379
- WORKER_RENDERER_REDIRECTED_DOMAINS=external-nginx|nginx
- WORKER_RENDERER_REDIRECTIONS=http://external-nginx|http://nginx

redis:
image: redis:6.2.2-buster
Expand Down
6 changes: 3 additions & 3 deletions src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const isManagerConfigurationValid = T

// isWorkerConfigurationValid :: Configuration -> Boolean
const isWorkerConfigurationValid = pipe(
path(['worker', 'renderer', 'domain_redirections']),
path(['worker', 'renderer', 'redirections']),
reduce(
(acc, { from, to }) => unless(
equals(false),
Expand Down Expand Up @@ -96,11 +96,11 @@ const generate = () => ({
authorized_request_resources: commaSeparatedStringToArray(
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_RESOURCES ?? '*',
),
domain_redirections: pipe(
redirections: pipe(
commaSeparatedStringToArray,
map(pipeSeparatedStringToArray),
map(([from, to]) => ({ from, to })),
)(process.env.WORKER_RENDERER_REDIRECTED_DOMAINS ?? ''),
)(process.env.WORKER_RENDERER_REDIRECTIONS ?? ''),
},
},
})
Expand Down
16 changes: 8 additions & 8 deletions src/configuration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('configuration :: createConfiguration', () => {
authorized_request_resources: [
'*',
],
domain_redirections: [],
redirections: [],
},
},
})
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('configuration :: createConfiguration', () => {
authorized_request_resources: [
'*',
],
domain_redirections: [],
redirections: [],
},
},
})
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('configuration :: createConfiguration', () => {
authorized_request_resources: [
'*',
],
domain_redirections: [],
redirections: [],
},
},
})
Expand All @@ -129,7 +129,7 @@ describe('configuration :: createConfiguration', () => {
process.env.WORKER_ENABLED = 1
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_DOMAINS = 'localhost, nginx'
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_RESOURCES = 'document, script'
process.env.WORKER_RENDERER_REDIRECTED_DOMAINS = 'example.com|nginx'
process.env.WORKER_RENDERER_REDIRECTIONS = 'http://example.com|http://nginx'

expect(createConfiguration()).toStrictEqual({
log: {
Expand Down Expand Up @@ -159,9 +159,9 @@ describe('configuration :: createConfiguration', () => {
'document',
'script',
],
domain_redirections: [{
from: 'example.com',
to: 'nginx',
redirections: [{
from: 'http://example.com',
to: 'http://nginx',
}],
},
},
Expand All @@ -181,7 +181,7 @@ describe('configuration :: createConfiguration', () => {

it(`throws an exception when the worker configuration is invalid`, () => {
process.env.QUEUE_REDIS_DSN = 'redis://redis:6379'
process.env.WORKER_RENDERER_REDIRECTED_DOMAINS = 'example.com|'
process.env.WORKER_RENDERER_REDIRECTIONS = 'http://example.com|'

expect(() => createConfiguration()).toThrow('Invalid configuration.')
})
Expand Down
26 changes: 15 additions & 11 deletions src/worker/renderers/chrome/browserRequestHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { T, anyPass, complement, cond, equals, find, ifElse, isNil, pipe, propEq, test } from 'ramda'
import { T, anyPass, complement, cond, equals, find, ifElse, isNil, pipe, replace, tap, test } from 'ramda'

// resolveRequestDomain :: Request -> String
const resolveRequestDomain = req => req.url().match(/^(https?:\/\/)?(?<host>[^/]+)/).groups.host
Expand Down Expand Up @@ -29,19 +29,23 @@ const isRequestResourceAuthorized = authorizedRequestResources => pipe(
complement(isNil),
)

// getDomainRedirection :: [Object] -> Request -> Object|Null
const getDomainRedirection = domainRedirections => domain => find(propEq('from', domain), domainRedirections)
// getRedirection :: [Object] -> Request -> Object|Null
const getRedirection = redirections => req => find(
({ from }) => test(new RegExp(`^${from}`, 'i'), req.url()),
redirections,
)

// allowRequest :: Request -> _
const allowRequest = domainRedirections => req => pipe(
resolveRequestDomain,
getDomainRedirection(domainRedirections),
// allowRequest :: (logger, [Object]) -> Request -> _
const allowRequest = (logger, redirections) => req => pipe(
getRedirection(redirections),
ifElse(
isNil,
() => req.continue(),
({ from, to }) => req.continue({
url: req.url().replace(from, to),
}),
pipe(
({ from, to }) => replace(new RegExp(`^${from}`, 'i'), to, req.url()),
tap(redirectedUrl => logger.debug(`Redirecting "${req.url()}" to ${redirectedUrl}`)),
redirectedUrl => req.continue({ url: redirectedUrl }),
),
),
)(req)

Expand All @@ -58,5 +62,5 @@ export default (configuration, logger) => cond([
complement(isRequestResourceAuthorized(configuration.worker.renderer.authorized_request_resources)),
blockRequest(logger, 'resource type'),
],
[T, allowRequest(configuration.worker.renderer.domain_redirections)],
[T, allowRequest(logger, configuration.worker.renderer.redirections)],
])
8 changes: 6 additions & 2 deletions src/worker/renderers/chrome/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apply } from 'ramda'
import { apply, map, pipe } from 'ramda'
import browserRequestHandler from './browserRequestHandler'
import { formatException } from './../../../logger'
import getBrowserProvider from './browserProvider'
Expand All @@ -13,7 +13,11 @@ const renderPageContent = async (configuration, logger, browserInstance, url) =>
page.on('error', error => logger.error(formatException(error)))
page.on('pageerror', error => logger.error(formatException(error)))
page.on('requestfailed', req => logger.debug(`Browser request failed. ${req.url()}.`))
page.on('console', msg => apply(logger.debug, msg.args()))
page.on('console', pipe(
msg => msg.args(),
map(arg => arg.toString()),
apply(logger.debug),
))

await page.goto(url, {
waitUntil: 'networkidle0',
Expand Down

0 comments on commit 90d674e

Please sign in to comment.