Check if username is valid and remove slashes from filename

Backport of #2236 to stable45