sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inner bug: TypeError: __init__() got an unexpected keyword argument 'escape_forward_slashes'

ArdenL opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

i'm not sure if this is a bug, a internal exception occurred when i try to access the inspector page(http://localhost:6457/),

pkgs version:
sanic 23.3.0
sanic-ext 23.3.0
sanic-routing 22.8.0

log:

[ERROR] Exception occurred while handling uri: 'http://localhost:6457/'
Traceback (most recent call last):
  File "handle_request", line 97, in handle_request
    from sanic_ext.extensions.base import Extension  # type: ignore
  File "D:\apps\Anaconda\envs\sanic\lib\site-packages\sanic\worker\inspector.py", line 82, in _info
    return await self._respond(request, self._state_to_json())
  File "D:\apps\Anaconda\envs\sanic\lib\site-packages\sanic\worker\inspector.py", line 86, in _respond
    return json(
  File "D:\apps\Anaconda\envs\sanic\lib\site-packages\sanic\response\convenience.py", line 50, in json
    return JSONResponse(
  File "D:\apps\Anaconda\envs\sanic\lib\site-packages\sanic\response\types.py", line 361, in __init__
    self._encode_body(self._use_dumps(body, **self._use_dumps_kwargs)),
  File "D:\apps\Anaconda\envs\sanic\lib\json\__init__.py", line 234, in dumps
    return cls(
TypeError: __init__() got an unexpected keyword argument 'escape_forward_slashes'

Code snippet

No response

Expected Behavior

No response

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

windows

Sanic Version

Sanic 23.3.0; Routing 22.8.0

Additional context

No response

This appears to be a bug in sanic/worker/inspector.py:86 which uses escape_forward_slashes=False argument meant for ujson.dumps() but the Python standard library json.dumps doesn't support that argument. As a workaround, pip install ujson could work, as Sanic should then use that library rather than Python's.

As a more proper fix, I would like to see Sanic always inject that argument to ujson.dumps, and not doing it specifically in inspector code. The built-in json.dumps has no such escaping (equivalent to =False with ujson).

Escaping forward slashes is a retarded feature meant for injecting JSON into HTML script element such that it reads as Javascript and doesn't accidentally contain </script> (in some string literal) and I have no idea why ujson enables it by default but Sanic would be wise to disable it globally, and instead rely on html5tagger's capability to escape script inline content in HTML correctly if that was ever needed.

Thanks for you reply, just like you said inject the argument to json.dumps instead of default action is a good idea. Anyway, i use ujson to get what i want. Thank you again.