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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

project_loader: handle invalid unicode chars #1941

Merged
merged 6 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions snapcraft/internal/project_loader/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import jsonschema
import yaml
import yaml.reader


import snapcraft
from snapcraft.internal import common, deprecations, remote_parts, states
Expand Down Expand Up @@ -262,12 +264,21 @@ def _snapcraft_yaml_load(yaml_file):
else:
encoding = 'utf-8'

# Work-around for https://github.com/yaml/pyyaml/issues/25
yaml.reader.Reader.NON_PRINTABLE = re.compile(
u'[^\x09\x0A\x0D\x20-\x7E\x85\xA0-'
'\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]')

try:
with open(yaml_file, encoding=encoding) as fp:
return yaml.load(fp)
except yaml.scanner.ScannerError as e:
raise errors.YamlValidationError('{} on line {} of {}'.format(
e.problem, e.problem_mark.line + 1, yaml_file)) from e
except yaml.reader.ReaderError as e:
raise errors.YamlValidationError(
'Invalid character {!r} at position {} of {}: {}'.format(
chr(e.character), e.position + 1, yaml_file, e.reason)) from e


def _ensure_confinement_default(yaml_data, schema):
Expand Down
35 changes: 35 additions & 0 deletions snapcraft/tests/unit/project_loader/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,41 @@ def test_invalid_yaml_missing_icon(self):
self.assertThat(raised.message,
Equals("Specified icon 'icon.png' does not exist"))

def test_invalid_yaml_invalid_unicode_chars(self):
fake_logger = fixtures.FakeLogger(level=logging.ERROR)
self.useFixture(fake_logger)

self.make_snapcraft_yaml("""name: foobar
version: "1"
summary: test\uffff
description: nothing
""")
raised = self.assertRaises(
errors.YamlValidationError,
_config.Config)

self.assertThat(raised.message, Equals(
"Invalid character '\\uffff' at position 40 "
"of snap/snapcraft.yaml: special characters are not allowed"))

def test_invalid_yaml_invalid_unicode_workaround(self):
fake_logger = fixtures.FakeLogger(level=logging.ERROR)
self.useFixture(fake_logger)

self.make_snapcraft_yaml("""name: foobar
version: "1"
summary: test馃挬
description: nothing

parts:
part1:
plugin: go
stage-packages: [fswebcam]
""")
c = _config.Config()

self.assertThat(c.data['summary'], Equals('test馃挬'))

def test_invalid_yaml_invalid_name_chars(self):
fake_logger = fixtures.FakeLogger(level=logging.ERROR)
self.useFixture(fake_logger)
Expand Down