Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pongo2_issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2471,3 +2471,26 @@ func TestBugAndOrPrecedence(t *testing.T) {
})
}
}

type issue204Item struct{ index string }

func (i issue204Item) String() string { return "Item-" + i.index }

// Issue #204: accessing an unexported struct field from a template must not panic.
func TestIssue204UnexportedStructField(t *testing.T) {
items := []interface{}{issue204Item{"0"}, issue204Item{"1"}, issue204Item{"2"}}

for _, tplStr := range []string{
`{% for item in items %}{% if item.index %}x{% endif %}{% endfor %}`,
`{% for item in items %}{{ item["index"] }}{% endfor %}`,
`{% for item in items %}{{ item.index }}{% endfor %}`,
} {
tpl, err := pongo2.FromString(tplStr)
if err != nil {
t.Fatalf("parse error for %q: %v", tplStr, err)
}
if _, err := tpl.Execute(pongo2.Context{"items": items}); err != nil {
t.Fatalf("execute error for %q: %v", tplStr, err)
}
}
}
14 changes: 12 additions & 2 deletions variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,12 @@
func (vr *variableResolver) resolveIdentifier(current reflect.Value, part *variablePart) (reflect.Value, bool, error) {
switch current.Kind() {
case reflect.Struct:
return current.FieldByName(part.s), false, nil
field := current.FieldByName(part.s)
if field.IsValid() && !field.CanInterface() {
// Unexported fields are not accessible from templates (Django renders them empty).
return reflect.Value{}, true, nil
}
return field, false, nil
case reflect.Map:
return current.MapIndex(reflect.ValueOf(part.s)), false, nil
default:
Expand All @@ -391,7 +396,7 @@
}

// resolveSubscript resolves a subscript access (e.g., foo[bar]).
func (vr *variableResolver) resolveSubscript(

Check failure on line 399 in variable.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=flosch_pongo2&issues=AZ4ehh7LpETyNqbob63H&open=AZ4ehh7LpETyNqbob63H&pullRequest=377
ctx *ExecutionContext,
current reflect.Value,
part *variablePart,
Expand All @@ -417,7 +422,12 @@
}
return reflect.Value{}, true, nil
case reflect.Struct:
return current.FieldByName(sv.String()), false, nil
field := current.FieldByName(sv.String())
if field.IsValid() && !field.CanInterface() {
// Unexported fields are not accessible from templates (Django renders them empty).
return reflect.Value{}, true, nil
}
return field, false, nil
case reflect.Map:
if sv.IsNil() {
return reflect.Value{}, true, nil
Expand Down