FormatURI formats a string literal or placeholder containing a URI. If the node is a string literal, we redact the contents to avoid leaking secrets.
(uri Expr)
| 577 | // FormatURI formats a string literal or placeholder containing a URI. If the |
| 578 | // node is a string literal, we redact the contents to avoid leaking secrets. |
| 579 | func (ctx *FmtCtx) FormatURI(uri Expr) { |
| 580 | switch n := uri.(type) { |
| 581 | case *StrVal, *DString: |
| 582 | if ctx.HasAnyFlags( |
| 583 | FmtShowPasswords | FmtShowFullURIs | FmtHideConstants | FmtConstantsAsUnderscores, |
| 584 | ) { |
| 585 | ctx.FormatNode(n) |
| 586 | return |
| 587 | } |
| 588 | var raw, elided string |
| 589 | if str, ok := n.(*StrVal); ok { |
| 590 | raw = str.RawString() |
| 591 | } else { |
| 592 | raw = string(MustBeDString(uri)) |
| 593 | } |
| 594 | if raw == "" || raw == "_" { |
| 595 | // Some commands treat empty URIs as special. And if we've re-parsed a URI |
| 596 | // formatted with FmtHideConstants, we should not try to interpret it as a |
| 597 | // URL but should leave it as-is. |
| 598 | ctx.FormatNode(n) |
| 599 | return |
| 600 | } |
| 601 | // TODO(michae2): Call SanitizeExternalStorageURI for fine-grained |
| 602 | // sanitization. |
| 603 | elided = strings.Trim(PasswordSubstitution, "'") |
| 604 | ctx.FormatNode(NewStrVal(elided)) |
| 605 | case *Placeholder: |
| 606 | ctx.FormatNode(n) |
| 607 | default: |
| 608 | // We don't want to fail to sanitize other literals, so disallow other types |
| 609 | // of expressions (which should already be disallowed by the parser anyway). |
| 610 | panic(errors.AssertionFailedf("expected *StrVal, *DString, or *Placeholder, found %T", n)) |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | // FormatNode recurses into a node for pretty-printing. |
| 615 | // Flag-driven special cases can hook into this. |
no test coverage detected