-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Refactor some Str2html code #29397
Refactor some Str2html code #29397
Conversation
70d39fc
to
d69dbbe
Compare
d69dbbe
to
18c5653
Compare
Ideally we should ensure that Go's contextual auto-escaping works wherever possible to avoid needing such helpers. Are the limitations of this auto-escaper well-known? |
What I am doing is this. You can see we don't need "Safe" or "Escape" in templates anymore. "limitations of this auto-escaper": there are many problems: it has limitations, that's why there were a lot of "printf" "Escape" "Safe" and "QueryEscape" in code. Some could be improved by our framework code, some can't be avoided. |
in my opinion no ... it's to error prone |
* giteaofficial/main: Set pre-step status to `skipped` if job is skipped (go-gitea#29489) Use a predictiable fork URL to allow forking repositories without providing a repo ID (go-gitea#29519) Adding back missing options to app.example.ini (go-gitea#29511) Refactor the "attachments" sub-template data key to RenderedContent (go-gitea#29517) Rename Str2html to SanitizeHTML and clarify its behavior (go-gitea#29516) Add admin API route for managing user's badges (go-gitea#23106) Refactor some Str2html code (go-gitea#29397) Move migration functions to services layer (go-gitea#29497)
This PR touches the most interesting part of the "template refactoring".
Before, "Tr", "Escape" and "Safe" are quite straightforward, they just escapes and casts.
But "Str2html" is special: it sanitizes the input and casts the "safe" result to HTML type.
During the refactoring, I can see that there are many legacy problems, for example:
project.RenderedContent = project.Description
in web/org/projects.gor.Note = rendered content
in web/repo/release.go{{Str2html .PackageDescriptor.Metadata.ReleaseNotes}}
in package/content/nuget.tmpl, I guess the name Str2html misleads developers to use it to "render string to html", but it only sanitizes .... well, if ReleaseNotes really contains HTML, then this is not a problem, but I haven't figured out it yet.There is still a question: should we encourage to use "Str2html" in templates in the future? Actually, variables like "RenderedContent" are generated by "markup.Render", it has already been sanitized by the renderer. Although double-sanitizing doesn't cause side-effect, I don't know whether people would to keep using it in templates (for a sense of security) or just only use it when it is necessary (for making code to have clearly defined behaviors)