Is this a legal problem?
As I understand it (ask your lawyer, etc.), all users posting comment to Stack Exchange agree to the terms of service. In part, this contains:
You agree that all Subscriber Content that You contribute to the
Network is perpetually and irrevocably licensed to Stack Exchange
under the Creative Commons Attribution Share Alike license. You grant
Stack Exchange the perpetual and irrevocable right and license to use,
copy, cache, publish, display, distribute, modify, create derivative
works and store such Subscriber Content and to allow others to do so
in any medium now known or hereinafter developed (“Content License”)
in order to provide the Services, even if such Subscriber Content has
been contributed and subsequently removed by You. Subscriber warrants,
represents and agrees Subscriber has the right to grant Stack Exchange
and the Network the rights set forth above. Subscriber represents,
warrants and agrees that it will not contribute any Subscriber Content
that (a) infringes, violates or otherwise interferes with any
copyright or trademark of another party
I have highlighted some sections there.
Basically, when you add content to Stack Exchange, you are saying that you have the right to do that, you are not infringing anyone's rights, and that you are giving the right to Stack Exchange to use it in a number of ways.
Like many user-content type sites (Youtube, twitter, etc.), Stack Exchange does not need to police it's users for copyright violations. Instead, it provides a mechanism for copyright owners to ask for any infringing content to be removed (think DMCA, etc., and similar to what Youtube does when you see links to videos that have been removed 'due to a claimed copyright violation').
So, as I understand it, it is not a Legal problem for us to read that content, or for Stack Exchange to publish that content. It may be a legal problem for the person who posted that content.
What is the right way to include such content?
Copying content from Stack Exchange to somewhere else
Content copied from Stack Exchange should always link back to the source where the content was copied from (which is required by the Creative Commons Share-alike with attribution license which Stack Exchange uses).
Copying content from somewhere else to Stack Exchange
Whether you have to create a link back to the source when posting a question/answer here is dependent on the license requirements of the source. Even if the content is free-to-use it strikes me that it would at least be polite, and in many cases useful to include the link.
The right thing to do depends on where you are copying your content from when you post it to Stack Exchange. The right thing to do may vary from:
- you cannot do it at all
- you may link back, but it is not required
- you have to link back
- you have to do some other things as well, or instead.
Note that in many cases, your use of the content on Stack Exchange may fall under "fair use", or other exclusions of the copyright and other laws of the country that has jurisdiction over your activities.
In other words, there is no clear answer to "what is right".
If content on Stack Exchange needs to attributed, how should it be done?
I tend to add a comment to a question when I find there is a potential issue.
Do not edit the code or content in to the question yourself, let the owner of the question do that. They are the one who should be attributing their work, if needed.
Apart from that, you can just stay quiet. If the actual copyright owner has an issue, they can take it up with Stack Exchange using the official channels that are designed to handle that exact situation. You really do not want to be involved.