Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

doc($sce): Overhaul the $sce service's documentation. #15735

Closed
wants to merge 6 commits into from

Conversation

rjamet
Copy link
Contributor

@rjamet rjamet commented Feb 21, 2017

A big docs update around the $sce: there's a lot of content in there that's often misunderstood, and some of the documentation starts to get really old too. Plus some minor falsehoods in there... Overall, this isn't a major refactoring, but there's a lot of smaller changes.

cc @mprobst for another set of eyes on the contents.

Does this PR introduce a breaking change?
Nope, it's a doc-only CL.

Other information:

  • I haven't tested how the doc renders, as I couldn't find a way to do so ?

A big docs update around the $sce: there's a lot of content in there that's often misunderstood, and some of the documentation starts to get really old too.
src/ng/sce.js Outdated
* attribute interpolation, any dom event binding attribute interpolation
* such as for onclick, etc.) that uses the provided value.
* See {@link ng.$sce $sce} for enabling strict contextual escaping.
* Marks the parameter as trusted for the specified context, and returns the
Copy link
Contributor

Choose a reason for hiding this comment

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

This is misleading - this doesn't change the parameter at all, but the language suggests it: "marks the parameter...". Maybe "Returns a version of the parameter that is trusted for the specified context..." or so?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, right, my formulation might imply that there's a global list of trusted stuff somewhere. Changed to "Returns a trusted representation of the parameter for the specified context. "

src/ng/sce.js Outdated
* such as for onclick, etc.) that uses the provided value.
* See {@link ng.$sce $sce} for enabling strict contextual escaping.
* Marks the parameter as trusted for the specified context, and returns the
* trusted representation of it. This trusted object should later on be
Copy link
Contributor

Choose a reason for hiding this comment

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

s/should/will/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, done.

src/ng/sce.js Outdated
* See {@link ng.$sce $sce} for enabling strict contextual escaping.
* Marks the parameter as trusted for the specified context, and returns the
* trusted representation of it. This trusted object should later on be
* used as-is, without any security check, by sinks that use this context.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure users will undestand what sinks are in this context. Maybe just say "Angular", or Angular's templating engine, or so? It's less precise, but much closer to the mental model most users have.

Copy link
Contributor Author

@rjamet rjamet Feb 22, 2017

Choose a reason for hiding this comment

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

I've replaced it by "used as is without any security checks by bindings or directives that require this security context."

(I'd like to keep the notion that it's not automagical and that modules are on the hook for their contexts)

src/ng/sce.js Outdated
* For instance, marking a string as trusted for the $sce.HTML context will
* entirely bypass the potential $sanitize call in corresponding $sce.HTML
* sinks, such as an ng-bind-html directive, while using the same value
* passed as a string will cause it to be sanitized.
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be worth calling out that calling this method does not need to be called for most use cases where HTML is interpolated, as sanitization is fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added "Note that in most cases you won't need to call trustAsHtml: if you have the sanitizer loaded, passing the value itself will render all HTML that does not pose a security risk."

src/ng/sce.js Outdated
* for general documentation about strict contextual escaping.
*
* @param {string} type The kind of context in which this value is safe
* for use. e.g. url, resourceUrl, html, js and css.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit +4 indent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I do that for the whole file, while I'm at it? Do you have a style guide for doc?

Copy link
Contributor

Choose a reason for hiding this comment

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

Please do. I don't think we have a style guide in Angular, but +4 matches Google JS style and what you generally do in other languages.

Copy link
Contributor Author

@rjamet rjamet Feb 22, 2017

Choose a reason for hiding this comment

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

Ok, I'll do that. 80 or 100 cols, btw? (edit: CONTRIBUTING.md says 100, I'll go with that)

src/ng/sce.js Outdated
* Disabling auto-escaping is extremely dangerous, it usually creates a Cross Site Scripting
* (XSS) vulnerability in your application.
* </div>
* In practice, there's several cases. When given a string, then this should
Copy link
Contributor

Choose a reason for hiding this comment

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

s/should/will/, or just this runs checks and sanitization....

src/ng/sce.js Outdated
* bindings. (HTML is just one example of a context where rendering user controlled input creates
* security vulnerabilities.)
* disabled, this application allows the user to render arbitrary HTML into the DIV, which would
* be an XSS bug. In a more realistic example, one may be rendering user comments, blog articles,
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe XSS security bug or XSS security hole in your application? Users might not know what this means, being explicit might make them look it up.

src/ng/sce.js Outdated
* return value of {@link ng.$sce#trustAs $sce.trustAs}.)
* @param {*} value The value to mark as trusted for URL context.
* @returns {*} A wrapped version of value that can be used as a trusted variant
* of your `value` in URL context (currently unused.)
Copy link
Contributor

Choose a reason for hiding this comment

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

(currently untrusted) is a bit misleading, maybe used as a trusted variant of your previously untrusted value?

Copy link
Contributor Author

@rjamet rjamet Feb 22, 2017

Choose a reason for hiding this comment

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

it's unused, not untrusted. Fixed by saying more explicitly that the context is unused so the corresponding trustAs has no reason to be called.

@rjamet
Copy link
Contributor Author

rjamet commented Feb 22, 2017

Some more formatting fixes after your comments:

  • +4 spaces for indents
  • 100 col everywhere
  • s/@returns/@return/
  • I uniformized context names : it's not "x for a kind of context" anymore, just "x for context", and always refer to them with the form $sce.HTML
  • a bunch of backticks here and there

@mprobst
Copy link
Contributor

mprobst commented Feb 22, 2017

LGTM.

src/ng/sce.js Outdated
*
* @return {Array} the currently set whitelist array.
*
* The **default value** when no whitelist has been explicitly set is `['self']` allowing only
* same origin resource requests.
*
* <div class="alert alert-warning">
* **Note:** the default whitelist of 'self' is not recommanded if your app shares its origin
Copy link
Member

Choose a reason for hiding this comment

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

recommanded --> recommended

Copy link
Member

Choose a reason for hiding this comment

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

While you are at it, can you indent the preceeding paragraph as well for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

* @param {*} value The value that that should be considered trusted/safe.
* @returns {*} A value that can be used to stand in for the provided `value` in places
* where AngularJS expects a $sce.trustAs() return value.
* @param {*} value The value that that should be considered trusted.
Copy link
Member

Choose a reason for hiding this comment

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

that that --> that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Choose a reason for hiding this comment

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

#157325

Choose a reason for hiding this comment

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

#157325

src/ng/sce.js Outdated
* Disabling auto-escaping is extremely dangerous, it usually creates a Cross Site Scripting
* (XSS) vulnerability in your application.
* </div>
* In practice, there's several cases. When given a string, this run checks and sanitization to
Copy link
Member

Choose a reason for hiding this comment

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

there's --> there are
this run --> it runs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

src/ng/sce.js Outdated
* </div>
* In practice, there's several cases. When given a string, this run checks and sanitization to
* make it safe without prior assumptions. When given the result of a {@link
* ng.$sceDelegate#trustAs `$sceDelegate.trustAs`} call, this returns the originally supplied
Copy link
Member

Choose a reason for hiding this comment

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

this --> it (we generally try to avoid "this" as it can be confusing)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done (plus another this above clarified in this function)

src/ng/sce.js Outdated
* @returns {*} The value the was originally provided to {@link ng.$sceDelegate#trustAs
* `$sceDelegate.trustAs`} if valid in this context. Otherwise, throws an exception.
* `$sceDelegate.trustAs`} call, or anything else (which will not be considered trusted.)
* @return {*} A version of value that's safe to use in the given context, or throws an
Copy link
Member

Choose a reason for hiding this comment

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

of value --> of the value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

src/ng/sce.js Outdated
* context. That trust is formalized with a function call. This means that as a developer, you
* can assume all untrusted bindings are safe. Then, to audit your code for binding security issues,
* you just need to ensure the values you mark as trusted indeed are safe - because they were
* received from your server, sanitized by your library, etc.. You can organize your codebase to
Copy link
Member

Choose a reason for hiding this comment

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

etc.. --> etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

src/ng/sce.js Outdated
* through {@link ng.$sce#getTrusted $sce.getTrusted}. SCE doesn't play a role here.
* call `$sce.trustAs` on them (e.g.
* `<div ng-bind-html="'<b>implicitly trusted</b>'"></div>`) just works. The `$sceDelegate` will
* also use the `$sanitize` injectable if it is available when binding untrusted values to
Copy link
Member

Choose a reason for hiding this comment

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

injectable --> service (It would be clearer to the reader, as this is the term we use throughout the docs.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, right

src/ng/sce.js Outdated
*
* @param {*} value The value to mark as trusted for `$sce.CSS` context.
* @return {*} A wrapped version of value that can be used as a trusted variant
* of your `value` in CSS context. This context is currently unused, so there are almost no
Copy link
Member

Choose a reason for hiding this comment

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

CSS --> `$sce.CSS` (?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I missed that one. Fixed.

src/ng/sce.js Outdated
* takes any input, and either returns a value that's safe to use in the specified context,
* or throws an exception. This function is aware of trusted values created by the `trustAs`
* function and its shorthands, and when contexts are appropriate, returns the unwrapped value
* as-is. Finally, this function can also throw when there is no way to turn maybeTrusted in a
Copy link
Member

Choose a reason for hiding this comment

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

maybeTrusted --> `maybeTrusted`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

src/ng/sce.js Outdated
* Otherwise, throws an exception.
* @param {string} type The context in which this value is to be used.
* @param {*} maybeTrusted The result of a prior {@link ng.$sce#trustAs `$sce.trustAs`} call.
* @return {*} The value the was originally provided to {@link ng.$sce#trustAs `$sce.trustAs`}
Copy link
Member

Choose a reason for hiding this comment

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

the was --> that was

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@gkalpak
Copy link
Member

gkalpak commented Feb 28, 2017

BTW, regarding the 4-space indentation on @param or @return descriptions: I think that is some cases (e.g. when there are bullet-points or empty lines) dgeni will recongnize this indended blocks as code blocks and wrap them in <pre> tags. Have you checked the rendered output?

@gkalpak gkalpak added this to the Backlog milestone Feb 28, 2017
@rjamet
Copy link
Contributor Author

rjamet commented Mar 3, 2017

I don't know how to render these so far, so everything is blind edits... I'll look into dgeni.

@gkalpak
Copy link
Member

gkalpak commented Mar 4, 2017

My only concern is whether the 4-space indentations make some blocks incorrectly rendered as code blocks. Other than that it LGTM.

@rjamet, you can see the rendered output with the folloing commands:

grunt package   # May take a while :)
grunt webserver

The open http://localhost:8000/build/docs/.

(After you have run grunt package once, you can run grunt docs (to only update the docs) and reload your browser to see the changes.)

@rjamet
Copy link
Contributor Author

rjamet commented Mar 15, 2017

Your fears are justified, that parses paragraphs in param/return blocks as code blocks if there's an empty line.

I'm tempted to keep the +4 indentation, and work around this by either two spaces + simple line break (=<br/> in markdown), or </p><p>. Would that be fine? It lets us be consistent, even if it's not that pretty: the issue is then to know if anything else consumes this. BR loses paragraphs, paragraphs add markup that might not make sense in IDEs.

I spotted a few unrelated issues otherwise, I'll bundle them as part of the next fix.

@gkalpak
Copy link
Member

gkalpak commented Mar 20, 2017

Either way works for me, as long as the output looks the way it should.

Some capitalization was wrong, formatting fixes for +4 indentation on @param tags,
and uniformize some params descriptions on the same functions (scedelegate / sce).
@rjamet
Copy link
Contributor Author

rjamet commented Mar 21, 2017

Ok, fixed:

  • capitalization of the return sections
  • some sce/scedelegate uniformization between getTrusted parameters
  • one "js" that should have been $sce.JS
  • default values for the resource url white/blacklists should be described in description, not in return
  • +4 indentation everywhere it makes sense, </p><p> to keep paragraphs in there, except on the parseAs functions where I kept the +3 because +4-indented lists resist any dirty hack I could find :/

I checked all the $sce doc output, it's all sensible at least. I kept paragraphs where they made sense.

Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

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

I trust you that it looks good when rendered 😃
(You still need to fix the linting issue Travis complains about.)

@rjamet
Copy link
Contributor Author

rjamet commented Mar 21, 2017

Oops, sorry, I missed it. Fixed.

@gkalpak gkalpak closed this in 0d9d57d Mar 21, 2017
gkalpak pushed a commit that referenced this pull request Mar 21, 2017
A big docs update around `$sce`:
There is a lot of content in there that is often misunderstood, and some of the
documentation starts to get really old too. Also fixed capitalization,
formatting, indentation and uniformized `@param` descriptions.

Closes #15735
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants