@@ -194,10 +194,40 @@ func MigrateCORSConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version) err
194194// MigrateCSRFConfig updates csrf middleware configuration fields
195195func MigrateCSRFConfig (cmd * cobra.Command , cwd string , _ , _ * semver.Version ) error {
196196 replacer := strings .NewReplacer ("Expiration:" , "IdleTimeout:" )
197- re := regexp .MustCompile (`\s*SessionKey:\s*[^,]+,?\n` )
197+ reSession := regexp .MustCompile (`\s*SessionKey:\s*[^,]+,?\n` )
198+ reKeyLookup := regexp .MustCompile (`(\s*)KeyLookup:\s*([^,\n]+)(,?)(\n?)` )
198199 err := internal .ChangeFileContent (cwd , func (content string ) string {
199200 content = replacer .Replace (content )
200- return re .ReplaceAllString (content , "" )
201+ content = reSession .ReplaceAllString (content , "" )
202+
203+ content = reKeyLookup .ReplaceAllStringFunc (content , func (s string ) string {
204+ sub := reKeyLookup .FindStringSubmatch (s )
205+ indent := sub [1 ]
206+ val := strings .TrimSpace (sub [2 ])
207+ comma := sub [3 ]
208+ newline := sub [4 ]
209+
210+ if uq , err := strconv .Unquote (val ); err == nil {
211+ val = uq
212+ }
213+
214+ var extractor string
215+ switch {
216+ case strings .HasPrefix (val , "header:" ):
217+ extractor = fmt .Sprintf ("Extractor: csrf.FromHeader(%q)" , strings .TrimPrefix (val , "header:" ))
218+ case strings .HasPrefix (val , "form:" ):
219+ extractor = fmt .Sprintf ("Extractor: csrf.FromForm(%q)" , strings .TrimPrefix (val , "form:" ))
220+ case strings .HasPrefix (val , "query:" ):
221+ extractor = fmt .Sprintf ("Extractor: csrf.FromQuery(%q)" , strings .TrimPrefix (val , "query:" ))
222+ default :
223+ // Unsupported or insecure value (e.g. cookie) - remove
224+ return ""
225+ }
226+
227+ return fmt .Sprintf ("%s%s%s%s" , indent , extractor , comma , newline )
228+ })
229+
230+ return content
201231 })
202232 if err != nil {
203233 return fmt .Errorf ("failed to migrate CSRF configs: %w" , err )
0 commit comments