Skip to content

Comments

Added additional test case for UrlGenerator class#90

Merged
LukeTowers merged 3 commits intowintercms:developfrom
arvislacis:more-url-tests
Jun 26, 2022
Merged

Added additional test case for UrlGenerator class#90
LukeTowers merged 3 commits intowintercms:developfrom
arvislacis:more-url-tests

Conversation

@arvislacis
Copy link
Contributor

I tested the latest 32df10c commit and it seems that issues described in wintercms/wn-translate-plugin#36 is not solved.

So I added additional test case.

@arvislacis
Copy link
Contributor Author

Added test cases fail so issue still exists (or I made super dummy mistake in these tests).

wintercms/wn-translate-plugin#36 issue also mostly focus about this case because although initial filter URL is like https://www.example.com/path?array[]=v1&array[]=v2 - later it gets replaced to https://www.example.com/path?array[0]=v1&array[1]=v2 because of language switch component... https://www.example.com/path?array[]=v1&array[]=v2 and https://www.example.com/path?array[0]=v1&array[1]=v2 is same but Redirect::to() then translates it to https://www.example.com/path?array[0][]=v1&array[1][]=v2 which is not correct.

@mjauvin
Copy link
Member

mjauvin commented Jun 26, 2022

I confirm.

@mjauvin
Copy link
Member

mjauvin commented Jun 26, 2022

@arvislacis please add this to this PR, I think it fixes the issue:

diff --git a/src/Router/UrlGenerator.php b/src/Router/UrlGenerator.php
index 5c6f4a8..7268b46 100644
--- a/src/Router/UrlGenerator.php
+++ b/src/Router/UrlGenerator.php
@@ -326,7 +326,7 @@ public static function buildStr(array $query, string $prefix = '', $argSeparator
             if ($i === $k) {
                 if ($prefix) {
                     // Make sure the key is setup for array values
-                    if (Str::endsWith($prefix, '[]')) {
+                    if (Str::endsWith($prefix, ']')) {
                         $key = $prefix;
                     } else {
                         $key = "{$prefix}[]";

@arvislacis
Copy link
Contributor Author

@mjauvin Done in 5be073c As far as I tested then this solves my issue (wintercms/wn-translate-plugin#36) but I would suggest someone else also to take look.

Also added another test case (f329c19) for

$key = $prefix ? "{$prefix}[{$k}]" : $k;

^ Everything is fine for this, I think test cases wouldn't be bad.

Probably there could also be tests for

$key = "{$prefix}[]";
and although at the moment I couldn't think of any real life examples.

@arvislacis
Copy link
Contributor Author

@mjauvin A little bit off-topic but is there any particular reason Winter CMS has implemented it's own URL processing and decoding, encoding and not using the Laravel one? I understand that users are nasty and can insert a lot of interesting things in URLs so decoding/encoding and additional processing may avoid this.

For example, if we look at my Locale Switcher case then for some users it also may look strange that [] gets encoded and displayed as %5B0%5D - of course that's nothing too critical but still...

@LukeTowers
Copy link
Member

@arvislacis it's because Laravel does not do any of the parsing or url encoding / decoding of URLs that Winter is doing. There is no guarantee in Laravel when you passing a value to Url::to() (or any variation) that you will get a valid, safe URL back.

I personally wanted a helper that would guarantee that the output generated by it was a spec-following URL that could be safely used in element attributes and anywhere else you might need a URL without worrying about potential injection attacks from users controlling part of or the entire URL.

@LukeTowers LukeTowers modified the milestones: v1.2.0, v1.1.9 Jun 26, 2022
@arvislacis
Copy link
Contributor Author

@LukeTowers Thanks for clarification.

@LukeTowers LukeTowers merged commit c15512e into wintercms:develop Jun 26, 2022
@LukeTowers
Copy link
Member

Thanks for the help @arvislacis & @mjauvin!

LukeTowers added a commit that referenced this pull request Jun 26, 2022
* develop:
  Added additional test case for UrlGenerator class (#90)
  Improve port validation when building URLs
LukeTowers added a commit that referenced this pull request Jul 15, 2022
* develop:
  Apply fix from 1.0
  Add test case for trimStringAttributes flag
  Make string attribute value auto-trimming optional (#97)
  Added additional test case for UrlGenerator class (#90)
  Improve port validation when building URLs
  Avoid issues coming from double encoded URLs
  remove non-existent argument to Ini::parse() method (#82)
  Fix docblock typehint (#71)
  Code quality and windows test fix
  Improve Halcyon SectionParser logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants