Skip to content

Commit

Permalink
Simplify HOST_NAME_PATTERN
Browse files Browse the repository at this point in the history
  • Loading branch information
jirik authored and index-git committed Oct 2, 2023
1 parent ef70e26 commit c4de052
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/client-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ By default, Layman will not adjust URLs in its response to contain also URL path

Layman supports three optional X-Forwarded HTTP headers, whose values will be used in some URLs in Layman responses:
- `X-Forwarded-Proto`: The value will be used as protocol in some URLs, and it is required to be `http` or `https`.
- `X-Forwarded-Host`: The value will be used as host in some URLs, and it is required to match regular expression `^(?=.{1,253}\.?(?:\:[0-9]{1,5})?$)(?:(?!-)[a-z0-9-_]{1,63}(?<!-)(?:\.|(?:\:[0-9]{1,5})?$))+$`.
- `X-Forwarded-Host`: The value will be used as host in some URLs, and it is required to match regular expression `^(?=.{1,253}(?:\:|$))(?:(?!-)[a-z0-9-_]{1,63}(?<!-)(?:\.|(?:\:[0-9]{1,5})?$))+$`.
- `X-Forwarded-Prefix`: The value will be used as prefix in some URL paths, and it is required to match regular expression `^(?:/[a-z0-9_-]+)*$`.

For example, consider there is layman running at `https://enjoychallenge.tech/rest` and client proxy running at `https://laymanproxy.com/layman-client-proxy`. If you send request to your Layman proxy `https://laymanproxy.com/layman-client-proxy/rest/publications` with HTTP headers
Expand Down
2 changes: 1 addition & 1 deletion src/layman/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
HEADER_X_FORWARDED_PROTO_KEY = 'X-Forwarded-Proto'
HEADER_X_FORWARDED_HOST_KEY = 'X-Forwarded-Host'
HEADER_X_FORWARDED_PREFIX_KEY = 'X-Forwarded-Prefix'
HOST_NAME_PATTERN = r'^(?=.{1,253}\.?(?:\:[0-9]{1,5})?$)(?:(?!-)[a-z0-9-_]{1,63}(?<!-)(?:\.|(?:\:[0-9]{1,5})?$))+$'
HOST_NAME_PATTERN = r'^(?=.{1,253}(?:\:|$))(?:(?!-)[a-z0-9-_]{1,63}(?<!-)(?:\.|(?:\:[0-9]{1,5})?$))+$'


class SimpleStorage:
Expand Down
4 changes: 2 additions & 2 deletions src/layman/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def test_get_x_forwarded_items(headers, exp_result):
'data': {
'header': 'X-Forwarded-Host',
'message': 'Optional header X-Forwarded-Host contains unsupported value.',
'expected': r'Expected header matching regular expression ^(?=.{1,253}\.?(?:\:[0-9]{1,5})?$)(?:(?!-)[a-z0-9-_]{1,63}(?<!-)(?:\.|(?:\:[0-9]{1,5})?$))+$',
'expected': r'Expected header matching regular expression ^(?=.{1,253}(?:\:|$))(?:(?!-)[a-z0-9-_]{1,63}(?<!-)(?:\.|(?:\:[0-9]{1,5})?$))+$',
'found': 'ABZ.COM',
},
}, id='uppercase-host'),
Expand All @@ -239,7 +239,7 @@ def test_get_x_forwarded_items(headers, exp_result):
'data': {
'header': 'X-Forwarded-Host',
'message': 'Optional header X-Forwarded-Host contains unsupported value.',
'expected': r'Expected header matching regular expression ^(?=.{1,253}\.?(?:\:[0-9]{1,5})?$)(?:(?!-)[a-z0-9-_]{1,63}(?<!-)(?:\.|(?:\:[0-9]{1,5})?$))+$',
'expected': r'Expected header matching regular expression ^(?=.{1,253}(?:\:|$))(?:(?!-)[a-z0-9-_]{1,63}(?<!-)(?:\.|(?:\:[0-9]{1,5})?$))+$',
'found': '',
},
}, id='empty-host'),
Expand Down

0 comments on commit c4de052

Please sign in to comment.