traefik / yaegi

Yaegi is Another Elegant Go Interpreter

Home Page:https://pkg.go.dev/github.com/traefik/yaegi

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

yaegi v0.14.3 stops to works with gorilla/sessions

wiltonsr opened this issue · comments

The following program sample.go triggers an unexpected result

// sessions.go
package main

import (
	"fmt"
	"net/http"

	"github.com/gorilla/sessions"
)

var (
	// key must be 16, 24 or 32 bytes long (AES-128, AES-192 or AES-256)
	key   = []byte("super-secret-key")
	store = sessions.NewCookieStore(key)
)

func secret(w http.ResponseWriter, r *http.Request) {
	session, _ := store.Get(r, "ldapauth")

	// Check if user is authenticated
	if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
		http.Error(w, "Forbidden", http.StatusForbidden)
		return
	}

	// Print secret message
	fmt.Fprintln(w, "The cake is a lie!")
}

func login(w http.ResponseWriter, r *http.Request) {
	session, _ := store.Get(r, "ldapauth")
	fmt.Println(session.IsNew)

	// Authentication goes here
	// ...

	// Set user as authenticated
	session.Values["authenticated"] = true
	session.Save(r, w)
}

func logout(w http.ResponseWriter, r *http.Request) {
	session, _ := store.Get(r, "ldapauth")

	// Revoke users authentication
	session.Values["authenticated"] = false
	session.Save(r, w)
}

func main() {
	http.HandleFunc("/secret", secret)
	http.HandleFunc("/login", login)
	http.HandleFunc("/logout", logout)

	http.ListenAndServe(":8080", nil)
}

Expected result

# The server starts to listens

Got

main.go:51:2: panic
run: reflect: Call using *interp.node as type func(http.ResponseWriter, *http.Request)
goroutine 1 [running]:
runtime/debug.Stack()
	/opt/hostedtoolcache/go/1.19.2/x64/src/runtime/debug/stack.go:24 +0x65
github.com/traefik/yaegi/interp.(*Interpreter).Execute.func1()
	/home/runner/work/yaegi/yaegi/interp/program.go:141 +0x94
panic({0xd85dc0, 0xc0004c53f0})
	/opt/hostedtoolcache/go/1.19.2/x64/src/runtime/panic.go:884 +0x212
github.com/traefik/yaegi/interp.runCfg.func1()
	/home/runner/work/yaegi/yaegi/interp/run.go:192 +0x148
panic({0xd85dc0, 0xc0004c53f0})
	/opt/hostedtoolcache/go/1.19.2/x64/src/runtime/panic.go:884 +0x212
reflect.Value.call({0xd99ba0?, 0xf14d90?, 0x41227f?}, {0xeab0c6, 0x4}, {0xc0008a7170, 0x2, 0xc0005932b0?})
	/opt/hostedtoolcache/go/1.19.2/x64/src/reflect/value.go:440 +0x1abf
reflect.Value.Call({0xd99ba0?, 0xf14d90?, 0x4571b2?}, {0xc0008a7170?, 0xea8ee0?, 0xc00053ee70?})
	/opt/hostedtoolcache/go/1.19.2/x64/src/reflect/value.go:368 +0xbc
github.com/traefik/yaegi/interp.callBin.func2({0xd99ba0?, 0xf14d90?, 0x405514?}, {0xc0008a7170?, 0xc00056d8c0?, 0xeb9417?})
	/home/runner/work/yaegi/yaegi/interp/run.go:1483 +0x28
github.com/traefik/yaegi/interp.callBin.func11(0xc00053ee70)
	/home/runner/work/yaegi/yaegi/interp/run.go:1658 +0x15f
github.com/traefik/yaegi/interp.runCfg(0xc00040f0e0, 0xc00053ee70, 0x2?, 0x2?)
	/home/runner/work/yaegi/yaegi/interp/run.go:200 +0x29d
github.com/traefik/yaegi/interp.(*Interpreter).run(0xc0000ef440, 0xc00040e6c0, 0xc0000d4630?)
	/home/runner/work/yaegi/yaegi/interp/run.go:119 +0x38e
github.com/traefik/yaegi/interp.(*Interpreter).Execute(0xc0000ef440, 0xc0008a5ef0)
	/home/runner/work/yaegi/yaegi/interp/program.go:167 +0x24b
github.com/traefik/yaegi/interp.(*Interpreter).eval(0xc0000ef440, {0xc0003faf00?, 0x49b?}, {0x7ffe24bb87d7?, 0xc0003fa500?}, 0x9b?)
	/home/runner/work/yaegi/yaegi/interp/interp.go:561 +0x5c
github.com/traefik/yaegi/interp.(*Interpreter).EvalPath(0xc0000ef440, {0x7ffe24bb87d7, 0x7})
	/home/runner/work/yaegi/yaegi/interp/interp.go:510 +0xab
main.runFile(0x7ffe24bb87d7?, {0x7ffe24bb87d7, 0x7}, 0x0)
	/home/runner/work/yaegi/yaegi/cmd/yaegi/run.go:153 +0xee
main.run({0xc0000361a0?, 0x1, 0x1})
	/home/runner/work/yaegi/yaegi/cmd/yaegi/run.go:116 +0xbec
main.main()
	/home/runner/work/yaegi/yaegi/cmd/yaegi/yaegi.go:133 +0xcf

Yaegi Version

0.14.3

Additional Notes

Works as expected in 0.14.2.

Hello, @mvertes

Any news about this issue?

This is working in v0.15.0

Hello @mvertes,

Thanks for the fixes.

gorilla/sessions is working now, but go-ldap stopped to works again.

There's a fix for that in issue #1275. But with v0.15.0 I'm getting the error:

<nil>
LDAP Request: (Universal, Constructed, Sequence and Sequence of) Len=47 "<nil>"
 MessageID: (Universal, Primitive, Integer) Len=1 "1"
 Bind Request: (Application, Constructed, 0x00) Len=42 "<nil>"
  Version: (Universal, Primitive, Integer) Len=1 "3"
  User Name: (Universal, Primitive, Octet String) Len=27 "uid=tesla,dc=example,dc=com"
  Password: (Context, Primitive, 0x00) Len=8 "password"
2023/02/28 11:44:46 flags&startTLS = 0
2023/02/28 11:44:46 1: returning
2023/02/28 11:44:46 1: waiting for response
2023/02/28 11:44:46 Sending message 1
2023/02/28 11:44:47 Receiving message 1
2023/02/28 11:44:47 1: got response %!p(interp.valueInterface={0xc001372f00 {0xc000591580 0xc0005ba3c8 406}})
LDAP Response: (Universal, Constructed, Sequence and Sequence of) Len=12 "<nil>"
 Message ID: (Universal, Primitive, Integer) Len=1 "1"
 Bind Response: (Application, Constructed, 0x01) Len=7 "<nil>"
  Result Code (Success): (Universal, Primitive, Enumerated) Len=1 "0"
  Matched DN (): (Universal, Primitive, Octet String) Len=0 ""
  Success: (Universal, Primitive, Octet String) Len=0 ""
2023/02/28 11:44:47 Finished message 1
/home/wilton/go/src/github.com/wiltonsr/yaegi-test/vendor/github.com/go-ldap/ldap/v3/conn.go:264:2: panic
run: reflect: Call using bool as type string
goroutine 1 [running]:
runtime/debug.Stack()
	/opt/hostedtoolcache/go/1.20.0/x64/src/runtime/debug/stack.go:24 +0x65
github.com/traefik/yaegi/interp.(*Interpreter).Execute.func1()
	/home/runner/work/yaegi/yaegi/interp/program.go:146 +0x94
panic({0xda72c0, 0xc000e91cb0})
	/opt/hostedtoolcache/go/1.20.0/x64/src/runtime/panic.go:884 +0x213
github.com/traefik/yaegi/interp.runCfg.func1()
	/home/runner/work/yaegi/yaegi/interp/run.go:205 +0x1a5
panic({0xda72c0, 0xc000e91cb0})
	/opt/hostedtoolcache/go/1.20.0/x64/src/runtime/panic.go:884 +0x213
reflect.Value.call({0xdbb560?, 0xc0008c94d0?, 0xdbb560?}, {0xed11c4, 0x4}, {0xc0008c9500, 0x2, 0x30?})
	/opt/hostedtoolcache/go/1.20.0/x64/src/reflect/value.go:442 +0x1a9f
reflect.Value.Call({0xdbb560?, 0xc0008c94d0?, 0x456512?}, {0xc0008c9500?, 0xecefa0?, 0x1?})
	/opt/hostedtoolcache/go/1.20.0/x64/src/reflect/value.go:370 +0xbc
github.com/traefik/yaegi/interp.call.func9.2({0xc0008c9500?, 0xc0008c94d0?, 0x5?})
	/home/runner/work/yaegi/yaegi/interp/run.go:1301 +0x3c
github.com/traefik/yaegi/interp.call.func9(0xc001456790)
	/home/runner/work/yaegi/yaegi/interp/run.go:1326 +0x1265
github.com/traefik/yaegi/interp.runCfg(0xc000f09e00, 0xc001456790, 0x0?, 0xc001419a80?)
	/home/runner/work/yaegi/yaegi/interp/run.go:213 +0x29d
github.com/traefik/yaegi/interp.genFunctionWrapper.func1.1({0x15eff40, 0x0, 0x0?})
	/home/runner/work/yaegi/yaegi/interp/run.go:1015 +0x825
reflect.Value.call({0xd9de80?, 0xc0008bd2f0?, 0x13?}, {0xed11c4, 0x4}, {0xc0013ad200, 0x0, 0xdd12a0?})
	/opt/hostedtoolcache/go/1.20.0/x64/src/reflect/value.go:586 +0xb07
reflect.Value.Call({0xd9de80?, 0xc0008bd2f0?, 0xc0008bd3e0?}, {0xc0013ad200?, 0xc0011dad10?, 0xc00053f8c8?})
	/opt/hostedtoolcache/go/1.20.0/x64/src/reflect/value.go:370 +0xbc
github.com/traefik/yaegi/interp.runCfg.func1()
	/home/runner/work/yaegi/yaegi/interp/run.go:190 +0xd7
github.com/traefik/yaegi/interp.runCfg(0xc00037d680, 0xc0011dad10, 0x4?, 0x0?)
	/home/runner/work/yaegi/yaegi/interp/run.go:215 +0x2f1
github.com/traefik/yaegi/interp.(*Interpreter).run(0xc0000f9680, 0xc00037c8c0, 0xc0000dc580?)
	/home/runner/work/yaegi/yaegi/interp/run.go:119 +0x40a
github.com/traefik/yaegi/interp.(*Interpreter).Execute(0xc0000f9680, 0xc0008b9b30)
	/home/runner/work/yaegi/yaegi/interp/program.go:172 +0x246
github.com/traefik/yaegi/interp.(*Interpreter).eval(0xc0000f9680, {0xc0000ee5a0?, 0x10e?}, {0x7fff6d1e60f9?, 0x200?}, 0x60?)
	/home/runner/work/yaegi/yaegi/interp/interp.go:564 +0x5c
github.com/traefik/yaegi/interp.(*Interpreter).EvalPath(0xc0000f9680, {0x7fff6d1e60f9, 0x7})
	/home/runner/work/yaegi/yaegi/interp/interp.go:513 +0xab
main.runFile(0x7fff6d1e60f9?, {0x7fff6d1e60f9, 0x7}, 0x0)
	/home/runner/work/yaegi/yaegi/cmd/yaegi/run.go:153 +0xee
main.run({0xc000036050?, 0x1, 0x1})
	/home/runner/work/yaegi/yaegi/cmd/yaegi/run.go:116 +0xb9a
main.main()
	/home/runner/work/yaegi/yaegi/cmd/yaegi/yaegi.go:144 +0x2cb

I used the same example from another issue.

would you rather I open another issue?

Thanks. I confirm that #1275 is broken again with v0.15.0. But I just tested with #1516 and it is ok to me. It's very likely that we merge #1516 soon and make a new release.

I'm reopening #1275 in the mean time.