Skip to content

Commit 4a5fcbe

Browse files
authored
Don't allow restricted characters in identifiers (UTS 39, C1) (#11580)
1 parent 8e154d8 commit 4a5fcbe

File tree

6 files changed

+2522
-5
lines changed

6 files changed

+2522
-5
lines changed

lib/elixir/pages/unicode-security.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Unicode Security
2+
3+
(See [Unicode Syntax](unicode-syntax.html) for information on Unicode usage in Elixir).
4+
5+
Elixir will prevent, or warn on, confusing or suspicious uses of Unicode in identifiers since Elixir v1.15, as defined in the [Unicode Technical Standard #39](https://unicode.org/reports/tr39/) on Security.
6+
7+
The focus of this document is to describe how Elixir implements the conformance clauses from that standard, referred to as C1, C2, and so on. All quotes are from the spec unless otherwise noted.
8+
9+
## C1. General Security Profile for Identifiers
10+
11+
Elixir will not allow tokenization of identifiers with codepoints in `\p{Identifier_Status=Restricted}`.
12+
13+
> An implementation following the General Security Profile does not permit any characters in \p{Identifier_Status=Restricted}, ...
14+
15+
For instance, the 'HANGUL FILLER' (``) character, which is often invisible, is an uncommon codepoint and will trigger this warning.
16+
17+
## C2, C3 (planned)
18+
19+
Elixir may implement Confusable Detection, and Mixed-Script Confusable detection, in the future, and will likely emit warnings in those cases; there is a reference implementation.
20+
21+
## C4, C5 (inapplicable)
22+
23+
'C4 - Restriction Level detection' conformance is not claimed and is inapplicable. (It applies to classifying the level of safety of a given arbitrary string into one of 5 restriction levels).
24+
25+
'C5 - Mixed number detection' conformance is inapplicable as Elixir does not support Unicode numbers.

lib/elixir/pages/unicode-syntax.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Quoted identifiers, such as strings (`"olá"`) and charlists (`'olá'`), support
66

77
Elixir also supports Unicode in identifiers since Elixir v1.5, as defined in the [Unicode Annex #31](https://unicode.org/reports/tr31/). The focus of this document is to describe how Elixir implements the requirements outlined in the Unicode Annex. These requirements are referred to as R1, R6 and so on.
88

9+
Elixir provides identifier security as defined in the [Unicode Technical Standard #39](https://unicode.org/reports/tr39/) on Security. The [Unicode Security](unicode-security.html) document describes how Elixir implements the clauses of that Standard.
10+
911
To check the Unicode version of your current Elixir installation, run `String.Unicode.version()`.
1012

1113
## R1. Default Identifiers

lib/elixir/test/elixir/kernel/parser_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@ defmodule Kernel.ParserTest do
499499
end
500500

501501
test "invalid token" do
502+
assert_syntax_error(
503+
~r/nofile:1:1: unexpected token: "#{"\u3164"}" \(column 1, code point U\+3164\)/,
504+
'ㅤ = 1'
505+
)
506+
502507
assert_syntax_error(
503508
~r/nofile:1:7: unexpected token: "#{"\u200B"}" \(column 7, code point U\+200B\)/,
504509
'[foo: \u200B]\noops'

0 commit comments

Comments
 (0)