|
| 1 | +//go:build ignore |
| 2 | +// +build ignore |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "regexp" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// This tool patches the generated oas_json_gen.go file to stop ambiguous oneOf |
| 15 | +// decoding from silently defaulting to variant 0. When one oneOf variant has |
| 16 | +// no fields unique to it, ogen makes it the sum type's default mapping and |
| 17 | +// falls back to it whenever no discriminating key matches, which can misroute |
| 18 | +// a payload (e.g. decode a SAML/enterprise connection config as a plain OAuth |
| 19 | +// one) once sibling variants share field names. This makes it fail closed. |
| 20 | + |
| 21 | +const targetFile = "oas_json_gen.go" |
| 22 | + |
| 23 | +// Matches the generated "if !found { s.Type = <value> }" fallback. <value> is |
| 24 | +// checked separately for the "X0X" sum-type-variant-0 shape (Go's regexp |
| 25 | +// package has no backreferences), e.g. |
| 26 | +// CreateConnectionReqOptions0CreateConnectionReqOptions. |
| 27 | +var fallbackPattern = regexp.MustCompile(`if !found \{\n\t\ts\.Type = (\w+)\n\t\}`) |
| 28 | + |
| 29 | +// remainingFallbackPattern is a looser tripwire: any `if !found` block that |
| 30 | +// still assigns s.Type before its closing brace. If it matches after the |
| 31 | +// patch pass, the generated shape has drifted past fallbackPattern (or a sum |
| 32 | +// type gained a non-variant-0 default) and the generate run must fail rather |
| 33 | +// than ship the silent-default behaviour. |
| 34 | +var remainingFallbackPattern = regexp.MustCompile(`if !found \{[^}]*s\.Type\s*=`) |
| 35 | + |
| 36 | +// isVariant0Assignment reports whether value has the "X0X" shape ogen uses |
| 37 | +// for a sum type's zero-variant constant. |
| 38 | +func isVariant0Assignment(value string) bool { |
| 39 | + n := len(value) |
| 40 | + if n < 3 || n%2 == 0 { |
| 41 | + return false |
| 42 | + } |
| 43 | + half := (n - 1) / 2 |
| 44 | + return value[half] == '0' && value[:half] == value[half+1:] |
| 45 | +} |
| 46 | + |
| 47 | +func main() { |
| 48 | + fmt.Printf("Patching %s for ambiguous oneOf handling...\n", targetFile) |
| 49 | + |
| 50 | + content, err := os.ReadFile(targetFile) |
| 51 | + if err != nil { |
| 52 | + fmt.Fprintf(os.Stderr, "Error reading file: %v\n", err) |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | + |
| 56 | + patched := 0 |
| 57 | + newContent := fallbackPattern.ReplaceAllFunc(content, func(match []byte) []byte { |
| 58 | + sub := fallbackPattern.FindSubmatch(match) |
| 59 | + if sub == nil || !isVariant0Assignment(string(sub[1])) { |
| 60 | + return match |
| 61 | + } |
| 62 | + patched++ |
| 63 | + return []byte(`if !found { |
| 64 | + return errors.New("unable to detect sum type variant") |
| 65 | + }`) |
| 66 | + }) |
| 67 | + |
| 68 | + // Validate before writing so a failed run leaves the file exactly as ogen |
| 69 | + // generated it, never partially patched. |
| 70 | + if locs := remainingFallbackPattern.FindAllIndex(newContent, -1); len(locs) > 0 { |
| 71 | + fmt.Fprintf(os.Stderr, "Error: %d oneOf fallback(s) would still assign s.Type after patching:\n", len(locs)) |
| 72 | + for _, loc := range locs { |
| 73 | + line := 1 + bytes.Count(newContent[:loc[0]], []byte("\n")) |
| 74 | + snippet := strings.Join(strings.Fields(string(newContent[loc[0]:loc[1]])), " ") |
| 75 | + fmt.Fprintf(os.Stderr, " %s:%d: %s\n", targetFile, line, snippet) |
| 76 | + } |
| 77 | + fmt.Fprintf(os.Stderr, "The generated shape has likely changed (ogen upgrade?) - update fix_oneof.go\n") |
| 78 | + fmt.Fprintf(os.Stderr, "%s left unmodified\n", targetFile) |
| 79 | + os.Exit(1) |
| 80 | + } |
| 81 | + |
| 82 | + if patched == 0 { |
| 83 | + fmt.Println("No ambiguous oneOf fallback found - already patched or no longer generated") |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if err := os.WriteFile(targetFile, newContent, 0644); err != nil { |
| 88 | + fmt.Fprintf(os.Stderr, "Error writing patched file: %v\n", err) |
| 89 | + os.Exit(1) |
| 90 | + } |
| 91 | + fmt.Printf("✅ Patched %d ambiguous oneOf fallback(s) to fail closed\n", patched) |
| 92 | +} |
0 commit comments