Skip to content

Microtask 1 #1

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

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open

Microtask 1 #1

wants to merge 33 commits into from

Conversation

yashasingh
Copy link
Owner

@tgr Please review

Copy link

@tgr tgr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR doesn't contain most of the changes. You can always make a PR from the master into a new branch (instead of the other way around) if you don't want to rewrite master's history.

@yashasingh
Copy link
Owner Author

@tgr Sir, I have updated the PR to contain all the changes.

Comment = {{x.comment}} <br>
Size = {{x.size}} <br>

<a href="https://en.wikipedia.org/w/index.php?title=User:{{x.user}}&diff=prev&oldid={{x.revid}}">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, that's not valid HTML (which is why Github shows it in red); special characters like & or " need to be encoded in attributes. So it should be {{x.user}}&amp;diff. In practice, it works anyway and no one really cares about, so feel free to ignore :) just mentioning for sake of completeness.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rectified .

{% if userid %}
Username = {{username}} <br>
Userid = {{userid}} <br>
<a href="https://phabricator.wikimedia.org/p/{{username}}/"> Profile link </a>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Phabricator username is not typically the same as the wiki username.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. I added that link to augment to the information. Since I just got to know that phabricator and wiki usernames are not same, I'd rather remove this link. Thank you .

App/views.py Outdated
if(request.method=='POST'):
usr = request.POST['username'] #Here, we get the useranme fetched from html page
URL = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=usercontribs&ucuser="
URL = URL + usr
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What it the username contains a &?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handled the situation. 👍

App/views.py Outdated
URL = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=usercontribs&ucuser="
URL = URL + usr
r = requests.get(URL) #Recieved data in json-format
lst = json.loads(r.text)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The request might fail (e.g. the API is down and responds with HTTP 503), in which case this won't work.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Will handle this, when response code is other than 200.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

App/views.py Outdated
URL = URL + usr
r = requests.get(URL) #Recieved data in json-format
lst = json.loads(r.text)
sub_lst = lst['query']['usercontribs'] #Contribution data extracted
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API might respond with an error, in which case this key might not exist. (You can test with a username containing <.)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved.

App/views.py Outdated
fetched_user = sub_lst[0]['user']
fetched_user_id = sub_lst[0]['userid']
except:
fetched_user = fetched_user_id = 'No user found'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or possibly the user was found but has no edits.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay , this was a loophole, will handle it 👍

@yashasingh
Copy link
Owner Author

@tgr I have made the required changes . Please review.

App/views.py Outdated
lst = json.loads(r.text)
try:
if(lst['error']):
return(render(request,'App/index.html',{"error":'INVALID INPUT'}))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be other reasons for getting an API. You probably want to show the user the error message returned by the API instead.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

App/views.py Outdated
def main(request):
if(request.method=='POST'):
usr = request.POST['username'] #Here, we get the username fetched from html page
usr = usr.replace("&","%26")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But now the username will look weird when you print it out :)

Best practice is to sanitize close to the input, escape close to the output. (Sanitize = reject invalid input. Here the API takes care of that so you don't have to. Escape = convert the variable to a safe format. "safe" depends on context (you escape differently for text in a HTML document, for an URL part, for a Javascript variable...) so it has to be done when you already know how you'll use it; ideally in the template. Most templating languages (including Django templates) support that via some special notation.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to make the variable safe, by escaping &. Is It ok?

App/views.py Outdated
@@ -6,7 +6,7 @@
def main(request):
if(request.method=='POST'):
usr = request.POST['username'] #Here, we get the username fetched from html page
usr = usr.replace("&","%26")
usr = usr.replace("&","")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will just break the URL.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir so should I consider input containing "&" as invalid , not deal with trying correct the handle ? Please tell . Currently , if I enter a username ey bd&808 , it converts to bd808 and displays the result.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bd&808 is a different (valid) username though so that would prevent that person from using your tool. The percent-encoding approach wasn't wrong, you just did it at the wrong place (also, no point in limiting it to just &). See my earlier comment - you should do escaping in the template, and use whatever tools the templating language provides.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir, in django templating, escaping is done by default on. Now, I have used 'safe' filter in my html file , so the username will not be auto-escaped. Am I going right now ?

@yashasingh
Copy link
Owner Author

@tgr Please review this PR.

@@ -19,7 +19,7 @@
{{error}}
{% endif %}
{% if username %}
Username = {{username}} <br>
Username = {{username | safe}} <br>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This turns off escaping. There is no reason to do that and it will result in some usernames looking wrong (if they contain a HTML entity). It could also be a security hole although here it probably won't be since MediaWiki disallows <> in usernames.

Default autoescaping works well when you just want to display text. Where it doesn't work is when you want use a variable in an URL or a Javascript variable or any other context where the escaping/encoding rules are different from that of HTML.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I understood not this thing in the first instance. Marking it safe is not the solution. So I have handled this now, using urlencoding. Now, the code works for all possible usernames else displays the error msg.

@yashasingh yashasingh changed the title files to be reviewed Microtask 1 Oct 9, 2017
@yashasingh
Copy link
Owner Author

@tgr Sir, Please review, sorry for delay.

Copy link

@tgr tgr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks quite nice! The edit links use the wrong title parameter, but MediaWiki ignores the title anyway when oldid is set, so it doesn't matter much.

App/views.py Outdated
return render(request, "App/index.html", {"error": "Please enter a username"})

# if username is not blank
params = {'ucuser': username}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more readable to put the constant parameters here as well.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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

Successfully merging this pull request may close these issues.

2 participants