Skip to content

Conversation

@damar-openlearning
Copy link
Contributor

No description provided.

${
typeof page.content === "string"
? DOMPurify.sanitize(page.content)
: page.content
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would it be that it's not a string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above it defaults to ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

content: string | Section[];

when it's an assessment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do those sections also have HTML?

Copy link
Contributor

@dcollien dcollien Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g.

const sanitizeAnswer = <T extends Answer>(answer: T) => ({
  ...answer,
  text: sanitize(answer.text)
});

const sanitizeAnswerArray = <T extends Answer>(answers: T[] | undefined) => answers ? answers.map(sanitizeAnswer) : undefined;

const sanitizeMatches = (matches: Match[] | undefined) => matches ?
  matches.map((match) => ({
    pair: [sanitizeAnswer(match.pair[0]), sanitizeAnswer(match.pair[1])]
  })): undefined;

const sanitize = (content: string | Section[]) => {
  if (typeof content === "string") {
    return DOMPurify.sanitize(content);
  } else {
    const sanitizedSection = content.map((section) => ({
      ...section,
      title: sanitize(section.title),
      question: sanitize(section.question),
      answers: sanitizeAnswerArray(section.answers),
      choices: sanitizeAnswerArray(section.choices),
      matches: sanitizeMatches(section.matches)
    }));
    return sanitizedSection;
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: any user editable field that could possibly be reflected back to a user as HTML needs to be sanitized

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do those sections also have HTML?

yes, only questions that can be in html format. currently i sanitized the question in coursemagic side because it's in escaped format, i sanitze it before escaping it. should the imscc-packger be the one that responsible for sanitizing the html?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants