mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-31 10:29:20 +00:00 
			
		
		
		
	Protect against user injection for registration CLI page
This commit addresses a potential issue where we allowed unsanitised content to be passed through a go template without validation. We now try to unmarshall the incoming node key and fails to render the template if it is not a valid node key. Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
		
							
								
								
									
										16
									
								
								api.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								api.go
									
									
									
									
									
								
							| @@ -9,6 +9,7 @@ import ( | ||||
|  | ||||
| 	"github.com/gorilla/mux" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| 	"tailscale.com/types/key" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| @@ -93,7 +94,18 @@ func (h *Headscale) RegisterWebAPI( | ||||
| ) { | ||||
| 	vars := mux.Vars(req) | ||||
| 	nodeKeyStr, ok := vars["nkey"] | ||||
| 	if !ok || nodeKeyStr == "" { | ||||
|  | ||||
| 	// We need to make sure we dont open for XSS style injections, if the parameter that | ||||
| 	// is passed as a key is not parsable/validated as a NodePublic key, then fail to render | ||||
| 	// the template and log an error. | ||||
| 	var nodeKey key.NodePublic | ||||
| 	err := nodeKey.UnmarshalText( | ||||
| 		[]byte(NodePublicKeyEnsurePrefix(nodeKeyStr)), | ||||
| 	) | ||||
|  | ||||
| 	if !ok || nodeKeyStr == "" || err != nil { | ||||
| 		log.Warn().Err(err).Msg("Failed to parse incoming nodekey") | ||||
|  | ||||
| 		writer.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||||
| 		writer.WriteHeader(http.StatusBadRequest) | ||||
| 		_, err := writer.Write([]byte("Wrong params")) | ||||
| @@ -130,7 +142,7 @@ func (h *Headscale) RegisterWebAPI( | ||||
|  | ||||
| 	writer.Header().Set("Content-Type", "text/html; charset=utf-8") | ||||
| 	writer.WriteHeader(http.StatusOK) | ||||
| 	_, err := writer.Write(content.Bytes()) | ||||
| 	_, err = writer.Write(content.Bytes()) | ||||
| 	if err != nil { | ||||
| 		log.Error(). | ||||
| 			Caller(). | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Kristoffer Dalby
					Kristoffer Dalby