lua-nginx-module icon indicating copy to clipboard operation
lua-nginx-module copied to clipboard

Add a flag when decoded args are truncated

Open hamishforbes opened this issue 10 years ago • 23 comments

Currently there's no way to tell if ngx.decode_args, ngx.req.get_uri_args or ngx.req.get_post_args silently discard arguments without enabling debug logging.

This change adds a second boolean return value to indicate if arguments have been truncated.

hamishforbes avatar Aug 05 '15 15:08 hamishforbes

@hamishforbes An additional return value can break backward compatibility, for example,

foo(ngx.req.get_uri_args(), "some other things")

will not work as expected because the second argument now becomes the 3rd, leading to (kindy) hard-to-debug issues.

We need to figure out a better way without breaking existing user code.

agentzh avatar Aug 05 '15 17:08 agentzh

Ah, that is a good point, didn't think of that!

We could add another argument that toggles returning this flag, e.g. local args, truncated = ngx.req.get_uri_args(100, true) but thats not particularly elegant either... what do you think?

The main issue is the args get truncated silently which can lead to odd issues when passing on request to other systems. So maybe just raising the log level of the existing message to error or warn would be sufficient?

I think it would be preferable to allow the user to handle it themselves rather than log something but at least it would be clear what has happened.

hamishforbes avatar Aug 06 '15 09:08 hamishforbes

@hamishforbes You beat me :)

Another flag argument looks artificial to me as well. I would have a hard time documenting this argument if we implemented it.

Raising default error log level for the logging can open a door for malicious clients to flush your nginx error logs which can be very expensive because error logging is never buffered (for obvious reasons). Maybe adding a new configuration directive to allow the user to change the level? Still a bit cumbersome but a bit better than messing up the Lua API :) What do you think? (Sorry)

agentzh avatar Aug 06 '15 12:08 agentzh

Do you mean adding an option similar to lua_socket_log_errors? That sounds reasonable.

The only other approach I can think of would be to add something like ngx.get_last_args_parse_error(). But thats not very nice either and smacks a bit of PHP...

hamishforbes avatar Aug 06 '15 14:08 hamishforbes

@hamishforbes Oh, seems like there's another way. How about adding an empty metatable to the resulting table in case of truncation? This won't break existing code I suppose and you have a way to handle this case in Lua yourself.

agentzh avatar Aug 06 '15 14:08 agentzh

I'm not entirely sure I follow, do you mean setting the metatable to {} rather than nil?

hamishforbes avatar Aug 06 '15 14:08 hamishforbes

@hamishforbes Yes.

agentzh avatar Aug 06 '15 14:08 agentzh

That sounds like a better solution.

I'm pretty new to the Lua C API but am I right in thinking this is as simple as

lua_newtable(L);
lua_setmetatable(L, top);

hamishforbes avatar Aug 06 '15 16:08 hamishforbes

@hamishforbes Instead of creating a new table upon every call, we could reuse and share a single empty table. It's supposed to be read-only anyway.

agentzh avatar Aug 07 '15 10:08 agentzh

Whats the best way to go about doing that? Or is there somewhere else in the codebase you've already used that technique? Is it as simple as using lua_setglobal and lua_get_global?

hamishforbes avatar Aug 07 '15 10:08 hamishforbes

@hamishforbes Maybe you can find this helpful: http://www.lua.org/manual/5.1/manual.html#luaL_newmetatable

agentzh avatar Aug 07 '15 11:08 agentzh

Ah missed that one! Sounds like repeatedly calling luaL_newmetatable doesn't create a new table each time, so that should be fine?

Do you have any preference for naming of metatables like this, there doesn't seem to be any others in the current codebase.

I'll update and push tests/docs if you're happy with this approach.

hamishforbes avatar Aug 07 '15 11:08 hamishforbes

@hamishforbes Yes. Please go ahead :) Though I'm still not 100% happy with this approach ;)

agentzh avatar Aug 07 '15 11:08 agentzh

Ok cool, thanks for the pointers!

hamishforbes avatar Aug 07 '15 12:08 hamishforbes

This pull request is now in conflict :(

mergify[bot] avatar Jun 26 '20 00:06 mergify[bot]