-
Notifications
You must be signed in to change notification settings - Fork 314
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
ESC 399 - Enhance redirect URL handling and MentionParser functionality #639
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,13 +9,20 @@ class MentionParser | |||||||||||||||||||||||
{ | ||||||||||||||||||||||||
private $content; | ||||||||||||||||||||||||
private $data; | ||||||||||||||||||||||||
private $urlFriendly = false; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public function __construct($content, $data) | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
$this->content = $content; | ||||||||||||||||||||||||
$this->data = $data; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public function urlFriendlyOutput(bool $enable = true): self | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
$this->urlFriendly = $enable; | ||||||||||||||||||||||||
return $this; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public function parse() | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
$doc = new DOMDocument(); | ||||||||||||||||||||||||
|
@@ -40,7 +47,7 @@ public function parse() | |||||||||||||||||||||||
$value = $this->getData($fieldId); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ($value !== null) { | ||||||||||||||||||||||||
$textNode = $doc->createTextNode(is_array($value) ? implode(', ', $value) : $value); | ||||||||||||||||||||||||
$textNode = $doc->createTextNode(is_array($value) ? implode($this->urlFriendly ? ',+' : ', ', $value) : $value); | ||||||||||||||||||||||||
$element->parentNode->replaceChild($textNode, $element); | ||||||||||||||||||||||||
} elseif ($fallback) { | ||||||||||||||||||||||||
$textNode = $doc->createTextNode($fallback); | ||||||||||||||||||||||||
|
@@ -127,7 +134,13 @@ private function getData($fieldId) | |||||||||||||||||||||||
$value = collect($this->data)->firstWhere('id', $fieldId)['value'] ?? null; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if (is_object($value)) { | ||||||||||||||||||||||||
return (array) $value; | ||||||||||||||||||||||||
$value = (array) $value; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ($this->urlFriendly && $value !== null) { | ||||||||||||||||||||||||
return is_array($value) | ||||||||||||||||||||||||
? array_map('urlencode', $value) | ||||||||||||||||||||||||
: urlencode($value); | ||||||||||||||||||||||||
Comment on lines
+140
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add safeguards against double URL encoding. The current implementation might double-encode values if they're already URL-encoded. Consider adding a check to prevent this. if ($this->urlFriendly && $value !== null) {
+ $encode = function($str) {
+ return rawurlencode(rawurldecode($str));
+ };
return is_array($value)
- ? array_map('urlencode', $value)
- : urlencode($value);
+ ? array_map($encode, $value)
+ : $encode($value);
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return $value; | ||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class () extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('forms', function (Blueprint $table) { | ||
$table->text('redirect_url')->nullable()->change(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('forms', function (Blueprint $table) { | ||
$table->string('redirect_url')->nullable()->change(); | ||
}); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
test('form accepts long redirect urls', function () { | ||
$this->withoutExceptionHandling(); | ||
$user = $this->actingAsUser(); | ||
$workspace = $this->createUserWorkspace($user); | ||
$form = $this->createForm($user, $workspace); | ||
|
||
// Create a very long URL (more than 255 characters) | ||
$longUrl = 'https://example.com/?' . str_repeat('very-long-parameter=value&', 50); | ||
|
||
$this->putJson(route('open.forms.update', $form->id), array_merge($form->toArray(), [ | ||
'redirect_url' => $longUrl | ||
]))->assertStatus(200); | ||
|
||
expect($form->fresh()->redirect_url)->toBe($longUrl); | ||
}); | ||
|
||
test('form accepts null redirect url', function () { | ||
$user = $this->actingAsUser(); | ||
$workspace = $this->createUserWorkspace($user); | ||
$form = $this->createForm($user, $workspace); | ||
|
||
$this->putJson(route('open.forms.update', $form->id), array_merge($form->toArray(), [ | ||
'redirect_url' => null | ||
]))->assertStatus(200); | ||
|
||
expect($form->fresh()->redirect_url)->toBeNull(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding URL validation and reasonable length constraints.
While removing the 255-character limit is necessary for longer URLs, consider:
📝 Committable suggestion