Add allowed paths to backend.reading.allow

Signed-off-by: Gabriele Mambrini <gabriele.mambrini@jimdo.com>
This commit is contained in:
Gabriele Mambrini
2021-11-05 09:52:50 +01:00
parent a62bca43f8
commit b8b67b5740
2 changed files with 20 additions and 2 deletions
@@ -77,6 +77,10 @@ describe('FetchUrlReader', () => {
{ host: 'example.com:700' },
{ host: '*.examples.org' },
{ host: '*.examples.org:700' },
{
host: 'foobar.org',
paths: ['/dir1/'],
},
],
},
},
@@ -106,6 +110,9 @@ describe('FetchUrlReader', () => {
expect(predicate(new URL('https://examples.org:700/test'))).toBe(false);
expect(predicate(new URL('https://a.examples.org:700/test'))).toBe(true);
expect(predicate(new URL('https://a.b.examples.org:700/test'))).toBe(true);
expect(predicate(new URL('https://foobar.org/dir1/subpath'))).toBe(true);
expect(predicate(new URL('https://foobar.org/dir12'))).toBe(false);
expect(predicate(new URL('https://foobar.org/'))).toBe(false);
});
describe('read', () => {
@@ -24,6 +24,7 @@ import {
SearchResponse,
UrlReader,
} from './types';
import { normalize as normalizePath } from 'path';
/**
* A UrlReader that does a plain fetch of the URL.
@@ -39,18 +40,28 @@ export class FetchUrlReader implements UrlReader {
* `host`:
* Either full hostnames to match, or subdomain wildcard matchers with a leading `*`.
* For example `example.com` and `*.example.com` are valid values, `prod.*.example.com` is not.
*
* `paths`:
* An optional list of paths which are allowed. If the list is omitted all paths are allowed.
*/
static factory: ReaderFactory = ({ config }) => {
const predicates =
config
.getOptionalConfigArray('backend.reading.allow')
?.map(allowConfig => {
const paths = allowConfig.getOptionalStringArray('paths');
const checkPath = paths
? (url: URL) => {
const targetPath = normalizePath(url.pathname);
return paths.some(path => targetPath.startsWith(path));
}
: (_url: URL) => true;
const host = allowConfig.getString('host');
if (host.startsWith('*.')) {
const suffix = host.slice(1);
return (url: URL) => url.host.endsWith(suffix);
return (url: URL) => url.host.endsWith(suffix) && checkPath(url);
}
return (url: URL) => url.host === host;
return (url: URL) => url.host === host && checkPath(url);
}) ?? [];
const reader = new FetchUrlReader();