openfoodfacts / smooth-app

🤳🥫 The new Open Food Facts mobile application for Android and iOS, crafted with Flutter and Dart
https://world.openfoodfacts.org/open-food-facts-mobile-app?utm_source=off&utf_medium=web&utm_campaign=github-repo
Apache License 2.0
867 stars 285 forks source link

Crash on "edited products" screen #2793

Closed teolemon closed 2 years ago

teolemon commented 2 years ago

What

A user reports :

"there are a lot of bugs. When trying to see the ""edited products"" I get the following error :

FormatException: Unexpected character (at character 1)
<!-- start templates/web/common/site_layout.tt.html -->
^

To Reproduce

use smoothie and open edited products from your personal account in settings

Expected behavior

No API error

Part of

stephanegigandet commented 2 years ago

Moving to the app repo. What query is done to get the edited products? It is using the search API? (and not website URLs with .json at the end)

simonbengtsson commented 2 years ago

I got this as well, but found that it worked after I removed the username and password from the request parameters. It didn't matter if the user/password was correct or not. Knowing that it was an issue with username/password I tried logging out and then it started to work again.

monsieurtanuki commented 2 years ago

I could not reproduce it:

Therefore:

monsieurtanuki commented 2 years ago

@stephanegigandet The query for the products I edited is a POST:

simonbengtsson commented 2 years ago

Here is an example request that highlights the issue that I experienced. Note that if the user_id and password fields are removed the requests succeeds and returns expected json result. Also note that this is a minimal example of the post request that the app made that still has the issue. It was many other fields added to the app but these were not necessary to reproduce the issue.

var response = await http.post(
    Uri.parse('https://world.openfoodfacts.org/cgi/search.pl'), 
    body: {
      'json': '1',
      'search_terms': '',
      'fields': 'code',
      'user_id': 'simongbe',
      'password': '<password>', // doesn't matter if I use the wrong or correct password
    },
);

print(response.body); // prints html from website instead of expected result json (which will cause the Unexpected character error when the app tries to decode it with json.decode())

It could be something wrong with my simongbe account (which is a couple of years old). When I created a new one everything was working including when I use the app without being logged in. The issue keeps happening on the simongbe account despite that I logout and login.

Should also note that I'm not seeing a crash, only in app error message (screen recording below).

https://user-images.githubusercontent.com/3586691/184890622-4d63be2d-667f-49f7-885b-ff1b9346ff34.mov

monsieurtanuki commented 2 years ago

@simonbengtsson What you're displaying is interesting, but perhaps a bit off topic: the OP was more specifically about the "edited products" search.

That said, that may mean that some users are either deprecated or wrong.

The following query will land on the "Incorrect user name or password." off page: https://world.openfoodfacts.org/cgi/search.pl?json=1&search_terms=&fields=code&user_id=simongbe&password=password

If I don't put the password it's OK: https://world.openfoodfacts.org/cgi/search.pl?json=1&search_terms=&fields=code&user_id=simongbe

If I don't put the user id either it's OK too: https://world.openfoodfacts.org/cgi/search.pl?json=1&search_terms=&fields=code

That said, we should probably be more explicit and detect different use cases like "wrong password" or "server down".

stephanegigandet commented 2 years ago
  • we have a general problem with login vs. email - most of our code works with login but not when users connect with their email - we need somehow to retrieve the login from the email - is there a server side API for that?

@monsieurtanuki we don't have an API for that right now, but you could retrieve the user_id from the session cookie that is set when you login:

session=user_session&[session id]&user_id&stephane; domain=.openfoodfacts.org; path=/; expires=Sun, 15-Aug-2032 08:28:56 GMT

stephanegigandet commented 2 years ago

Note that search queries do not need user credentials.

If user credentials are supplied, then they will be verified, and if they are incorrect, then the server will return a HTML page.

What I could do is to change the status code to 403 for API requests that have an incorrect login / password. In that case, the dart package would know that the userid/password are incorrect, and it would not try to parse the response (because it's HTML and not the JSON structure it expects).

stephanegigandet commented 2 years ago
      'password': '<password>', // doesn't matter if I use the wrong or correct password

@simonbengtsson That's strange, the error you get is from an invalid password, but I don't see anything wrong with the simongbe account.

simonbengtsson commented 2 years ago

Despite triple checking the password when I created the simplified example above I still managed to include the wrong password... The repro steps in the simple example above is therefore invalid. I did get the error in the app after signing in for the first time in an iOS simulator. I cannot reproduce it now however. I will try some more and report back if I can reproduce it again.

monsieurtanuki commented 2 years ago

we don't have an API for that right now, but you could retrieve the user_id from the session cookie that is set when you login

@stephanegigandet I'm a bit puzzled: I don't think we actually manage sessions or cookies with our API calls, this is not the website. That said, I'm not an expert on cookies. Not my favorite food either, btw, but that's another story.

monsieurtanuki commented 2 years ago

@simonbengtsson Actually we do not send the password for each search query, only the user (if available).

stephanegigandet commented 2 years ago

@monsieurtanuki session cookies are returned by API calls too. When an API query (any type: login, search, product edit etc.) has an userid + password, the server will check those, and the response will include a session cookie that contains the username.

monsieurtanuki commented 2 years ago

@stephanegigandet Please provide a curl example, or someone else join on this issue, because cookies, sessions and curls are not my cup of tea.

monsieurtanuki commented 2 years ago

What I could do is to change the status code to 403 for API requests that have an incorrect login / password. In that case, the dart package would know that the userid/password are incorrect, and it would not try to parse the response (because it's HTML and not the JSON structure it expects).

@stephanegigandet That would be great!

teolemon commented 2 years ago
M123-dev commented 2 years ago

We don't add credentials to product queries anymore so this should be fixed, right @monsieurtanuki ?

Do you still have that problem @simonbengtsson ?

If it isn't fixed please reopen

simonbengtsson commented 2 years ago

I never found a way to reliably reproduce it and have not seen the error since 👍