Skip to content

[BUG] ApiToken does not generate a UUID for an empty-string token (only for null) #1023

@SuperCorleone

Description

@SuperCorleone

Describe the Bug

The constructor generates a random UUID only when token == null; an empty string "" is passed through unchanged, so getToken() returns "". The only token-generating call site (ApiTokenService.java:31new ApiToken(user, name)) passes null, and DB-loaded tokens always carry a real value, so this is not triggerable today — a defensive hardening item.

Root Cause

this.token = token != null ? token : UUID.randomUUID().toString();  // "" is not replaced

Suggested Fix

this.token = (token == null || token.isEmpty()) ? UUID.randomUUID().toString() : token;

Corresponding Test (generated)

@Test
public void testGetToken_WithEmptyToken_ReturnsGeneratedUUID() {
    // Arrange
    User user = new User("testuser", "Test User");
    ApiToken apiToken = new ApiToken(null, "", user, "test-name", null, null);
    
    // Act
    String actualToken = apiToken.getToken();
    
    // Assert
    assertNotNull(actualToken, "getToken() should not return null when token is empty");
    assertNotEquals("", actualToken, "getToken() should not return empty string when token is empty");
    assertTrue(UUID.fromString(actualToken) != null, "Generated token should be a valid UUID");
}

This input was generated by the test case generator TestFusion developed in our STAR lab.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions