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

Flarum throws error when calling $post->content in Saving listener #52

Open
clarkwinkelmann opened this issue Apr 29, 2024 · 0 comments
Assignees
Labels

Comments

@clarkwinkelmann
Copy link
Member

Bug Report

Current Behavior
This is the problem being reported and discussed on the forums https://discuss.flarum.org/d/34454-every-day-there-are-some-discussions-only-have-title-no-content/11

If a user tries to send an empty reply, or a new discussion with empty body while this extension is active, a 500 error will occur.

[2024-04-29 21:05:18] flarum.ERROR: TypeError: Flarum\Mentions\Formatter\UnparsePostMentions::__invoke(): Argument #2 ($xml) must be of type string, null given, called in /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Foundation/ContainerUtil.php on line 30 and defined in /home/clark/Projects/flarum-1.8/vendor/flarum/mentions/src/Formatter/UnparsePostMentions.php:34
Stack trace:
#0 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Foundation/ContainerUtil.php(30): Flarum\Mentions\Formatter\UnparsePostMentions->__invoke()
#1 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Formatter/Formatter.php(139): Flarum\Foundation\ContainerUtil::Flarum\Foundation\{closure}()
#2 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Post/CommentPost.php(134): Flarum\Formatter\Formatter->unparse()
#3 /home/clark/Projects/flarum-1.8/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php(620): Flarum\Post\CommentPost->getContentAttribute()
#4 /home/clark/Projects/flarum-1.8/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php(1991): Illuminate\Database\Eloquent\Model->mutateAttribute()
#5 /home/clark/Projects/flarum-1.8/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php(451): Illuminate\Database\Eloquent\Model->transformModelValue()
#6 /home/clark/Projects/flarum-1.8/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php(430): Illuminate\Database\Eloquent\Model->getAttributeValue()
#7 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Database/AbstractModel.php(135): Illuminate\Database\Eloquent\Model->getAttribute()
#8 /home/clark/Projects/flarum-1.8/vendor/illuminate/database/Eloquent/Model.php(2029): Flarum\Database\AbstractModel->getAttribute()
#9 /home/clark/Projects/flarum-1.8/workbench/filter/src/Listener/CheckPost.php(64): Illuminate\Database\Eloquent\Model->__get()
#10 /home/clark/Projects/flarum-1.8/vendor/illuminate/events/Dispatcher.php(424): FoF\Filter\Listener\CheckPost->handle()
#11 /home/clark/Projects/flarum-1.8/vendor/illuminate/events/Dispatcher.php(249): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}()
#12 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Post/Command/PostReplyHandler.php(97): Illuminate\Events\Dispatcher->dispatch()
#13 /home/clark/Projects/flarum-1.8/vendor/illuminate/bus/Dispatcher.php(122): Flarum\Post\Command\PostReplyHandler->handle()
#14 /home/clark/Projects/flarum-1.8/vendor/illuminate/pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}()
#15 /home/clark/Projects/flarum-1.8/vendor/illuminate/pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#16 /home/clark/Projects/flarum-1.8/vendor/illuminate/bus/Dispatcher.php(132): Illuminate\Pipeline\Pipeline->then()
#17 /home/clark/Projects/flarum-1.8/vendor/illuminate/bus/Dispatcher.php(78): Illuminate\Bus\Dispatcher->dispatchNow()
#18 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Discussion/Command/StartDiscussionHandler.php(82): Illuminate\Bus\Dispatcher->dispatch()
#19 /home/clark/Projects/flarum-1.8/vendor/illuminate/bus/Dispatcher.php(122): Flarum\Discussion\Command\StartDiscussionHandler->handle()
#20 /home/clark/Projects/flarum-1.8/vendor/illuminate/pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}()
#21 /home/clark/Projects/flarum-1.8/vendor/illuminate/pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#22 /home/clark/Projects/flarum-1.8/vendor/illuminate/bus/Dispatcher.php(132): Illuminate\Pipeline\Pipeline->then()
#23 /home/clark/Projects/flarum-1.8/vendor/illuminate/bus/Dispatcher.php(78): Illuminate\Bus\Dispatcher->dispatchNow()
#24 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Api/Controller/CreateDiscussionController.php(62): Illuminate\Bus\Dispatcher->dispatch()
#25 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Api/Controller/AbstractSerializeController.php(116): Flarum\Api\Controller\CreateDiscussionController->data()
#26 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Api/Controller/AbstractCreateController.php(22): Flarum\Api\Controller\AbstractSerializeController->handle()
#27 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/RouteHandlerFactory.php(41): Flarum\Api\Controller\AbstractCreateController->handle()
#28 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/ExecuteRoute.php(27): Flarum\Http\RouteHandlerFactory->Flarum\Http\{closure}()
#29 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\ExecuteRoute->process()
#30 /home/clark/Projects/flarum-1.8/workbench/flarum-ext-audit-free/src/Middlewares/SetLoggerActor.php(28): Laminas\Stratigility\Next->handle()
#31 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Kilowhat\Audit\Middlewares\SetLoggerActor->process()
#32 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Api/Middleware/ThrottleApi.php(33): Laminas\Stratigility\Next->handle()
#33 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Api\Middleware\ThrottleApi->process()
#34 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/CheckCsrfToken.php(44): Laminas\Stratigility\Next->handle()
#35 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\CheckCsrfToken->process()
#36 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/ResolveRoute.php(69): Laminas\Stratigility\Next->handle()
#37 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\ResolveRoute->process()
#38 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/SetLocale.php(51): Laminas\Stratigility\Next->handle()
#39 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\SetLocale->process()
#40 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/AuthenticateWithHeader.php(58): Laminas\Stratigility\Next->handle()
#41 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\AuthenticateWithHeader->process()
#42 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/AuthenticateWithSession.php(31): Laminas\Stratigility\Next->handle()
#43 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\AuthenticateWithSession->process()
#44 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/RememberFromCookie.php(52): Laminas\Stratigility\Next->handle()
#45 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\RememberFromCookie->process()
#46 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/StartSession.php(61): Laminas\Stratigility\Next->handle()
#47 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\StartSession->process()
#48 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Api/Middleware/FakeHttpMethods.php(29): Laminas\Stratigility\Next->handle()
#49 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Api\Middleware\FakeHttpMethods->process()
#50 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/ParseJsonBody.php(28): Laminas\Stratigility\Next->handle()
#51 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\ParseJsonBody->process()
#52 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/HandleErrors.php(57): Laminas\Stratigility\Next->handle()
#53 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\HandleErrors->process()
#54 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/InjectActorReference.php(25): Laminas\Stratigility\Next->handle()
#55 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\InjectActorReference->process()
#56 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/MiddlewarePipe.php(75): Laminas\Stratigility\Next->handle()
#57 /home/clark/Projects/flarum-1.8/vendor/middlewares/request-handler/src/RequestHandler.php(84): Laminas\Stratigility\MiddlewarePipe->process()
#58 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Middlewares\RequestHandler->process()
#59 /home/clark/Projects/flarum-1.8/vendor/middlewares/base-path-router/src/BasePathRouter.php(101): Laminas\Stratigility\Next->handle()
#60 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Middlewares\BasePathRouter->process()
#61 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Middleware/OriginalMessages.php(36): Laminas\Stratigility\Next->handle()
#62 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Laminas\Stratigility\Middleware\OriginalMessages->process()
#63 /home/clark/Projects/flarum-1.8/vendor/middlewares/base-path/src/BasePath.php(73): Laminas\Stratigility\Next->handle()
#64 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Middlewares\BasePath->process()
#65 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Middleware/ProcessIp.php(24): Laminas\Stratigility\Next->handle()
#66 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/Next.php(49): Flarum\Http\Middleware\ProcessIp->process()
#67 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/MiddlewarePipe.php(75): Laminas\Stratigility\Next->handle()
#68 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-stratigility/src/MiddlewarePipe.php(64): Laminas\Stratigility\MiddlewarePipe->process()
#69 /home/clark/Projects/flarum-1.8/vendor/laminas/laminas-httphandlerrunner/src/RequestHandlerRunner.php(73): Laminas\Stratigility\MiddlewarePipe->handle()
#70 /home/clark/Projects/flarum-1.8/vendor/flarum/core/src/Http/Server.php(45): Laminas\HttpHandlerRunner\RequestHandlerRunner->run()
#71 /home/clark/Projects/flarum-1.8/public/index.php(26): Flarum\Http\Server->listen()
#72 {main}

Steps to Reproduce

  1. Enable FoF Filter and Flarum Mentions
  2. No need to set any setting
  3. Switch to a user without bypassFoFFilter permission
  4. Try to create discussion with empty body

Expected Behavior
The behavior from Flarum isn't entirely clear here.

$post->content should return either null or an empty string. Definitely not an error.

The extension uses the method "correctly" in that regard. It shouldn't error.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment

  • Flarum version: 1.8.5
  • Extension version: 1.1.3, dev-master
  • Website URL: http://example.com
  • Webserver: apache
  • Hosting environment: local
  • PHP version: 8.1.28
Output of "php flarum info", run this in terminal in your Flarum directory.

Possible solution(s)
Ideally, this should be fixed in Flarum.

Since CommentPost::setContentAttribute() sets the content attribute to null when falsy without going through the formatter parser, it would be logical for CommentPost::getContentAttribute() to also return null verbatim without going through the unparser.

In Flarum v2, the signature to $formatter->unparse has already been changed to only accept string, so the error will look different on Flarum v2 as it will error one step earlier.

No idea if this will warrant a hotfix on Flarum 1.8.x or if this will have to wait to be fixed in v2.

Additional Context

The error is triggered here but it happens as soon as $post->content is invoked, the code from FoF Filter isn't at fault.

if ($this->checkContent($post->content)) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants