8000 Preserve URL hash on 'fragment' response by jonkoops · Pull Request #251 · keycloak/keycloak-js · GitHub
[go: up one dir, main page]

Skip to content

Preserve URL hash on 'fragment' response - #251

Merged
jonkoops merged 1 commit into
keycloak:mainfrom
jonkoops:preserve-complex-hash
Feb 4, 2026
Merged

jonkoops merged 1 commit into
keycloak:mainfrom
jonkoops:preserve-complex-hash

Conversation

@jonkoops
Copy link
Copy Markdown
Contributor

Fixes an issue where URL fragments with path-style routing (e.g., #/admin/maintenance/scripts) were being URL-encoded after OAuth callback, breaking some applications using hash-based routing. The fix correctly separates the application portion of the hash (e.g. routes) from OAuth callback parameters in fragment response.

Closes #241

@jonkoops
Copy link
Copy Markdown
Contributor Author

@djoho < 8000 a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/2manyvcos/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/2manyvcos">@2manyvcos could I ask you to test this implementation, and let me know if this solves the issues you are having?

This comment was marked as outdated.

@jonkoops
jonkoops force-pushed the preserve-complex-hash branch from a96520c to a056e35 Compare January 27, 2026 15:01
@jonkoops
Copy link
Copy Markdown
Contributor Author

I have a more solid idea for an implementation, marking this as draft until then.

@jonkoops
jonkoops marked this pull request as draft January 27, 2026 15:09

This comment was marked as off-topic.

@jonkoops
Copy link
Copy Markdown
Contributor Author

Alright, I went ahead and changed the implementation. It now tries to find the place where Keycloak appends the parameters at the end of the fragment, taking advantage of the rigid way Keycloak adds the parameters (always at the end, only prefixed with a & if a fragment exists).

This comment was marked as outdated.

@jonkoops
Copy link
Copy Markdown
Contributor Author

I tightened up the implementation a little more, there are now some extra test cases that handle various things like duplicated parameters, and some other various edge cases. Marking this as ready for review.

@jonkoops
jonkoops marked this pull request as ready for review January 30, 2026 11:04

This comment was marked as outdated.

This comment was marked as outdated.

@rmartinc rmartinc left a comment •
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jonkoops Maybe it is easier to just change the parseCallbackParamsmethod. Something like this works for me with your tests:

  #parseCallbackParams (paramsString, supportedParams) {
    const params = paramsString.split("&")
    /** @type {Record<string, string>} */
    const oauthParams = {}
    let result = ""

    for (const param of params.reverse()) {
      const entry = new URLSearchParams(param).entries().find(() => true);
      if (entry && supportedParams.includes(entry[0]) && !oauthParams[entry[0]]) {
        oauthParams[entry[0]] = entry[1]
      } else {
        result = result.length == 0 ? param : param + "&" + result
      }
    }

    return {
      paramsString: result,
      oauthParams
    }
  }

This does not modify the fragment or the query and respect all the characters. I have even added a test to see several &&& are maintained.

@jonkoops
jonkoops force-pushed the preserve-complex-hash branch 2 times, most recently from 0d8cfbd to dfaff40 Compare February 4, 2026 11:47
@jonkoops
jonkoops commented Feb 4, 2026
Copy link
Copy Markdown
Contributor Author

@rmartinc Yes, I like your suggestion. I have pushed a slightly modified version and added you as a co-author. Can I ask you for a re-review?

Initially, I was focussing a lot on having this code separated, as I want to eventually deprecate and remove this behavior, so I felt I needed to have a proper piece of code to detect this. However, I can simply introduce a flag to disable this behavior, and never send a fragment in the first place.

@jonkoops
jonkoops requested a review from Copilot February 4, 2026 11:53

This comment was marked as resolved.

rmartinc
rmartinc previously approved these changes Feb 4, 2026
@rmartinc rmartinc left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a minor comment. Just in case you want to also consider several ampersands in the original fragment...

Comment thread lib/keycloak.js Resolved
Closes keycloak#241

Co-authored-by: rmartinc <rmartinc@redhat.com>
Signed-off-by: Jon Koops <jonkoops@gmail.com>
@rmartinc rmartinc left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just check the length to not add the ampersand the first time.

Comment thread lib/keycloak.js Resolved
@jonkoops
jonkoops merged commit a60272e into keycloak:main Feb 4, 2026
10 of 13 checks passed
@jonkoops
jonkoops deleted the preserve-complex-hash branch February 4, 2026 12:51
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.

Update to 26.2.2: URL encoded redirect urls in Angular application

3 participants

0