Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

References to model names are allowed to have URI illegal characters #651

Closed
flayman opened this issue Jun 13, 2019 · 1 comment
Closed

Comments

@flayman
Copy link

flayman commented Jun 13, 2019

In the ref() function in swagger.py, model.name is formatted into an internal $ref without first ensuring URI compliance. Here's how the function looks right now:

def ref(model):
    '''Return a reference to model in definitions'''
    name = model.name if isinstance(model, ModelBase) else model
    return {'$ref': '#/definitions/{0}'.format(name)}

Nor is the model name sanitized when the instance is constructed. The name variable falls back on the string representation of model if model.name evaluates to False, which is less likely to produce non-comforming strings, but it's still possible. This violates the JSON Reference spec and can create problems in swagger-ui where references do not resolve properly. It's easy to break some things in the front end just by providing a name that has slashes. It's easily fixed though. Here's my suggestion for a quick fix:

try:
   from urllib.parse import quote
except ImportError:
   from urllib import quote

def ref(model):
    '''Return a reference to model in definitions'''
    name = model.name if isinstance(model, ModelBase) else model
    return {'$ref': '#/definitions/{0}'.format(quote(name, safe=''))}
flayman pushed a commit to flayman/flask-restplus that referenced this issue Jun 13, 2019
@j5awry
Copy link
Collaborator

j5awry commented Oct 27, 2019

merged. Thanks!

@j5awry j5awry closed this as completed Oct 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants