In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, a magic string is an input that a programmer believes will never come externally and which activates otherwise hidden functionality. A user of this program would likely provide input that gives an expected response in most situations. However, if the user does in fact innocently provide the pre-defined input, invoking the internal functionality, the program response is often quite unexpected to the user (thus appearing "magical").
Background
Typically, the implementation of magic strings is due to time constraints. A developer must find a fast solution instead of delving more deeply into a problem and finding a better solution.
For example, when testing a program that takes a user's personal details and verifies their credit card number, a developer may decide to add a magic string shortcut whereby entering the unlikely input of "***" as a credit card number would cause the program to automatically proceed as if the card were valid, without spending time verifying it. If the developer forgets to remove the magic string, and a user of the final program happens to enter "***" as a placeholder credit card number while filling in the form, the user would inadvertently trigger the hidden functionality.
Resolution
Situations/issues of cause
Often there are significant time constraints out of the developer's control right from the beginning of their involvement in a project. Common issues that might lead to this anti-pattern as a result:
* Null != null
or any variation where a
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
doesn't compare bitwise to a supposedly identical type. This is an issue that can even occur within the same development environment (same programming language and compiler). This problem has a long history for numerical and boolean types and most compilers handle this well (with applicable warnings and errors, default resolution, etc...).
Nullable types such as strings have the difficulty of historically different definitions for
NULL
Null may refer to:
Science, technology, and mathematics Computing
* Null (SQL) (or NULL), a special marker and keyword in SQL indicating that something has no value
* Null character, the zero-valued ASCII character, also designated by , often use ...
. The errors/warnings produced are often general or a 'best fit' default error whose message does not actually describe what's going on. If the developer can't get enough clues to track the issue down through debugging, taking a short cut, and coding in a 'default' string, may be the only way to keep the project on schedule. One solution to this may be the application of the
Null Object pattern
In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (''null'') behavior. The null object design pattern, which describes the uses of such objects and their behavior (or lack thereof) ...
.
[Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates; 2004, ''Head First Design Patterns'', 1st ed., O'Reilly, Chapter 6, pg. 214, ''The Command Pattern'', , ]
* Programmed into a corner. Sometimes a design seems straightforward and even simple but turns out to have a logical flaw, dependent upon the possible user inputs, due to an often unforeseen circumstance towards the end of planned development. Thus a developer might feel the need to implement a user input with special security/operational allowances to deal with such circumstances. This can be particularly ironic since it will sometimes become obvious that a more robust design from the beginning would likely have left room to handle the flaw. However this would perhaps have taken too much time to implement and it might have conflicted with the fundamental engineering concept of
KISS
A kiss is the touch or pressing of one's lips against another person or an object. Cultural connotations of kissing vary widely. Depending on the culture and context, a kiss can express sentiments of love, passion, romance, sexual attraction, ...
, keeping a design and implementation simple and meeting only the initial necessary requirements.
* Allowing external access to a
global
Global means of or referring to a globe and may also refer to:
Entertainment
* ''Global'' (Paul van Dyk album), 2003
* ''Global'' (Bunji Garlin album), 2007
* ''Global'' (Humanoid album), 1989
* ''Global'' (Todd Rundgren album), 2015
* Bruno ...
flag.
Over-confidence that a global flag can never be set accidentally or maliciously (often a quite reasonable assumption) justifies such implementation for testing and debug purposes, especially for small applications with simple interfaces. If the distribution of the program is considerable however, it is usually just a matter of time before somebody sets the flag. An obvious solution is to never use a global variable in such a manner. A developer might also make the flag ''circumstantially'' accessible. So the magic string by itself would be dealt with by the program as any other input.
[Andrew Cumming; 2007, ''SQL Hacks'', 1st ed., O'Reilly, pg. 174, ''Prevent an SQL Injection Attack'', , ] The user has to then reproduce the setting as well as produce some collection of other events, that the user interface discreetly allows, for the flag to accept the setting; a far more unlikely scenario, though still possible.
Strict formatting
Restricting the format of the input is a possible maintenance (bug fixing) solution. Essentially, this means validating input information to check that it is in the correct format, in order to reduce the possibility of the magic string being discovered by the user. Examples include validating a telephone number to ensure that it contains only digits (and possibly spaces and punctuation to a limited extent) or checking that a person's name has a forename and a surname (and is appropriately capitalised). An exception is made for the magic string in the validation code so that it will not be rejected by validation. It is expected that, since a user would likely quickly notice the strict enforcement of formatting, it would likely not occur to the user to try inputting a string not conforming to the format. Therefore, it is very unlikely for the user to try the magic string.
As with any input validation process, it is important to ensure that the format is not restrictive in a way that unintentionally restricts the use of the application by some users. An example of this is restricting
telephone number
A telephone number is a sequence of digits assigned to a landline telephone subscriber station connected to a telephone line or to a wireless electronic telephony device, such as a radio telephone or a mobile telephone, or to other devices f ...
or
postal code
A postal code (also known locally in various English-speaking countries throughout the world as a postcode, post code, PIN or ZIP Code) is a series of letters or digits or both, sometimes including spaces or punctuation, included in a postal a ...
[Brian Knight, Allan Mitchell, Darren Green, Douglas Hinson, Kathi Kellenberger; 2005, ''Professional SQL server 2005 integration services'', 1st ed., John Wiley and Sons, Chapter 5, pg. 129, ''Handling Dirty Data'', , ] input based on one country's system (e.g. requiring every user to give a five-digit
ZIP code), causing problems for legitimate users who are based in other countries.
Purposeful implementation
As is often the case with anti-patterns, there exists specific scenarios where magic strings are a correct solution for an implementation. Examples include
cheat codes
Cheating in video games involves a video game player using various methods to create an advantage beyond normal gameplay, usually in order to make the game easier. Cheats may be activated from within the game itself (a cheat code implemented by ...
and
Easter eggs
Easter eggs, also called Paschal eggs, are eggs that are decorated for the Christian feast of Easter, which celebrates the resurrection of Jesus. As such, Easter eggs are common during the season of Eastertide (Easter season). The oldest tra ...
. Furthermore, there are cases when users invent magic strings, and systems that have not coded to accept them can produce unexpected results such as missing license plates.
Incidents
The following is a list of some known incidents where use of a magic string has caused problems.
* In several different cases, motorists with personalized strings on their
vehicle registration plates
A vehicle registration plate, also known as a number plate (British English), license plate (American English), or licence plate (Canadian English), is a metal or plastic plate attached to a motor vehicle or trailer for official identificatio ...
have received incorrect
traffic ticket
A traffic ticket is a notice issued by a law enforcement official to a Driving, motorist or other road user, indicating that the user has violated Traffic, traffic laws. Traffic tickets generally come in two forms, citing a moving violation, suc ...
s. In affected ticketing systems, when police officers would fill out a traffic ticket for a car with no registration plate, they would write "NOPLATE", "NOTAG", "MISSING", or similar. This caused issues when motorists were granted actual registration plates with these values, and thus began receiving numerous traffic tickets intended for these plateless vehicles.
* In 1999, hackers revealed a security flaw in
Hotmail
Outlook.com is a webmail service that is part of the Microsoft 365 product family. It offers mail, Calendaring software, calendaring, Address book, contacts, and Task management, tasks services.
Founded in 1996 by Sabeer Bhatia and Jack Smit ...
that permitted anybody to log in to any Hotmail account using the password 'eh'. At the time it was called "the most widespread security incident in the history of the Web".
* People with the last name Null have reported a variety of problems using online systems, such as being unable to book plane tickets, use government tax websites, or pay utility bills. The issue stems from these systems confusing their name for a
null pointer
In computing, a null pointer or null reference is a value saved for indicating that the pointer or reference does not refer to a valid object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown lengt ...
. Depending on the system, this may cause the system to not show their name, to ask the user to enter a different name (sometimes with a message claiming that the name field had been left blank), or to show an error message.
[{{cite magazine , last1=Null , first1=Christopher , title=Hello, I'm Mr. Null. My Name Makes Me Invisible to Computers , url=https://www.wired.com/2015/11/null/ , magazine=]Wired
''Wired'' (stylized as ''WIRED'') is a monthly American magazine, published in print and online editions, that focuses on how emerging technologies affect culture, the economy, and politics. Owned by Condé Nast, it is headquartered in San Fra ...
, access-date=30 January 2022 , date=5 November 2015
See also
*
Magic number (programming)
In computer programming, a magic number is any of the following:
* A unique value with unexplained meaning or multiple occurrences which could (preferably) be replaced with a named constant
* A constant numerical or text value used to identify a ...
*
Time formatting and storage bugs
In computer science, time formatting and storage bugs are a class of software bugs that may cause time and date calculation or display to be improperly handled. These are most commonly manifestations of arithmetic overflow, but can also be the re ...
, for problems that can be caused by magics
*
Sentinel value
In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data) is a special value in the context of an algorithm which uses its presence as a condition of termination, typically in ...
(aka flag value, trip value, rogue value, signal value, dummy data)
*
Canary value
Canary originally referred to the island of Gran Canaria on the west coast of Africa, and the group of surrounding islands (the Canary Islands). It may also refer to:
Animals Birds
* Canaries, birds in the genera ''Serinus'' and ''Crithagra'' in ...
, special value to detect buffer overflows
*
Video games cheat codes which have the same origin
*
XYZZY (command)
References
Anti-patterns