Skip to content

Commit

Permalink
Fix authorization header encoding sometimes not working (#297)
Browse files Browse the repository at this point in the history
The regex probably adapted to the system language, allowing incorrect characters
  • Loading branch information
nielsvanvelzen committed Jul 13, 2021
1 parent 1a35394 commit 3b56a38
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ package org.jellyfin.sdk.api.client.util

public object AuthorizationHeaderBuilder {
public const val AUTHORIZATION_SCHEME: String = "MediaBrowser"
private val ENCODING_REGEX = "[^\\w\\s]".toRegex()

public fun encodeParameterValue(raw: String): String = raw.replace(ENCODING_REGEX, "").trim()
public fun encodeParameterValue(raw: String): String = raw
// Trim whitespace
.trim()
// Only allow ASCII characters
.toByteArray(Charsets.US_ASCII)
.toString(Charsets.US_ASCII)
// Remove characters used for parsing
.replace('=', '?')
.replace(',', '?')
.replace('"', '?')

public fun buildParameter(key: String, value: String): String {
// Check for bad strings to prevent endless hours debugging why the server throws http 500 errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ public class AuthorizationHeaderBuilderTests {
@Test
public fun `encodeParameter removes special characters`() {
assertEquals("test", encodeParameterValue("""test"""))
assertEquals("test", encodeParameterValue("""test+"""))
assertEquals("test", encodeParameterValue("""'test'"""))
assertEquals("", encodeParameterValue("""今日は"""))
assertEquals("", encodeParameterValue("""水母"""))
assertEquals("test+", encodeParameterValue("""test+"""))
assertEquals("'test'", encodeParameterValue("""'test'"""))
assertEquals("???", encodeParameterValue("""今日は"""))
assertEquals("??", encodeParameterValue("""水母"""))
assertEquals("??????", encodeParameterValue("""ἈᾼᾺΆᾍᾋ"""))
}

@Test
Expand Down

0 comments on commit 3b56a38

Please sign in to comment.