mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-10-31 06:21:11 +00:00 
			
		
		
		
	[GITEA] Use restricted sanitizer for repository description
- Currently the repository description uses the same sanitizer as a normal markdown document. This means that element such as heading and images are allowed and can be abused. - Create a minimal restricted sanitizer for the repository description, which only allows what the postprocessor currently allows, which are links and emojis. - Added unit testing. - Resolves https://codeberg.org/forgejo/forgejo/issues/1202 - Resolves https://codeberg.org/Codeberg/Community/issues/1122 (cherry picked from commita8afa4cd18) (cherry picked from commit0238587c51) (cherry picked from commita8c7bbf728) (cherry picked from commit80e05a8245) (cherry picked from commitf5af5050b3) (cherry picked from commit608f981e55)
This commit is contained in:
		
					parent
					
						
							
								0d6f131e21
							
						
					
				
			
			
				commit
				
					
						6591867502
					
				
			
		
					 3 changed files with 56 additions and 5 deletions
				
			
		|  | @ -18,9 +18,10 @@ import ( | |||
| // Sanitizer is a protection wrapper of *bluemonday.Policy which does not allow | ||||
| // any modification to the underlying policies once it's been created. | ||||
| type Sanitizer struct { | ||||
| 	defaultPolicy    *bluemonday.Policy | ||||
| 	rendererPolicies map[string]*bluemonday.Policy | ||||
| 	init             sync.Once | ||||
| 	defaultPolicy     *bluemonday.Policy | ||||
| 	descriptionPolicy *bluemonday.Policy | ||||
| 	rendererPolicies  map[string]*bluemonday.Policy | ||||
| 	init              sync.Once | ||||
| } | ||||
| 
 | ||||
| var ( | ||||
|  | @ -41,6 +42,7 @@ func NewSanitizer() { | |||
| func InitializeSanitizer() { | ||||
| 	sanitizer.rendererPolicies = map[string]*bluemonday.Policy{} | ||||
| 	sanitizer.defaultPolicy = createDefaultPolicy() | ||||
| 	sanitizer.descriptionPolicy = createRepoDescriptionPolicy() | ||||
| 
 | ||||
| 	for name, renderer := range renderers { | ||||
| 		sanitizerRules := renderer.SanitizerRules() | ||||
|  | @ -161,6 +163,27 @@ func createDefaultPolicy() *bluemonday.Policy { | |||
| 	return policy | ||||
| } | ||||
| 
 | ||||
| // createRepoDescriptionPolicy returns a minimal more strict policy that is used for | ||||
| // repository descriptions. | ||||
| func createRepoDescriptionPolicy() *bluemonday.Policy { | ||||
| 	policy := bluemonday.NewPolicy() | ||||
| 
 | ||||
| 	// Allow italics and bold. | ||||
| 	policy.AllowElements("i", "b", "em", "strong") | ||||
| 
 | ||||
| 	// Allow code. | ||||
| 	policy.AllowElements("code") | ||||
| 
 | ||||
| 	// Allow links | ||||
| 	policy.AllowAttrs("href", "target", "rel").OnElements("a") | ||||
| 
 | ||||
| 	// Allow classes for emojis | ||||
| 	policy.AllowAttrs("class").Matching(regexp.MustCompile(`^emoji$`)).OnElements("img", "span") | ||||
| 	policy.AllowAttrs("aria-label").OnElements("span") | ||||
| 
 | ||||
| 	return policy | ||||
| } | ||||
| 
 | ||||
| func addSanitizerRules(policy *bluemonday.Policy, rules []setting.MarkupSanitizerRule) { | ||||
| 	for _, rule := range rules { | ||||
| 		if rule.AllowDataURIImages { | ||||
|  | @ -176,6 +199,12 @@ func addSanitizerRules(policy *bluemonday.Policy, rules []setting.MarkupSanitize | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| // SanitizeDescription sanitizes the HTML generated for a repository description. | ||||
| func SanitizeDescription(s string) string { | ||||
| 	NewSanitizer() | ||||
| 	return sanitizer.descriptionPolicy.Sanitize(s) | ||||
| } | ||||
| 
 | ||||
| // Sanitize takes a string that contains a HTML fragment or document and applies policy whitelist. | ||||
| func Sanitize(s string) string { | ||||
| 	NewSanitizer() | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue