Plural Dynamics
All insights
Development

Handling Unicode Usernames in Dart or Flutter

Unicode, with its vast character set, offers amazing flexibility for representing text, making it ideal for handling Unicode usernames in Dart applications. However, this complexity can also introduce security…

Plural Dynamics8 min read

Unicode, with its vast character set, offers amazing flexibility for representing text, making it ideal for handling Unicode usernames in Dart applications. However, this complexity can also introduce security vulnerabilities if not handled properly. This is where PRECIS comes in! By using PRECIS, along with Dart’s built-in functionalities like codeUnits and runes, and the characters package, you can manage Unicode usernames in Dart securely and efficiently.

The Problem: Hidden Characters and Impersonation

Unicode includes non-printable characters like the Zero-width Space, invisible to the naked eye. Additionally, certain characters have visually similar counterparts, like the full-width and half-width versions of letters. These seemingly minor details can be exploited for malicious purposes.

Imagine usernames like “E lon,” “Elon,” “El on,” and “Elo n.” Can you tell the real Elon from the imposters? Highlighting the text reveals the trickery – only the username that highlights entirely is genuine. Attackers can leverage such techniques to impersonate legitimate users.

The Solution: PRECIS to the Rescue

To combat these vulnerabilities, we turn to PRECIS, a set of rules defined in RFC 8264, RFC 8265, and RFC 8266. These guidelines establish best practices for validating and sanitizing strings containing Unicode characters. Here’s what PRECIS offers:

  • Username and Password Rules (RFC 8265): Defines security considerations for usernames and passwords, ensuring they are robust and resistant to manipulation.
  • Nickname Rules (RFC 8266): Provides guidelines for handling nicknames, which may have more relaxed requirements compared to usernames but still need proper validation.

A Dart Implementation: Taking Control

While Dart offers comprehensive Unicode support, developer access to certain functionalities can be limited. To address this, we’ve built a custom character handling library, drawing inspiration from the open-source Java SDK’s Character classes. This allows for more granular control over Unicode characters within your Dart applications.

The development of this solution for handling Unicode usernames in Dart would not have been possible without the open-source spirit of the Java community and the valuable contributions of the rocks.xmpp.precis project by Christian Schudt. Their work served as a foundation for our Dart implementation, allowing us to effectively manage and secure Unicode usernames in Dart applications.

  • Download: The code is compatible with Flutter 3.3.2 and Dart 2.18.1. Head to the Precis Pub Page to download the latest version and integrate it into your projects.
  • Source Code and Documentation: Visit the GitHub repository to access the source code, submit bug reports, and explore additional documentation.

By incorporating PRECIS rules and custom character handling, you can significantly enhance the security of your Dart applications, especially when dealing with Unicode usernames and sensitive user-generated content. Additionally, by utilizing specific exception handling and carefully selecting the appropriate PRECIS profiles, you ensure robust validation and consistent processing. Consequently, this approach not only strengthens application security but also improves the reliability of handling user data, ultimately leading to a more secure and dependable Dart application.

Unicode, with its vast character set, offers immense flexibility for text representation. However, this complexity can create security vulnerabilities if not handled properly. PRECIS tackles this challenge by providing a set of rules for validating and sanitizing user-generated content.

Installation:

  • For Dart Projects: Use the command dart pub add precis.
  • For Flutter Projects: Leverage the command flutter pub add precis.

Import & Code Snippets:

  1. 1Import the Package: Begin by importing the precis package into your code. Here’s an example with a named import:

Dart

import ‘package:precis/precis.dart’ as precis;

  1. 1Code Examples: Let’s explore some common use cases for PRECIS:

Use Case 1: Validating Usernames

This code snippet checks if a username contains any invalid characters according to PRECIS guidelines:

Dart

// Returns a String with basic rules applied

// Throws [InvalidCodePointException] on failure

try {

precis.usernameCaseMapped.prepare(username);

} on PrecisException catch (e) {

print(‘Invalid username: ${e.message}’);

}

Use Case 2: Formatting Usernames for Duplicate Checks

This code snippet formats a username to identify potential duplicates, ensuring adherence to all PRECIS rules:

Dart

// Returns the formatted string with all rules applied

// Throws exceptions for invalid characters, LTR/RTL mixes, and empty strings

try {

precis.usernameCaseMapped.enforce(username);

} on PrecisException catch (e) {

print(‘Invalid username: ${e.message}’);}

Use Case 3: Comparing Passwords for Matching

This code snippet verifies if two passwords match, enforcing all PRECIS rules on both strings:

Dart

bool passwordsMatch(String password1, String password2) {

try {

return precis.opaqueString.compare(password1, password2) == 0;

} on PrecisException {

return false;

}

The code mentions three potential exceptions thrown by PRECIS functions:

  • InvalidCodePointException: Indicates the presence of invalid characters in the string.
  • InvalidDirectionalityException: Highlights issues with mixing left-to-right (LTR) and right-to-left (RTL) characters inappropriately.
  • EnforcementException: Signals other errors, such as empty strings violating PRECIS rules.

By integrating PRECIS and utilizing its validation and sanitization functions, you can significantly enhance the security of your Dart applications, particularly when dealing with user-generated content like usernames and passwords. Furthermore, this helps prevent malicious actors from exploiting vulnerabilities within your app.

Also, this section delves deeper into the functionalities of the PRECIS package, specifically focusing on the various profiles and exception handling.

PRECIS Profiles

The precis library offers four built-in PRECIS profiles, each aligned with the corresponding RFC:

  • usernameCaseMapped: Ideal for case-insensitive usernames, adhering to RFC 8265 guidelines. This ensures usernames are validated without regard to case variations.
  • usernameCasePreserved: Maintains case sensitivity for usernames, as defined in RFC 8265. However, this profile is suitable for scenarios where preserving the exact case entered by the user is important.
  • opaqueString: Designed specifically for passwords as per RFC 8265. By using this profile, you can enforce strict password validation rules.
  • nickname: Caters to nicknames, following RFC 8266. Nicknames may have more relaxed validation requirements compared to usernames, but still benefit from proper sanitization.

Utilizing Profile Methods for Effective Validation

Each profile provides a set of methods to interact with strings:

  • prepare(String): Ensures the string contains only characters permitted by the underlying PRECIS rule. It returns the original string for convenience. This method acts as a preliminary check.
  • enforce(String): Applies all relevant PRECIS rules to the provided string. It returns the formatted string with all rules enforced. Nonetheless, this method performs a more comprehensive validation and sanitization.
  • compare(String, String): Acts as a comparator, applying profile-specific rules to each string before comparison. This ensures accurate comparisons while considering PRECIS regulations.
  • toComparableString(String): Generates a new string with all PRECIS rules applied, suitable for comparisons. Additionally, this is a convenient way to ensure fair and consistent comparisons between user-generated strings.

Flexibility at Your Fingertips: Mixing and Matching Profiles

Consequently, the beauty of PRECIS lies in its flexibility when handling Unicode usernames in Dart. Indeed, you can seamlessly mix and match profiles and methods based on your specific needs. For example, you might first use usernameCaseMapped.prepare(username) for initial username validation to ensure that Unicode usernames in Dart are correctly formatted. Then, if case sensitivity is crucial for your application, you can subsequently follow up with usernameCasePreserved.enforce(username). Consequently, this approach allows for robust and customizable user input validation while also maintaining the security and integrity of your Dart applications.

Customizing PRECIS (Not Recommended):

While the library offers built-in profiles for common use cases, extending the abstract PrecisProfile class to create custom implementations is possible. However, this approach is generally not recommended as it requires in-depth understanding of PRECIS rules and potential pitfalls.

Also, the precis library utilizes exceptions to communicate validation errors. Here’s a breakdown of the exception hierarchy:

  • PrecisException: The base class for all exceptions thrown by the library. Catching this exception indicates a general validation error occurred.
  • InvalidDirectionalityException: Thrown when the string violates rules regarding left-to-right (LTR) and right-to-left (RTL) character mixing.
  • InvalidCodePointException: Signals the presence of invalid characters in the string after applying PRECIS preparation or enforcement.
  • EnforcementException: Indicates errors encountered during string processing, such as empty strings violating PRECIS rules.

For fine-grained control when handling Unicode usernames in Dart, you can catch the most specific exception relevant to your scenario. For example, if only invalid character handling is of concern, catching InvalidCodePointException is optimal. Alternatively, catching the general PrecisException offers a more generic approach for managing various error types. This strategy ensures that Unicode usernames in Dart are processed securely and efficiently, minimizing potential vulnerabilities.

By leveraging PRECIS profiles and effectively handling exceptions, you can significantly enhance user input validation when managing Unicode usernames in Dart applications. Although, this approach not only bolsters security but also ensures consistent and reliable processing of user-generated content. Moreover, using specific exceptions like InvalidCodePointException or more general ones like PrecisException allows for fine-tuned control, further reinforcing the integrity of user input and reducing potential vulnerabilities.

Thus, this section explores additional resources to complement your understanding of Unicode handling in Dart, particularly when working with PRECIS.

Built-in Functionalities: codeUnits and runes

Dart offers built-in mechanisms for handling Unicode characters:

  • codeUnits: Represents a string as a list of integer values, each corresponding to a Unicode code point. Furthermore, this is often used when working with byte-level data or interfacing with external systems.
  • runes: Represents a string as an iterable of Unicode scalar values (characters). This is more intuitive for human understanding and manipulation of characters.

For a detailed breakdown of Dart’s runes functionality, refer to the GeeksForGeeks article linked in the Additional Reading section.

Moreover, the characters package can be valuable for handling multi-byte characters within strings. However, it’s crucial to note that its parsing rules may not align perfectly with PRECIS requirements. Hence, in scenarios where PRECIS-style code unit parsing is essential, the characters package might not be the ideal choice.

Conclusion

By incorporating PRECIS, understanding built-in functionalities like codeUnits and runes, and carefully evaluating the use of the characters package, you can effectively manage Unicode usernames in Dart applications. Moreover, this approach not only empowers you to leverage the richness of Unicode but also ensures a secure environment for user-generated content. Consequently, it helps prevent vulnerabilities like the notorious “Elon Crypto Scammers” scenario. Understanding how to handle Unicode usernames in Dart is crucial for developing secure and robust applications.

In conclusion, mastering the management of Unicode usernames in Dart through these methods is crucial for developing secure and efficient applications. Stay tuned and explore our latest article to dive deeper into effective strategies and best practices for handling Unicode in Dart applications.

Found this useful? Share it with your team.

Start Your Project with Us!

Our engineers are ready to assist you. Contact us today, and we’ll provide a prompt response.