Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A signature check fails for some requests: the client sends an HMAC as Base64 in a query parameter, and about 1 in 10 requests fail. What is the likely cause?

The likely cause is that the client sends standard Base64, whose + and / and = characters are not safe in a query string, without URL-encoding it. A + is decoded by the server as a space, and depending on the libraries / and = may be altered too, so only the requests whose HMAC happens to contain those characters fail. The fix is URL-safe Base64 without padding, or proper URL-encoding of the value, then comparing decoded bytes with MessageDigest.isEqual in constant time, making sure both sides sign the same UTF-8 canonical input, and logging decoded lengths, never secrets, while debugging.

Why this question is asked

Intermittent signature failures are some of the most frustrating bugs to debug, because the keys are right, the algorithm is right, and most requests work. When a failure rate is stable and seems random, it usually means the problem depends on the data, not on the code path. Interviewers like this question because it combines encodings, URL handling and secure comparison in a realistic incident. In this answer, we will recap how the HMAC check works, find why only some requests fail, fix the encoding, make the comparison safe, check the signed input, and see what to log while debugging.

1A Quick Recap of the HMAC Check

An HMAC (hash-based message authentication code) is a tag computed from a message and a shared secret key, for example with HmacSHA256, which produces 32 bytes. The client computes the tag over the request, encodes the bytes as Base64 to make them text, and sends them in a query parameter such as sig. The server recomputes the tag over the same request with the same key and compares. If even one bit differs, the check fails.

So a failure means one of three things: the key differs, the signed input differs, or the tag was changed in transit. A key mismatch would fail every request. A rate of about 1 in 10 points at something that depends on each request's data.

2Why Only Some Requests Fail

Standard Base64 uses an alphabet that includes +, / and the = padding character. In a query string, + is the old form encoding for a space, and servers decode it that way: the servlet getParameter method, Spring's @RequestParam and most frameworks turn + into a space. So if the client puts the raw Base64 into the URL, any tag containing + arrives altered. Some clients, proxies or frameworks also treat / and = specially, or normalise them, depending on the libraries.

Whether a given tag contains one of these characters depends on its bytes, which are effectively random. Each Base64 character has a 1 in 64 chance of being +, so the fraction of affected requests depends on the tag length and on which characters the client's library leaves unencoded; a steady partial failure rate like 1 in 10 is the typical sign of this data-dependent problem.

Java
// Client side, buggy: raw standard Base64 in the URL
String sig = Base64.getEncoder().encodeToString(hmac); // e.g. "Xk3+Lq/9aB...Zw="
String url = base + "/orders?id=42&ts=1730900000&sig=" + sig;
// Server side
String received = request.getParameter("sig"); // "Xk3 Lq/9aB...Zw=" : the + became a space
Base64.getDecoder().decode(received); // IllegalArgumentException: Illegal base64 character 20
// or, if the code strips spaces first, decoding gives different bytes and the HMAC check fails
Step throughHow a + in the signature breaks the check
ClientHTTP serverSignature check
HMAC -> standard Base64 "Xk3+Lq..."
GET ...&sig=Xk3+Lq... (not URL-encoded)
query decoding: + -> space
sig = "Xk3 Lq..."
decode fails or gives different bytes
401 signature invalid

6 messages in order. Play them, or step through them one at a time.

Requests whose tag has no +, / or = pass, which is why the failure looks random.

Notice that the error can show up in two ways. If the server decodes strictly, it throws on the space, which may be logged as a generic signature failure. If it is lenient or trims, the decoded bytes are simply wrong. Either way, only requests whose tag contained the character fail.

3Fix the Encoding

The cleanest fix is URL-safe Base64 without padding on the client, which uses - and _ instead of + and / and drops the =, so the value passes through any URL unchanged. The server decodes with the URL decoder. If changing the client format is not possible right away, the client must URL-encode the standard Base64 value (so + becomes %2B, / becomes %2F and = becomes %3D), and the server's normal parameter decoding restores it. Sending the signature in a header instead of the query string also avoids query decoding.

Java
// Client
String sig = Base64.getUrlEncoder().withoutPadding().encodeToString(hmac); // "Xk3-Lq_9aB...Zw"
// or, keeping standard Base64:
String sigParam = URLEncoder.encode(Base64.getEncoder().encodeToString(hmac), StandardCharsets.UTF_8);
// "Xk3%2BLq%2F9aB...Zw%3D"
// Server
byte[] receivedTag = Base64.getUrlDecoder().decode(request.getParameter("sig")); // 32 bytes

During a migration, the server can accept both: try the URL decoder, and if the value contains characters from the standard alphabet, decode it with the standard decoder after replacing spaces with +. That lets old clients keep working while they upgrade.

4Compare Bytes in Constant Time

Once decoding is right, how we compare matters for security. Comparing Base64 strings is fragile, because padding and alphabet differences make equal tags look different. We compare decoded bytes. And we use MessageDigest.isEqual rather than Arrays.equals or String.equals, because those return as soon as one byte differs, so the response time leaks how many leading bytes matched, which in principle lets an attacker forge a tag byte by byte. MessageDigest.isEqual takes the same time regardless of where the first difference is.

Java
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(clientSecret, "HmacSHA256"));
byte[] expected = mac.doFinal(canonical.getBytes(StandardCharsets.UTF_8));
boolean ok = MessageDigest.isEqual(expected, receivedTag); // constant time
// receivedTag.length != 32 -> treat as invalid before comparing

5Check That Both Sides Sign the Same Input

The other half of an HMAC check is the message. Both sides must sign exactly the same bytes, so we define a canonical form: which fields, in which order, with which separators, and the character encoding, always UTF-8. A typical canonical string for a request is the method, the path, the query parameters sorted by name excluding sig, and the timestamp, joined by newlines. If the client signs the parameters URL-encoded and the server signs them decoded, or one side uses the platform default charset, requests with spaces or non-ASCII characters (such as a name containing U+00E9) fail, which is another data-dependent failure that looks random.

Java
String canonical = String.join("\n",
"GET", "/orders", "id=42&ts=1730900000"); // sorted, decoded values, no sig
byte[] input = canonical.getBytes(StandardCharsets.UTF_8); // never getBytes() without a charset

6What to Log While Debugging

When investigating, the useful facts are structural: the length of the received sig parameter, the decoded tag length (32 for HmacSHA256), whether decoding threw, whether the raw parameter contained a space, +, / or =, and a hash or the length of the canonical string on both sides. A decoded length of 31 or a space in the parameter tells the whole story without exposing anything.

We never log the shared secret, and we avoid logging full tags or full canonical strings that may include personal data; a short prefix of a SHA-256 of the canonical string is enough to see whether client and server signed the same input.

7Follow-up questions

Why does a key mismatch not explain the 1 in 10 rate?

Because with the wrong key every tag would differ, so every request would fail. A partial, stable rate means the failure depends on something that varies between requests, such as the characters in the tag or in the signed values.

Should the signature include a timestamp?

Yes. Including a timestamp in the signed input and rejecting requests older than a few minutes prevents replay of captured requests. It must be part of the canonical string, and clocks need to be roughly in sync.

8Conclusion

In this answer, we traced intermittent HMAC failures to standard Base64 in a query string, where + is decoded as a space and / or = may be altered, so only tags containing those characters fail. We fixed it with URL-safe Base64 without padding or URL-encoding, compared decoded bytes with MessageDigest.isEqual in constant time, made both sides sign the same UTF-8 canonical input, and logged decoded lengths rather than secrets while debugging.

What to say in the interviewAbout 90 seconds aloud

Open with

A stable partial failure rate with correct keys tells me the problem depends on the data, and the classic cause here is the encoding. Standard Base64 includes plus, slash and equals.

Then, if they want more

If the client puts the Base64 tag into the query string without URL-encoding it, the server's parameter decoding turns every plus into a space, and depending on the libraries slash and equals can be changed too.

So only the requests whose tag happens to contain those characters fail: the decoder either throws IllegalArgumentException on the space, or produces different bytes and the comparison fails. To confirm it, I would log the decoded length and whether the raw parameter contained a space, never the secret.

The fix is URL-safe Base64 without padding on the client and the URL decoder on the server, or at least URL-encoding the value, or moving the signature into a header. During migration the server can accept both formats. I would also check two related things.

The comparison should be on decoded bytes with MessageDigest.isEqual, which is constant time, rather than comparing strings or using Arrays.equals, which leak timing.

And both sides must sign exactly the same canonical input, the same fields in the same order, decoded or encoded consistently, and always as UTF-8 bytes, because a charset mismatch causes the same kind of intermittent failure for values with non-ASCII characters.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Security reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A signature check fails for some requests: the client sends an HMAC as Base64 in a query...": https://scrivio-interview-library.netlify.app/spring-boot/secrets-and-crypto/b529cb6886/