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

Refactor some Str2html code #29397

Merged
merged 5 commits into from
Mar 1, 2024
Merged

Conversation

wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented Feb 25, 2024

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:

  1. Unclear variable type. Especially for "web/feed/convert.go": sometimes it uses text, sometimes it uses HTML.
  2. Assign text content to "RenderedContent" field, for example: project.RenderedContent = project.Description in web/org/projects.go
  3. Assign rendered content to text field, for example: r.Note = rendered content in web/repo/release.go
  4. (possible) Incorrectly calling {{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)

@wxiaoguang wxiaoguang added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Feb 25, 2024
@wxiaoguang wxiaoguang added this to the 1.22.0 milestone Feb 25, 2024
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 25, 2024
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 25, 2024
@wxiaoguang wxiaoguang force-pushed the refactor-str2html branch 4 times, most recently from 70d39fc to d69dbbe Compare February 25, 2024 15:18
@silverwind
Copy link
Member

silverwind commented Feb 26, 2024

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?

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Feb 27, 2024

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.

@6543
Copy link
Member

6543 commented Feb 29, 2024

should we encourage to use "Str2html" in templates in the future?

in my opinion no ... it's to error prone

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Feb 29, 2024
@wxiaoguang wxiaoguang enabled auto-merge (squash) February 29, 2024 13:29
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 1, 2024
@wxiaoguang wxiaoguang merged commit e71eb89 into go-gitea:main Mar 1, 2024
26 checks passed
@wxiaoguang wxiaoguang deleted the refactor-str2html branch March 1, 2024 07:13
zjjhot added a commit to zjjhot/gitea that referenced this pull request Mar 1, 2024
* 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)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants