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

Incorrect quotes in translate helper causes 500 error #15500

Closed
ErisDS opened this issue Sep 29, 2022 · 3 comments
Closed

Incorrect quotes in translate helper causes 500 error #15500

ErisDS opened this issue Sep 29, 2022 · 3 comments
Labels
bug [triage] something behaving unexpectedly Hacktoberfest Issues suitable for hacktoberfest participants help wanted [triage] Ideal issues for contributors to help with

Comments

@ErisDS
Copy link
Member

ErisDS commented Sep 29, 2022

Issue Summary

Using smart quotes in the translate helper causes page visits to throw 500 errors. Mac OS for example will auto-convert quotes by default, and this a common and VERY hard to spot issue for people new to code.

Smart quotes example: {{t “This causes an error”}}
Straight quotes example: {{t "This causes an error"}}

As handlebars would always expect straight quotes, I suspect this is true for several helpers.

Although the fact that this is an error is correct, we have a policy of never throwing 500 Internal Server errors for theme issues, but instead throw 400 Bad Request errors.

This is slightly odd, but nothing is wrong with the server, it's doing everything correctly and therefore this is not an error that the person running Ghost needs to deal with, but rather the user who uploaded the theme.

In this case, the error can probably be handled better.

Steps to Reproduce

  1. Open the default.hbs file of the current active theme
  2. Paste in the example translate helper with smart quotes {{t “This causes an error”}}
  3. Try to visit any page
  4. See the 500 error in the logs

Ghost Version

4.42.1

Node.js Version

16.14.2

How did you install Ghost?

ghost install

Database type

MySQL 8

Browser & OS version

Any

Relevant log / error output

HTTP/1.1 500 Internal Server Error
    --
    TypeError: [default.hbs] Cannot read properties of undefined (reading 'hash')
        at Object.t (/home/ghost/core/frontend/helpers/t.js:18:26)
        at Object.wrapper (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/internal/wrapHelper.js:15:19)
        at Object.eval [as main] (eval at createFunctionContext (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:262:23), <anonymous>:13:92)
        at main (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/runtime.js:208:32)
        at ret (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/runtime.js:212:12)
        at ret (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:519:21)
        at Object.invokePartial (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/runtime.js:334:12)
        at Object.invokePartialWrapper [as invokePartial] (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/runtime.js:84:39)
        at Object.eval [as main] (eval at createFunctionContext (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:262:23), <anonymous>:39:28)
        at main (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/runtime.js:208:32)
        at ret (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/runtime.js:212:12)
        at ret (/home/ghost/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:519:21)
        at renderTemplate (/home/ghost/node_modules/express-hbs/lib/hbs.js:490:13)
        at _stackRenderer (/home/ghost/node_modules/express-hbs/lib/hbs.js:519:9)
        at renderTemplate (/home/ghost/node_modules/express-hbs/lib/hbs.js:499:5)
        at render (/home/ghost/node_modules/express-hbs/lib/hbs.js:526:5)


### Code of Conduct

- [X] I agree to be friendly and polite to people in this repository
@github-actions github-actions bot added the needs:triage [triage] this needs to be triaged by the Ghost team label Sep 29, 2022
@ErisDS ErisDS added bug [triage] something behaving unexpectedly themes / frontend help wanted [triage] Ideal issues for contributors to help with Hacktoberfest Issues suitable for hacktoberfest participants labels Sep 29, 2022
@github-actions github-actions bot removed the needs:triage [triage] this needs to be triaged by the Ghost team label Sep 29, 2022
@shivamchourasia4
Copy link

I would like to work on this. Can you assign this to me?

@ErisDS
Copy link
Member Author

ErisDS commented Oct 2, 2022

@shivamchourasia4 we only assign issues to regular contributors. There is no open PR for this issue so it's free to work on.

@iBotPeaches
Copy link
Contributor

[ghost] [2022-10-03 11:16:14] ERROR "GET /" 400 60ms
[ghost] 
[ghost] [default.hbs] Cannot read properties of undefined (reading 'hash')
[ghost] 
[ghost] Error ID:
[ghost]     cb11e6d0-430c-11ed-ab42-97a8b8925e55
[ghost] 
[ghost] ----------------------------------------
[ghost] 
[ghost] TypeError: [default.hbs] Cannot read properties of undefined (reading 'hash')
[ghost]     at module.exports.prepareError (/home/ibotpeaches/Desktop/Projects/Node/Ghost/ghost/mw-error-handler/lib/mw-error-handler.js:74:19)
[ghost]     at Object.t (/home/ibotpeaches/Desktop/Projects/Node/Ghost/ghost/core/core/frontend/helpers/t.js:18:26)
[ghost]     at Object.wrapper (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/handlebars/dist/cjs/handlebars/internal/wrapHelper.js:15:19)
[ghost]     at Object.eval [as main] (eval at createFunctionContext (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:262:23), <anonymous>:44:92)
[ghost]     at main (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/handlebars/dist/cjs/handlebars/runtime.js:208:32)
[ghost]     at ret (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/handlebars/dist/cjs/handlebars/runtime.js:212:12)
[ghost]     at ret (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:519:21)
[ghost]     at renderTemplate (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/express-hbs/lib/hbs.js:490:13)
[ghost]     at _stackRenderer (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/express-hbs/lib/hbs.js:519:9)
[ghost]     at renderTemplate (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/express-hbs/lib/hbs.js:499:5)
[ghost]     at render (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/express-hbs/lib/hbs.js:526:5)
[ghost]     at renderIt (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/express-hbs/lib/hbs.js:588:18)
[ghost]     at /home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/express-hbs/lib/hbs.js:598:11
[ghost]     at _returnLayouts (/home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/express-hbs/lib/hbs.js:124:7)
[ghost]     at /home/ibotpeaches/Desktop/Projects/Node/Ghost/node_modules/express-hbs/lib/hbs.js:135:7
[ghost]     at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)
[ghost] 

This looks good on 5.17.x

image

However, I just realized this ticket called out 4.x as the affected version. So I'll revisit this when I checkout 4.x.

cweave added a commit to cweave/Ghost that referenced this issue Oct 5, 2022
closes TryGhost#15500
Per the issue, Ghost has a policy to never throw 500 Internal Server errors for theme issues. This change adds a check inside of `ghost\core\core\frontend\helpers\t.js` if `text` or `options` is undefined, to throw an `IncorrectUsageError` error within the function.

Messaging was borrowed from `ghost\core\core\frontend\web\middleware\error-handler.js`.
@ErisDS ErisDS closed this as completed in 87d2166 Oct 12, 2022
cweave added a commit to cweave/Ghost that referenced this issue Oct 13, 2022
Added unit tests to the MR TryGhost#15529 / issue TryGhost#15500. Added a regex in the `t()` to explicitly check for smart apostrophes in the event that they are not stripped out/come through as undefined.
sam-lord pushed a commit that referenced this issue Oct 17, 2022
closes: #15500

- Per the issue, Ghost has a policy to never throw 500 Internal Server errors for theme issues. This change adds a check inside of `ghost\core\core\frontend\helpers\t.js` if `text` or `options` is undefined, to throw an `IncorrectUsageError` error within the function.
- Messaging was borrowed from `ghost\core\core\frontend\web\middleware\error-handler.js`.
sagzy added a commit that referenced this issue Jul 25, 2024
…20663)

ref https://linear.app/tryghost/issue/SLO-182
ref #15500

- when the {{ t }} helper is used with no parameter or an empty string,
it now returns an empty string
- when the {{ t }} helper is used without options, it now does not throw
an error
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug [triage] something behaving unexpectedly Hacktoberfest Issues suitable for hacktoberfest participants help wanted [triage] Ideal issues for contributors to help with
Projects
None yet
Development

No branches or pull requests

3 participants