Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tab before multi line TXT string #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 52 additions & 23 deletions src/LTDBeget/dns/record/RData.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ class RData
*/
private $multiLineOpened = false;

/**
* Is the txt record surrounded by quotes
* @var bool
*/
private $txtRecordHasQuotes = false;

/**
* RData constructor.
*
Expand Down Expand Up @@ -268,50 +274,57 @@ private function txtExtractor(string $tokenName)
$this->stream->next();

// comment starts
if($ord === 59) {
if($ord === AsciiChar::SEMICOLON) {
$this->commentOpen = true;
goto start;
} elseif($this->commentOpen === true && $ord !== 10) {
} elseif($this->commentOpen === true && $ord !== AsciiChar::LINE_FEED) {
$this->commentOpen = true;
goto start;
} elseif($this->commentOpen === true && ($ord === 10 || $ord === 0)) {
} elseif($this->commentOpen === true && ($ord === AsciiChar::LINE_FEED || $ord === AsciiChar::NULL)) {
$this->stream->previous();
$this->commentOpen = false;
goto start;
} else {
// ignore blanck line
if($ord === 32) {
// ignore whitespace
if($ord === AsciiChar::SPACE || $ord === AsciiChar::HORIZONTAL_TAB) {
goto start;
}

// Find starts of char set
if($ord === 34 && !$this->commentOpen) { // "
$this->extractCharSet($tokenName);
}

// multi line opened
if($ord === 40 && !$this->commentOpen) {
if($ord === AsciiChar::OPEN_BRACKET && !$this->commentOpen) {
$this->multiLineOpened = true;
goto start;
}

// multi line closed
if($this->multiLineOpened && !$this->commentOpen && $ord === 41) {
elseif($this->multiLineOpened && !$this->commentOpen && $ord === AsciiChar::CLOSE_BRACKET) {
$this->multiLineOpened = false;
goto start;
}

// comment end in multi line TXT record
if($ord === 10 && $this->commentOpen && $this->multiLineOpened) {
elseif($ord === AsciiChar::LINE_FEED && $this->commentOpen && $this->multiLineOpened) {
goto start;
}

// is record ends?
if(!$this->multiLineOpened && ($ord === 10 || $ord === 0)) {
elseif(!$this->multiLineOpened && ($ord === AsciiChar::LINE_FEED || $ord === AsciiChar::NULL)) {
return;
} elseif($this->multiLineOpened && $ord === 10) {
} elseif($this->multiLineOpened && $ord === AsciiChar::LINE_FEED) {
goto start;
}
elseif(!$this->commentOpen) {
// Double quotes aren't required to start a string, but if they start the string then they must also end the string
if($ord !== AsciiChar::DOUBLE_QUOTES) {
$this->stream->previous();
$this->txtRecordHasQuotes = false;
} else {
$this->txtRecordHasQuotes = true;
}
$this->extractCharSet($tokenName);
}
}

// multi line should no longer be open
if($this->multiLineOpened) {
throw new SyntaxErrorException($this->stream);
}
}

Expand All @@ -328,16 +341,32 @@ private function extractCharSet(string $tokenName)
$ord = $this->stream->ord();
$this->stream->next();

if(!$escaping_open && $ord === 34) {
if(!$escaping_open && $this->txtRecordHasQuotes && $ord === AsciiChar::DOUBLE_QUOTES) {
$this->txtExtractor($tokenName);
} elseif(!$this->txtRecordHasQuotes && $ord === AsciiChar::SPACE) {
// If there aren't quotes around a character string, a space terminates the string
return;
} else {
if($ord === AsciiChar::LINE_FEED || $ord === AsciiChar::VERTICAL_TAB || $ord === AsciiChar::NULL) {
$this->stream->previous();
throw new SyntaxErrorException($this->stream);
if($this->txtRecordHasQuotes) {
$this->stream->previous();
throw new SyntaxErrorException($this->stream);
}
else {
return;
}
}

$this->tokens[$tokenName] .= chr($ord);
$escaping_open = ($ord === 92 && !$escaping_open);

// Escaping open is only set for one iteration so that it doesn't break on double slashes ie. \\
if($ord === AsciiChar::BACKSLASH) {
$escaping_open = true;
} else {
$escaping_open = false;
}

goto start;
}
}
}
}
2 changes: 1 addition & 1 deletion tests/SyntaxOkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testLifunRu()

public function testHell()
{
$expected = unserialize('a:25:{i:0;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"300";s:4:"TYPE";s:3:"SOA";s:5:"RDATA";a:7:{s:5:"MNAME";s:13:"ns1.beget.ru.";s:5:"RNAME";s:20:"hostmaster.beget.ru.";s:6:"SERIAL";s:10:"2015060403";s:7:"REFRESH";s:3:"300";s:5:"RETRY";s:3:"600";s:6:"EXPIRE";s:5:"86400";s:7:"MINIMUM";s:3:"300";}}i:1;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"600";s:4:"TYPE";s:1:"A";s:5:"RDATA";a:1:{s:7:"ADDRESS";s:12:"5.101.153.38";}}i:2;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"600";s:4:"TYPE";s:4:"AAAA";s:5:"RDATA";a:1:{s:7:"ADDRESS";s:14:"::ffff:a.b.c.d";}}i:3;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"300";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:13:"ns1.beget.ru.";}}i:4;a:4:{s:4:"NAME";s:30:"_xmpp-server._tcp.icq.beget.ru";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"SRV";s:5:"RDATA";a:4:{s:8:"PRIORITY";s:2:"10";s:6:"WEIGHT";s:1:"0";s:4:"PORT";s:4:"5247";s:6:"TARGET";s:6:"jabber";}}i:5;a:4:{s:4:"NAME";s:10:"autoconfig";s:3:"TTL";s:3:"600";s:4:"TYPE";s:5:"CNAME";s:5:"RDATA";a:1:{s:5:"CNAME";s:34:"cf-ssl00000-protected.example.com.";}}i:6;a:4:{s:4:"NAME";s:10:"arhangelsk";s:3:"TTL";s:3:"600";s:4:"TYPE";s:2:"MX";s:5:"RDATA";a:2:{s:10:"PREFERENCE";s:2:"10";s:8:"EXCHANGE";s:13:"mx2.beget.ru.";}}i:7;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:27:"Some text another Some text";}}i:8;a:4:{s:4:"NAME";s:4:"test";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:27:"Some text another Some text";}}i:9;a:4:{s:4:"NAME";s:7:"testtxt";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:188:"v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZKI3U+9acu3NfEy0NJHIPydxnPLPpnAJ7k2JdrsLqAK1uouMudHI20pgE8RMldB/TeWKXYoRidcGCZWXleUzldDTwZAMDQNpdH1uuxym0VhoZpPbI1RXwpgHRTbCk49VqlC";}}i:10;a:4:{s:4:"NAME";s:6:"testmx";s:3:"TTL";s:3:"600";s:4:"TYPE";s:2:"MX";s:5:"RDATA";a:2:{s:10:"PREFERENCE";s:2:"20";s:8:"EXCHANGE";s:8:"@;345345";}}i:11;a:4:{s:4:"NAME";s:7:"testmx2";s:3:"TTL";s:3:"600";s:4:"TYPE";s:2:"MX";s:5:"RDATA";a:2:{s:10:"PREFERENCE";s:2:"20";s:8:"EXCHANGE";s:18:".;23\'\'33\'123;;123;";}}i:12;a:4:{s:4:"NAME";s:13:"_domainkeytxt";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:10:"t=y;o=~;\0";}}i:13;a:4:{s:4:"NAME";s:3:"www";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:18:"@ A 79.125.10.157 ";}}i:14;a:4:{s:4:"NAME";s:4:"txt3";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:30:"www CNAME ;;;;\'werwerwer\'\0010";}}i:15;a:4:{s:4:"NAME";s:6:"nstest";s:3:"TTL";s:3:"300";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:3:"ns1";}}i:16;a:4:{s:4:"NAME";s:7:"nstest2";s:3:"TTL";s:3:"300";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:18:"85.249.229.194;111";}}i:17;a:4:{s:4:"NAME";s:63:"xn----7sbfndkfpirgcajeli2a4pnc.xn----7sbbfcqfo2cfcagacemif0ap5q";s:3:"TTL";s:3:"300";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:7:"1.1.1.1";}}i:18;a:4:{s:4:"NAME";s:6:"casino";s:3:"TTL";s:3:"600";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:5:"@;234";}}i:19;a:4:{s:4:"NAME";s:5:"bonus";s:3:"TTL";s:3:"600";s:4:"TYPE";s:5:"CNAME";s:5:"RDATA";a:1:{s:5:"CNAME";s:5:"@;111";}}i:20;a:4:{s:4:"NAME";s:1:"*";s:3:"TTL";s:3:"600";s:4:"TYPE";s:5:"CNAME";s:5:"RDATA";a:1:{s:5:"CNAME";s:5:"s;111";}}i:21;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"SRV";s:5:"RDATA";a:4:{s:8:"PRIORITY";s:2:"10";s:6:"WEIGHT";s:1:"0";s:4:"PORT";s:4:"5269";s:6:"TARGET";s:1:"@";}}i:22;a:4:{s:4:"NAME";s:4:"xmpp";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"SRV";s:5:"RDATA";a:4:{s:8:"PRIORITY";s:2:"10";s:6:"WEIGHT";s:1:"0";s:4:"PORT";s:4:"5222";s:6:"TARGET";s:15:"81.211.107.230.";}}i:23;a:4:{s:4:"NAME";s:6:"www222";s:3:"TTL";s:1:"0";s:4:"TYPE";s:5:"CNAME";s:5:"RDATA";a:1:{s:5:"CNAME";s:9:"lifun.ru.";}}i:24;a:4:{s:4:"NAME";s:12:"46.20.191.35";s:3:"TTL";s:4:"3600";s:4:"TYPE";s:3:"PTR";s:5:"RDATA";a:1:{s:8:"PTRDNAME";s:11:"office;1231";}}}');
$expected = unserialize('a:25:{i:0;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"300";s:4:"TYPE";s:3:"SOA";s:5:"RDATA";a:7:{s:5:"MNAME";s:13:"ns1.beget.ru.";s:5:"RNAME";s:20:"hostmaster.beget.ru.";s:6:"SERIAL";s:10:"2015060403";s:7:"REFRESH";s:3:"300";s:5:"RETRY";s:3:"600";s:6:"EXPIRE";s:5:"86400";s:7:"MINIMUM";s:3:"300";}}i:1;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"600";s:4:"TYPE";s:1:"A";s:5:"RDATA";a:1:{s:7:"ADDRESS";s:12:"5.101.153.38";}}i:2;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"600";s:4:"TYPE";s:4:"AAAA";s:5:"RDATA";a:1:{s:7:"ADDRESS";s:14:"::ffff:a.b.c.d";}}i:3;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"300";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:13:"ns1.beget.ru.";}}i:4;a:4:{s:4:"NAME";s:30:"_xmpp-server._tcp.icq.beget.ru";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"SRV";s:5:"RDATA";a:4:{s:8:"PRIORITY";s:2:"10";s:6:"WEIGHT";s:1:"0";s:4:"PORT";s:4:"5247";s:6:"TARGET";s:6:"jabber";}}i:5;a:4:{s:4:"NAME";s:10:"autoconfig";s:3:"TTL";s:3:"600";s:4:"TYPE";s:5:"CNAME";s:5:"RDATA";a:1:{s:5:"CNAME";s:34:"cf-ssl00000-protected.example.com.";}}i:6;a:4:{s:4:"NAME";s:10:"arhangelsk";s:3:"TTL";s:3:"600";s:4:"TYPE";s:2:"MX";s:5:"RDATA";a:2:{s:10:"PREFERENCE";s:2:"10";s:8:"EXCHANGE";s:13:"mx2.beget.ru.";}}i:7;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:27:"Some text another Some text";}}i:8;a:4:{s:4:"NAME";s:4:"test";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:27:"Some text another Some text";}}i:9;a:4:{s:4:"NAME";s:7:"testtxt";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:188:"v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZKI3U+9acu3NfEy0NJHIPydxnPLPpnAJ7k2JdrsLqAK1uouMudHI20pgE8RMldB/TeWKXYoRidcGCZWXleUzldDTwZAMDQNpdH1uuxym0VhoZpPbI1RXwpgHRTbCk49VqlC";}}i:10;a:4:{s:4:"NAME";s:6:"testmx";s:3:"TTL";s:3:"600";s:4:"TYPE";s:2:"MX";s:5:"RDATA";a:2:{s:10:"PREFERENCE";s:2:"20";s:8:"EXCHANGE";s:8:"@;345345";}}i:11;a:4:{s:4:"NAME";s:7:"testmx2";s:3:"TTL";s:3:"600";s:4:"TYPE";s:2:"MX";s:5:"RDATA";a:2:{s:10:"PREFERENCE";s:2:"20";s:8:"EXCHANGE";s:18:".;23\'\'33\'123;;123;";}}i:12;a:4:{s:4:"NAME";s:13:"_domainkeytxt";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:11:"t=y;o="~;\0";}}i:13;a:4:{s:4:"NAME";s:3:"www";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:18:"@ A 79.125.10.157 ";}}i:14;a:4:{s:4:"NAME";s:4:"txt3";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"TXT";s:5:"RDATA";a:1:{s:7:"TXTDATA";s:42:"www CNAME ;;;;\'werwerwer\'\0010\\\"quotetest";}}i:15;a:4:{s:4:"NAME";s:6:"nstest";s:3:"TTL";s:3:"300";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:3:"ns1";}}i:16;a:4:{s:4:"NAME";s:7:"nstest2";s:3:"TTL";s:3:"300";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:18:"85.249.229.194;111";}}i:17;a:4:{s:4:"NAME";s:63:"xn----7sbfndkfpirgcajeli2a4pnc.xn----7sbbfcqfo2cfcagacemif0ap5q";s:3:"TTL";s:3:"300";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:7:"1.1.1.1";}}i:18;a:4:{s:4:"NAME";s:6:"casino";s:3:"TTL";s:3:"600";s:4:"TYPE";s:2:"NS";s:5:"RDATA";a:1:{s:7:"NSDNAME";s:5:"@;234";}}i:19;a:4:{s:4:"NAME";s:5:"bonus";s:3:"TTL";s:3:"600";s:4:"TYPE";s:5:"CNAME";s:5:"RDATA";a:1:{s:5:"CNAME";s:5:"@;111";}}i:20;a:4:{s:4:"NAME";s:1:"*";s:3:"TTL";s:3:"600";s:4:"TYPE";s:5:"CNAME";s:5:"RDATA";a:1:{s:5:"CNAME";s:5:"s;111";}}i:21;a:4:{s:4:"NAME";s:1:"@";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"SRV";s:5:"RDATA";a:4:{s:8:"PRIORITY";s:2:"10";s:6:"WEIGHT";s:1:"0";s:4:"PORT";s:4:"5269";s:6:"TARGET";s:1:"@";}}i:22;a:4:{s:4:"NAME";s:4:"xmpp";s:3:"TTL";s:3:"600";s:4:"TYPE";s:3:"SRV";s:5:"RDATA";a:4:{s:8:"PRIORITY";s:2:"10";s:6:"WEIGHT";s:1:"0";s:4:"PORT";s:4:"5222";s:6:"TARGET";s:15:"81.211.107.230.";}}i:23;a:4:{s:4:"NAME";s:6:"www222";s:3:"TTL";s:1:"0";s:4:"TYPE";s:5:"CNAME";s:5:"RDATA";a:1:{s:5:"CNAME";s:9:"lifun.ru.";}}i:24;a:4:{s:4:"NAME";s:12:"46.20.191.35";s:3:"TTL";s:4:"3600";s:4:"TYPE";s:3:"PTR";s:5:"RDATA";a:1:{s:8:"PTRDNAME";s:11:"office;1231";}}}');
$config_path = realpath(__DIR__ . "/../zone/syntax_ok/hell");
$plain_config = file_get_contents($config_path);
static::assertEquals($expected, Tokenizer::tokenize($plain_config));
Expand Down
7 changes: 5 additions & 2 deletions zone/syntax_ok/hell
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ test 600 IN TXT (;11111
testtxt 600 IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZKI3U+9acu3NfEy0NJHIPydxnPLPpnAJ7k2JdrsLqAK1uouMudHI20pgE8RMldB/TeWKXYoRidcGCZWXleUzldDTwZAMDQNpdH1uuxym0VhoZpPbI1RXwpgHRTbCk49VqlC"
testmx 600 IN MX 20 @;345345
testmx2 600 IN MX 20 .;23''33'123;;123;
_domainkeytxt 600 IN TXT "t=y;o=~;\0";312
_domainkeytxt 600 IN TXT t=y;o="~;\0 ;test no quotes with comment and quote mid stream
www 600 IN TXT "@ A 79.125.10.157 "
txt3 600 IN TXT "www CNAME ;;;;'werwerwer'\0010"
txt3 600 IN TXT ( "www CNAME " ";;;"


";'werwerwer'\0010\\"quotetest" )
nstest 300 IN NS ns1
nstest2 300 IN NS 85.249.229.194;111
xn----7sbfndkfpirgcajeli2a4pnc.xn----7sbbfcqfo2cfcagacemif0ap5q 300 IN NS 1.1.1.1
Expand Down