Skip to content

Commit

Permalink
bugfix: prevent infinite loops in parseStringLiteralContents
Browse files Browse the repository at this point in the history
  • Loading branch information
elkrammer committed Sep 27, 2024
1 parent e7399af commit d0f70fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
12 changes: 12 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,7 @@ func (p *Parser) parseSwitchStatement() *ast.SwitchStatement {
}
}
} else {
p.reportError(fmt.Sprintf("parseSwitchStatement: Invalid case statement starting with token: %s", p.curToken.Literal))
return nil // Error occurred in parsing case statement
}

Expand Down Expand Up @@ -1701,6 +1702,7 @@ func (p *Parser) parseStringLiteralContents(s *ast.StringLiteral) ast.Expression
currentPart := ""
inCommand := false
value := s.Value
startPosition := 0

for len(value) > 0 {
if strings.HasPrefix(value, "[") && !inCommand {
Expand Down Expand Up @@ -1759,6 +1761,7 @@ func (p *Parser) parseStringLiteralContents(s *ast.StringLiteral) ast.Expression
currentPart += string(value[0])
value = value[1:]
} else {
// Handle regular text
end := strings.IndexAny(value, "[${$")
if end == -1 {
currentPart += value
Expand All @@ -1767,7 +1770,16 @@ func (p *Parser) parseStringLiteralContents(s *ast.StringLiteral) ast.Expression
currentPart += value[:end]
value = value[end:]
}

// Break condition to prevent infinite loop
if startPosition == len(value) {
if config.DebugMode {
fmt.Printf("DEBUG: parseStringLiteralContents - Breaking loop due to no progress\n")
}
break
}
}
startPosition = len(value)
}

if currentPart != "" {
Expand Down
30 changes: 15 additions & 15 deletions test-data/complex-http.irule
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ when HTTP_REQUEST {
}
}

# # Perform routing based on the URI path using switch
# switch -glob $uri_path {
# "/api*" {
# log local0. "Routing /api to internal IP 10.0.0.1"
# # Redirect to internal IP address
# HTTP::redirect "http://10.0.0.1[HTTP::uri]"
# }
# "/healthcheck" {
# log local0. "Routing /healthcheck to external domain health.example.com"
# HTTP::redirect "https://health.example.com"
# }
# default {
# log local0. "Default request, no action taken"
# }
# }
# Perform routing based on the URI path using switch
switch -glob $uri_path {
"/api*" {
log local0. "Routing /api to internal IP 10.0.0.1"
# Redirect to internal IP address
HTTP::redirect "http://10.0.0.1[HTTP::uri]"
}
"/healthcheck" {
log local0. "Routing /healthcheck to external domain health.example.com"
HTTP::redirect "https://health.example.com"
}
default {
log local0. "Default request, no action taken"
}
}
}

0 comments on commit d0f70fc

Please sign in to comment.