Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] website: prevent creation of 308 to existing controller #165083

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions addons/website/i18n/website.pot
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ msgstr ""
msgid "\"URL from\" can not be empty."
msgstr ""

#. module: website
#. odoo-python
#: code:addons/website/models/website_rewrite.py:0
#, python-format
msgid ""
"\"URL to\" cannot be set to \"/\". To change the homepage content, use the "
"\"Homepage URL\" field in the website settings or the page properties on any"
" custom page."
msgstr ""

#. module: website
#. odoo-python
#: code:addons/website/models/website_rewrite.py:0
#, python-format
msgid "\"URL to\" cannot be set to an existing page."
msgstr ""

#. module: website
#. odoo-python
#: code:addons/website/models/website_rewrite.py:0
Expand Down
12 changes: 12 additions & 0 deletions addons/website/models/website_rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ def _check_url_to(self):
for param in re.findall('/<.*?>', rewrite.url_to):
if param not in rewrite.url_from:
raise ValidationError(_('"URL to" cannot contain parameter %s which is not used in "URL from".', param))

if rewrite.url_to == '/':
raise ValidationError(_('"URL to" cannot be set to "/". To change the homepage content, use the "Homepage URL" field in the website settings or the page properties on any custom page.'))

if any(
rule for rule in self.env['ir.http'].routing_map().iter_rules()
# Odoo routes are normally always defined without trailing
# slashes + strict_slashes=False, but there are exceptions.
if rule.rule.rstrip('/') == rewrite.url_to.rstrip('/')
):
raise ValidationError(_('"URL to" cannot be set to an existing page.'))

try:
converters = self.env['ir.http']._get_converters()
routing_map = werkzeug.routing.Map(strict_slashes=False, converters=converters)
Expand Down
1 change: 1 addition & 0 deletions addons/website/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from . import test_page_manager
from . import test_performance
from . import test_qweb
from . import test_redirect
from . import test_res_users
from . import test_snippets
from . import test_theme
Expand Down
35 changes: 35 additions & 0 deletions addons/website/tests/test_redirect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo.models import ValidationError
from odoo.tests import TransactionCase, tagged


@tagged('-at_install', 'post_install')
class TestWebsiteRedirect(TransactionCase):
def test_01_website_redirect_validation(self):
with self.assertRaises(ValidationError) as error:
self.env['website.rewrite'].create({
'name': 'Test Website Redirect',
'redirect_type': '308',
'url_from': '/website/info',
'url_to': '/',
})
self.assertIn('homepage', str(error.exception))

with self.assertRaises(ValidationError) as error:
self.env['website.rewrite'].create({
'name': 'Test Website Redirect',
'redirect_type': '308',
'url_from': '/website/info',
'url_to': '/favicon.ico',
})
self.assertIn('existing page', str(error.exception))

with self.assertRaises(ValidationError) as error:
self.env['website.rewrite'].create({
'name': 'Test Website Redirect',
'redirect_type': '308',
'url_from': '/website/info',
'url_to': '/favicon.ico/', # trailing slash on purpose
})
self.assertIn('existing page', str(error.exception))