Skip to content

MINOR: Include the inner exception stack trace when re-throwing an exception#12229

Merged
ijuma merged 3 commits intoapache:trunkfrom
divijvaidya:propagate-exception
Jan 15, 2023
Merged

MINOR: Include the inner exception stack trace when re-throwing an exception#12229
ijuma merged 3 commits intoapache:trunkfrom
divijvaidya:propagate-exception

Conversation

@divijvaidya
Copy link
Member

Problem:

While wrapping the caught exception into a custom one, information about the caught exception is being lost, including information about the stack trace of the exception.

Change

When re-throwing an exception, we make sure to include the stack trace. Otherwise pertinent debug information is lost.

@divijvaidya
Copy link
Member Author

@dengziming @hachikuji kindly review.

Reviewer(s) please note that all tests failing for this CI are known to be flaky and are not related to this code change.

Test 1 - testListenerConnectionRateLimitWhenActualRateAboveLimit - fix is pending PR review #12045

Test 2 - testTopicIdUpgradeAfterReassigningPartitions - fix is pending PR review #11687

Test 3 - testSnapshotOnlyAfterConfiguredMinBytes - (possible) fix is pending PR review #12224

Test 4 - Created a ticket at https://issues.apache.org/jira/browse/KAFKA-13951

Copy link
Contributor

@Kvicii Kvicii left a comment

Choose a reason for hiding this comment

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

LGTM.

@divijvaidya divijvaidya force-pushed the propagate-exception branch from 10eeee8 to da8cca4 Compare June 8, 2022 08:12
@divijvaidya
Copy link
Member Author

@hachikuji Please review. This should be a small one.

Copy link
Member

@dengziming dengziming left a comment

Choose a reason for hiding this comment

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

Can this kind of problem be caught by spotbugs? manual checking is error prone.

@divijvaidya
Copy link
Member Author

divijvaidya commented Jun 15, 2022

Can this kind of problem be caught by spotbugs? manual checking is error prone.

Agreed @dengziming but unfortunately spotbugs isn't catching such errors.

@divijvaidya
Copy link
Member Author

@mimaison perhaps you may want to look into this? This already has 2 approvals from non-committers.

Copy link
Member

Choose a reason for hiding this comment

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

Have we checked that we're not introducing a security vulnerability in these security classes by including the original exception?

Copy link
Member Author

Choose a reason for hiding this comment

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

UnsupportedEncodingException is thrown for 2 cases and in either of the two cases, the string being encoded is not printed in the exception message and hence, we don't leak any message information using the exception.

Case 1: for UnsupportedCharsetException in which case, only charset is printed in the string as per the implementation as below:

public class UnsupportedCharsetException
    extends IllegalArgumentException
{

    private static final long serialVersionUID = 1490765524727386367L;

    private String charsetName;

    /**
     * Constructs an instance of this class.
     *
     * @param  charsetName
     *         The name of the unsupported charset
     */
    public UnsupportedCharsetException(String charsetName) {
        super(String.valueOf(charsetName));
	this.charsetName = charsetName;
    }

    /**
     * Retrieves the name of the unsupported charset.
     *
     * @return  The name of the unsupported charset
     */
    public String getCharsetName() {
        return charsetName;
    }

}

Case 2: for IllegalCharsetNameException which again, just prints the name of the charset. See implementation as below:

public class IllegalCharsetNameException
    extends IllegalArgumentException
{

    private static final long serialVersionUID = 1457525358470002989L;

    private String charsetName;

    /**
     * Constructs an instance of this class.
     *
     * @param  charsetName
     *         The illegal charset name
     */
    public IllegalCharsetNameException(String charsetName) {
        super(String.valueOf(charsetName));
	this.charsetName = charsetName;
    }

    /**
     * Retrieves the illegal charset name.
     *
     * @return  The illegal charset name
     */
    public String getCharsetName() {
        return charsetName;
    }

}

Is there any other security risk that you are alluding to here? Adding the stack trace is beneficial here to quickly determine whether the failure is due to case 1 or case 2.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation @divijvaidya , yes, I checked and confirmed it only output the charset name. So it's safe. @ijuma , any concern/comments here?

@divijvaidya
Copy link
Member Author

@ijuma I have addressed your comments. This is ready for your review (pending test run).

Copy link
Contributor

@mdedetrich mdedetrich left a comment

Choose a reason for hiding this comment

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

These changes seem good for me

@divijvaidya divijvaidya force-pushed the propagate-exception branch from 3c82b83 to bc87930 Compare August 4, 2022 15:19
@divijvaidya
Copy link
Member Author

@tombentley requesting code review bandwidth from you here. We already have 3 non-committer approvals on this one.

Copy link
Member

@showuon showuon left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the improvement.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation @divijvaidya , yes, I checked and confirmed it only output the charset name. So it's safe. @ijuma , any concern/comments here?

@divijvaidya
Copy link
Member Author

Rebased with the latest trunk to resolve merge conflicts.

@showuon
Copy link
Member

showuon commented Sep 28, 2022

I'm going to merge this PR this week if there's no more comments. Thanks.

Copy link
Member

@ijuma ijuma left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

@ijuma ijuma merged commit b2bc72d into apache:trunk Jan 15, 2023
ijuma added a commit to confluentinc/kafka that referenced this pull request Jan 17, 2023
…master

* apache-github/trunk: (23 commits)
  MINOR: Include the inner exception stack trace when re-throwing an exception (apache#12229)
  MINOR: Fix docs to state that sendfile implemented in `TransferableRecords` instead of `MessageSet` (apache#13109)
  Update ProducerConfig.java (apache#13115)
  KAFKA-14618; Fix off by one error in snapshot id (apache#13108)
  KAFKA-13709 (follow-up): Avoid mention of 'exactly-once delivery' or 'delivery guarantees' in Connect (apache#13106)
  KAFKA-14367; Add `TxnOffsetCommit` to the new `GroupCoordinator` interface (apache#12901)
  KAFKA-14568: Move FetchDataInfo and related to storage module (apache#13085)
  KAFKA-14612: Make sure to write a new topics ConfigRecords to metadata log iff the topic is created (apache#13104)
  KAFKA-14601: Improve exception handling in KafkaEventQueue apache#13089
  KAFKA-14367; Add `OffsetCommit` to the new `GroupCoordinator` interface (apache#12886)
  KAFKA-14530: Check state updater more often (apache#13017)
  KAFKA-14304 Use boolean for ZK migrating brokers in RPC/record (apache#13103)
  KAFKA-14003 Kafka Streams JUnit4 to JUnit5 migration part 2 (apache#12301)
  KAFKA-14607: Move Scheduler/KafkaScheduler to server-common (apache#13092)
  KAFKA-14367; Add `OffsetFetch` to the new `GroupCoordinator` interface (apache#12870)
  KAFKA-14557; Lock metadata log dir (apache#13058)
  MINOR: Implement toString method for TopicAssignment and PartitionAssignment (apache#13101)
  KAFKA-12558: Do not prematurely mutate internal partition state in Mirror Maker 2 (apache#11818)
  KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer (apache#13032)
  KAFKA-14600: Reduce flakiness in ProducerIdExpirationTest (apache#13087)
  ...
guozhangwang pushed a commit to guozhangwang/kafka that referenced this pull request Jan 25, 2023
…ception (apache#12229)

While wrapping the caught exception into a custom one, information about the caught
exception is being lost, including information about the stack trace of the exception.

When re-throwing an exception, we either include the original exception or the relevant
information is added to the exception message.

Reviewers: Ismael Juma <ismael@juma.me.uk>, Luke Chen <showuon@gmail.com>, dengziming <dengziming1993@gmail.com>, Matthew de Detrich <mdedetrich@gmail.com>
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.

6 participants