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

Angular should handle errors from backend better #24396

Closed
mraible opened this issue Nov 29, 2023 · 10 comments
Closed

Angular should handle errors from backend better #24396

mraible opened this issue Nov 29, 2023 · 10 comments
Assignees
Labels
$$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: angular theme: security $100 https://www.jhipster.tech/bug-bounties/
Milestone

Comments

@mraible
Copy link
Contributor

mraible commented Nov 29, 2023

Overview of the issue

Today, I noticed that if you return a 401 from a backend method, the Angular UI will redirect you to / and log you out. To work around this, I tried returning a 403 instead. This is better because it doesn't log you out but shows a blank screen.

Screenshot 2023-11-28 at 8 38 48 PM

Code that throws the exception:

throw new ResponseStatusException(HttpStatus.FORBIDDEN, "error.http.403");
Motivation for or Use Case

The error is in the console, and it should be in the UI too.

Reproduce the error

Create an app with Angular, add the exception to a method, and hit that method with a call from the UI.

blog@0.0.1-SNAPSHOT /Users/mraible/Downloads/blog
└── generator-jhipster@8.0.0
JHipster configuration, a .yo-rc.json file generated in the root folder
.yo-rc.json file
{
  "generator-jhipster": {
    "applicationType": "monolith",
    "authenticationType": "jwt",
    "baseName": "blog",
    "buildTool": "maven",
    "cacheProvider": "ehcache",
    "clientFramework": "angular",
    "clientTestFrameworks": [
      "cypress"
    ],
    "clientTheme": "none",
    "creationTimestamp": 1701088333195,
    "cypressAudit": true,
    "cypressCoverage": true,
    "databaseType": "sql",
    "devDatabaseType": "h2Disk",
    "devServerPort": 4200,
    "enableGradleEnterprise": null,
    "enableHibernateCache": true,
    "enableSwaggerCodegen": false,
    "enableTranslation": true,
    "entities": [
      "Blog",
      "Post",
      "Tag"
    ],
    "gradleEnterpriseHost": null,
    "jhipsterVersion": "8.0.0",
    "languages": [
      "en",
      "es"
    ],
    "lastLiquibaseTimestamp": 1701088513000,
    "messageBroker": false,
    "microfrontend": null,
    "microfrontends": [],
    "nativeLanguage": "en",
    "packageName": "org.jhipster.blog",
    "prodDatabaseType": "postgresql",
    "reactive": false,
    "searchEngine": false,
    "serverPort": null,
    "serverSideOptions": [],
    "serviceDiscoveryType": false,
    "testFrameworks": [
      "cypress"
    ],
    "websocket": false,
    "withAdminUi": true
  }
}
Environment and Tools

openjdk version "21" 2023-09-19
OpenJDK Runtime Environment (build 21+35-2513)
OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)

git version 2.39.3 (Apple Git-145)

node: v18.18.2
npm: 9.8.1

Docker version 24.0.6, build ed223bc

JDL for the Entity configuration(s) entityName.json files generated in the .jhipster directory
JDL entity definitions
@ChangelogDate("20231127123313")
entity Blog {
  name String required minlength(3)
  handle String required minlength(2)
}
@ChangelogDate("20231127123413")
entity Post {
  title String required
  content TextBlob required
  date Instant required
}
@ChangelogDate("20231127123513")
entity Tag {
  name String required minlength(2)
}
relationship ManyToOne {
  Blog{user(login)} to User with builtInEntity
  Post{blog(name)} to Blog
}
relationship ManyToMany {
  Post{tag(name)} to Tag{post}
}

search Blog, Post, Tag with no
paginate Post, Tag with infinite-scroll

@atomfrede
Copy link
Member

Is this a regression? I am pretty sure at least 403 have once been handled in another way

@mraible
Copy link
Contributor Author

mraible commented Nov 30, 2023

I tested my jhipster7-demo project and the bug happens there too. When you enter a URL for an entity that you don't have access to, you're logged out and redirected to the login page.

@mraible
Copy link
Contributor Author

mraible commented Nov 30, 2023

The 401 is caught by Angular in auth-expired.interceptor.ts. If I change it to a 403, the behavior is the same in JHipster 7.

Screenshot 2023-11-30 at 12 08 28 PM

@atomfrede
Copy link
Member

Interesting. Totally agree we should fix/change that

@mraible mraible added $100 https://www.jhipster.tech/bug-bounties/ $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ labels Dec 1, 2023
@mraible
Copy link
Contributor Author

mraible commented Dec 1, 2023

I added a bug bounty.

@atomfrede
Copy link
Member

Looking at the vue code, I think vue is handling these kind of errors, maybe I was thinking about that (didn't check react)

@atomfrede
Copy link
Member

atomfrede commented Dec 7, 2023

@mraible At least for 403 I can't reproduce this. The frontend shows a fine message

image

@mraible
Copy link
Contributor Author

mraible commented Dec 7, 2023

@atomfrede Does this happen when you click on an item that you don't have access to? In my case, I'm changing the ID in the URL. You can clone my example, add a couple of posts that belong to different blogs, then try to access one that you shouldn't be able to.

@atomfrede
Copy link
Member

Yes I just clicked on the link. Will try it by changing the id in the url.

@atomfrede
Copy link
Member

atomfrede commented Dec 10, 2023

@mraible You're right. Entering the url directly in the adressbar leads to an empty page

EDIT: I have found the issue, will do a PR later today. TL;DR; the issue is, in case you try to access the page directly a navigation error occurs, which is not handled, while when clicking the link the http request is handled during route transition (which does not occur when accessing the route directly, as far as I remember angular routing)

@atomfrede atomfrede self-assigned this Dec 10, 2023
atomfrede added a commit to atomfrede/generator-jhipster that referenced this issue Dec 10, 2023
atomfrede added a commit to atomfrede/generator-jhipster that referenced this issue Dec 10, 2023
atomfrede added a commit to atomfrede/generator-jhipster that referenced this issue Dec 10, 2023
atomfrede added a commit to atomfrede/generator-jhipster that referenced this issue Dec 10, 2023
atomfrede added a commit to atomfrede/generator-jhipster that referenced this issue Dec 11, 2023
atomfrede added a commit to atomfrede/generator-jhipster that referenced this issue Dec 12, 2023
@deepu105 deepu105 added this to the 8.2.0 milestone Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
$$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: angular theme: security $100 https://www.jhipster.tech/bug-bounties/
Projects
None yet
Development

No branches or pull requests

4 participants